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 freeman_lex
def follow_ghost_legs(n: int, legs: list[tuple[int, int]]) -> list[int]:
res = list(range(n + 1))
for i1, i2 in legs:
res[i1], res[i2] = res[i2], res[i1]
return res[1:]
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 24, 2025
Comments: