-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20200714_full_itinerary.py
More file actions
66 lines (52 loc) · 2.39 KB
/
20200714_full_itinerary.py
File metadata and controls
66 lines (52 loc) · 2.39 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''
2020-07-14
[from dailycodingproblems.com #41]
Given an unordered list of flights taken by someone, each represented as
(origin, destination) pairs, and a starting airport, compute the person's
itinerary. If no such itinerary exists, return null. If there are multiple
possible itineraries, return the lexicographically smallest one. All flights
must be used in the itinerary.
For example, given the list of flights [('SFO', 'HKO'), ('YYZ', 'SFO'),
('YUL', 'YYZ'), ('HKO', 'ORD')] and starting airport 'YUL', you should return
the list ['YUL', 'YYZ', 'SFO', 'HKO', 'ORD'].
Given the list of flights [('SFO', 'COM'), ('COM', 'YYZ')] and starting
airport 'COM', you should return null.
Given the list of flights [('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')]
and starting airport 'A', you should return the list ['A', 'B', 'C', 'A', 'C']
even though ['A', 'C', 'A', 'B', 'C'] is also a valid itinerary. However, the
first one is lexicographically smaller.
'''
def get_itinerary(flights, itinerary):
if not flights:
return itinerary
last_arrival = itinerary[-1]
for i, (departure, arrival) in enumerate(flights):
remaining_flights = flights[:i] + flights[i + 1:]
itinerary.append(arrival)
if departure == last_arrival:
return get_itinerary(remaining_flights, itinerary)
itinerary.pop()
return None
def get_first_itinerary(flights):
itineraries = []
for _ in range(len(flights)):
flights = [flights.pop()] + flights
for (departure, _) in flights:
itinerary = get_itinerary(flights, [departure])
if itinerary is not None:
itineraries.append(itinerary)
if len(itineraries) > 0:
itineraries.sort()
return itineraries[0]
return []
'''
# TESTS
get_first_itinerary([]) == []
get_first_itinerary([('A', 'B')]) == ['A', 'B']
get_first_itinerary([('B', 'A'), ('A', 'B')]) == ['A', 'B', 'A']
get_first_itinerary([('B', 'A'), ('A', 'C')]) == ['B', 'A', 'C']
get_first_itinerary([('SFO', 'HKO'), ('YYZ', 'SFO'), ('YUL', 'YYZ'), ('HKO', 'ORD')]) == ['YUL', 'YYZ', 'SFO', 'HKO', 'ORD']
get_first_itinerary([('SFO', 'HKO'), ('YUL', 'YYZ'), ('HKO', 'ORD')]) == []
get_first_itinerary([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')]) == ['A', 'B', 'C', 'A', 'C']
get_first_itinerary([('A', 'C'), ('B', 'C'), ('C', 'A'), ('A', 'B')]) == ['A', 'B', 'C', 'A', 'C']
'''