Skip to content

Instantly share code, notes, and snippets.

@wintermonster
Created December 9, 2022 04:12
Show Gist options
  • Save wintermonster/e4e08ade9488bba51e1354b596726377 to your computer and use it in GitHub Desktop.
Save wintermonster/e4e08ade9488bba51e1354b596726377 to your computer and use it in GitHub Desktop.
Solution to the cycling problem
list_of_races = [[('c','d'), ('a','b'),('b','c')],
[('C', 'B'), ('D', 'C'), ('B', 'E'), ('A', 'D')]]
for races in list_of_races:
print(races)
races = dict(races)
start_city = (set(races.keys()) - set(races.values())).pop()
end_city = (set(races.values()) - set(races.keys())).pop()
all_cities = list(set(races.keys()).union(set(races.values())))
order = start_city
while all_cities and start_city!= end_city:
next_city = races[start_city]
order += next_city
all_cities.remove(next_city)
start_city = next_city
print(order)
@wintermonster
Copy link
Author

Problem
A cycling race is composed of many legs. Each leg goes from one city(A) to another(B). Cyclists then rest for the night and start the next leg of journey from (B) to (C). If the legs are represented by Tuples (A,B), (B,C), (C,D)...and given a list of tuples out of order example [(C,D),(A,B),(B,C)...] can you print out the correct order of cities in the race (example "A B C D..")

Example : [(A C) (B D) (C B)]

output : A C B D

Example : [(C B) (D C) (B E) (A D)]

output : A D C B E

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment