Created
December 9, 2022 04:12
-
-
Save wintermonster/e4e08ade9488bba51e1354b596726377 to your computer and use it in GitHub Desktop.
Solution to the cycling problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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