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 -ej
def follow_ghost_legs(n: int, legs: list[tuple[int, int]]) -> list[int]:
ls = [0] * n
def follow(i: int) -> int:
current = i
for t in legs:
if current in t:
current = t[1] if current == t[0] else t[0]
return current
for i in range(1, n + 1):
ls[follow(i) - 1] = i
return ls
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!")
Sept. 4, 2025
Comments: