Advertisement
makispaiktis

DCP41 - Destinations

Oct 2nd, 2020 (edited)
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. '''
  2. This problem was asked by Facebook.
  3. Given an unordered list of flights taken by someone, each represented as (origin, destination) pairs, and a starting airport, compute the person's itinerary.
  4. If no such itinerary exists, return null. If there are multiple possible itineraries, return the lexicographically smallest one.
  5. All flights must be used in the itinerary.
  6. For example, given the list of flights [('SFO', 'HKO'), ('YYZ', 'SFO'), ('YUL', 'YYZ'), ('HKO', 'ORD')] and starting airport 'YUL',
  7. you should return the list ['YUL', 'YYZ', 'SFO', 'HKO', 'ORD'].
  8. Given the list of flights [('SFO', 'COM'), ('COM', 'YYZ')] and starting airport 'COM', you should return null.
  9. Given the list of flights [('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')] and starting airport 'A', you should return the list
  10. ['A', 'B', 'C', 'A', 'C'] even though ['A', 'C', 'A', 'B', 'C'] is also a valid itinerary. However, the first one is lexicographically smaller.
  11. '''
  12.  
  13. # 1st Function
  14. def isThisCityFirst(flights, city):
  15.     flag = False
  16.     for i in range(len(flights)):
  17.         if city == flights[i][0]:
  18.             flag = True
  19.             break
  20.     return flag
  21.  
  22. # 2nd Function
  23. def solve(flights, destination):
  24.     flightsCopy = flights.copy()
  25.     result = list()
  26.     result.append(destination)
  27.  
  28.     while len(flightsCopy) > 0:
  29.         # I have to check if the last city in my result exists as a first city in a flight
  30.         flag = isThisCityFirst(flightsCopy, result[len(result)-1])
  31.         if flag == False:
  32.             print("There is not a solution with beginning city = '" + str(destination) + "'")
  33.             return False
  34.         else:
  35.             # Now, that I know this city exists as first one of a trip, I have to search the next destination
  36.             for flight in flightsCopy:
  37.                 if result[len(result)-1] == flight[0]:
  38.                     result.append(flight[1])
  39.                     flightsCopy.remove(flight)
  40.                     break
  41.     return result
  42.  
  43.  
  44. # MAIN FUNCTION
  45. flights1 = [('SFO', 'HKO'), ('YYZ', 'SFO'), ('YUL', 'YYZ'), ('HKO', 'ORD')]
  46. destination1 = 'YUL'
  47. print(str(flights1) + " and " + str(destination1) + " ----> " + str(solve(flights1, destination1)))
  48. flights2 = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')]
  49. destination2 = 'A'
  50. print(str(flights2) + " and " + str(destination2) + " ----> " + str(solve(flights2, destination2)))
  51. flights3 = [('Thess', 'Athens'), ('Syros', 'Tinos'), ('Athens', 'Syros'), ('Larissa', 'Thess'), ('Falani', 'Larissa')]
  52. destination3 = 'Falani'
  53. print(str(flights3) + " and " + str(destination3) + " ----> " + str(solve(flights3, destination3)))
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement