-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsol.py
More file actions
26 lines (24 loc) · 778 Bytes
/
sol.py
File metadata and controls
26 lines (24 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def flightItinerary(flights: list, startingPoint: str) -> list:
H = {}
for flight in flights:
start, end = flight
if start not in H:
H[start] = [end]
else:
H[start].append(end)
H[start].sort()
print('H = ', H)
itinerary = [startingPoint]
while H:
start = itinerary[-1]
possibleEnds = H[start]
end = possibleEnds.pop(0)
itinerary.append(end)
if not H[start]:
del H[start]
return itinerary
if __name__ =='__main__':
flights = [('SFO', 'HKO'), ('YYZ', 'SFO'), ('YUL', 'YYZ'), ('HKO', 'ORD')]
print(flightItinerary(flights, 'YUL'))
flights = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')]
print(flightItinerary(flights, 'A'))