Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Follow Ghost Legs by zshlab
def follow_ghost_legs(n: int, legs: list[tuple[int, int]]) -> list[int]:
pos = list(range(1, n + 1))
for ed1, ed2 in legs:
pos[ed1-1], pos[ed2-1] = pos[ed2-1], pos[ed1-1]
return pos
print("Example:")
print(follow_ghost_legs(3, [(1, 2), (2, 3), (1, 2)]))
# These "asserts" are used for self-checking
assert follow_ghost_legs(3, [(1, 2), (2, 3), (1, 2)]) == [3, 2, 1]
assert follow_ghost_legs(4, [(2, 3), (1, 2), (2, 3), (3, 4)]) == [3, 2, 4, 1]
assert follow_ghost_legs(5, [(3, 4), (4, 5), (3, 4), (2, 3), (3, 4)]) == [1, 5, 4, 2, 3]
assert follow_ghost_legs(6, [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]) == [
2,
3,
4,
5,
6,
1,
]
print("The mission is done! Click 'Check Solution' to earn rewards!")
June 19, 2025
Comments: