Created
February 24, 2014 03:01
-
-
Save flaviocdc/9181244 to your computer and use it in GitHub Desktop.
TalentBuddy Plane Tickets - too slow!
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
from bisect import bisect_left | |
def index(a, x): | |
'Locate the leftmost value exactly equal to x' | |
i = bisect_left(a, x) | |
if i != len(a) and a[i] == x: | |
return i | |
raise ValueError | |
def get_journey(departure_ids, destination_ids): | |
departure_ids.sort() | |
destination_ids.sort() | |
departure_set = set(departure_ids) | |
destination_set = set(destination_ids) | |
diff = list(departure_set.symmetric_difference(destination_set)) | |
if diff[0] in departure_set: | |
origin = diff[0] | |
last = diff[1] | |
else: | |
origin = diff[1] | |
last = diff[0] | |
place = origin | |
itinerary = str(origin) | |
ticket = index(departure_ids, place) | |
while place != last: | |
destination = destination_ids[ticket] | |
itinerary = itinerary + " " + str(destination) | |
ticket = index(departure_ids, place) | |
place = departure_ids[ticket] | |
print itinerary | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment