Skip to content

Instantly share code, notes, and snippets.

@helloworld
Created May 1, 2018 03:39
Show Gist options
  • Save helloworld/03ca845613626e3aa1b72e9270c070b1 to your computer and use it in GitHub Desktop.
Save helloworld/03ca845613626e3aa1b72e9270c070b1 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from datetime import datetime, timedelta
from string import Template
going_text = \
"""JFK, Seville, 284, 1 stop, Mon 8/13 11:25 PM, 7h in Lisbon, Tue 8/14 3:35 PM, TAP
JFK, Madrid, 284, 1 stop, Mon 8/13 11:25 PM, 7h in Lisbon, Tue 8/14 9:20 PM, TAP
JFK, Madrid, 298, 2 stop, Wed 8/15 11:25 PM, 1h 45m in in Lisbon; 6h 15m in Porto, Thu 8/16 10:25 PM, TAP
JFK, Madrid, 263, 2 Stop, Sun 8/19 11:25 PM, 1h 45m in in Lisbon; 6h 15m in Porto, Mon 8/20 10:25 PM, TAP
JFK, Barcelona, 284 , 1 stop, Mon 8/13 11:25 PM, 10h 15m in Lisbon, Wed 8/15 12:20 AM, TAP
JFK, Barcelona, 329, 1 stop, Mon 8/13 11:25 PM, 5h 15m in Lisbon, Tue 8/14 7:20 PM, TAP
JFK, Barcelona, 328, 1 stop, Sun 8/19 11:25 PM, 7h 45m in Lisbon, Mon 8/20 7:20 PM, TAP"""
returning_text = \
"""Madrid, JFK, 344, 1 Stop, Wed 8/22 3:00 PM, 1h in Copenhagen, Wed 8/22 9:30 PM, Norwegian
Madrid, JFK, 366, 2 stops, Fri 8/24 10:20 AM, 2h in Porto; 3h 40m in Lisbon, Fri 8/24 8:00 PM, TAP
Madrid, JFK, 392, 1 stop, Sun 8/26 8:00 AM, 7h 40 min in London, Sun 8/26 8:05 PM, Norwegian
Barcelona, JFK, 360, 1 Stop, Wed 8/22 12:30 PM, 3h 40m in Copenhagen, Wed 8/22 9:30 PM, Norwegian
Barcelona, JFK, 365, 2 stops, Fri 8/24 10:00 AM, 1h 40m in Porto; 3h 40m in Lisbon, Fri 8/24 8:00 PM, TAP
Barcelona, JFK, 392, 1 stop, Sun 8/26 9:50 AM, 6h min in London, Sun 8/26 8:05 PM, Norwegian"""
def toArray(s):
return list(map(lambda line: [x.strip() for x in line.split(',')], s.split("\n")))
def toDate(s):
return datetime.strptime(s, "%a %m/%d %I:%M %p")
going = toArray(going_text)
returning = toArray(returning_text)
MIN_TRIP_DAYS = 6
trips = []
for g in going:
for r in returning:
(going_start, going_end, going_price, going_stop_count, going_departure, going_stop, going_arrival, going_airline) = g
(returning_start, returning_end, returning_price, returning_stop_count, returning_departure, returning_stop, returning_arrival, returning_airline) = r
going_departure_date = toDate(going_departure)
going_arrival_date = toDate(going_arrival)
returning_departure_date = toDate(returning_departure)
returning_arrival_date = toDate(returning_arrival)
total_trip_time = returning_arrival_date - going_departure_date + timedelta(1)
if(total_trip_time > timedelta(6)):
s = Template('''
Length: $total_trip_time :: $time_description
Going: $going_start to $going_end [$$$going_price] on $going_airline
departure: $going_departure,
stops: $going_stop
arrival: $going_arrival
Return: $returning_start to $returning_end [$$$returning_price]
depature: $returning_departure
stops: $returning_stop
arrival: $returning_arrival
Total: $$$total
''')
trip = s.substitute(
total_trip_time = str(total_trip_time).split(",")[0],
time_description = going_departure_date.strftime('%m/%d') + ' - '+ returning_arrival_date.strftime('%m/%d'),
total = int(going_price) + int(returning_price),
going_start = going_start,
going_end = going_end,
going_price = going_price,
going_airline = going_airline,
going_departure = going_departure,
going_stop = going_stop_count + " " + going_stop,
going_arrival = going_arrival,
returning_start = returning_start,
returning_end = returning_end,
returning_price = returning_price,
returning_airline = returning_airline,
returning_departure = returning_departure,
returning_stop = returning_stop_count + " " + returning_stop,
returning_arrival = returning_arrival,
)
trips.append((total_trip_time, trip))
sorted_trips = sorted(trips, key = lambda x: x[0])
starting_day = None
for i in sorted_trips:
if(str(i[0]).split(" ")[0] != starting_day):
print(str(i[0]).split(" ")[0] + " day trips")
print("=" * 20)
starting_day = str(i[0]).split(" ")[0]
print(i[1])
7 day trips
====================
Length: 7 days :: 08/19 - 08/26
Going: JFK to Madrid [$263] on TAP
departure: Sun 8/19 11:25 PM,
stops: 2 Stop 1h 45m in in Lisbon; 6h 15m in Porto
arrival: Mon 8/20 10:25 PM
Return: Madrid to JFK [$392]
depature: Sun 8/26 8:00 AM
stops: 1 stop 7h 40 min in London
arrival: Sun 8/26 8:05 PM
Total: $655
Length: 7 days :: 08/19 - 08/26
Going: JFK to Madrid [$263] on TAP
departure: Sun 8/19 11:25 PM,
stops: 2 Stop 1h 45m in in Lisbon; 6h 15m in Porto
arrival: Mon 8/20 10:25 PM
Return: Barcelona to JFK [$392]
depature: Sun 8/26 9:50 AM
stops: 1 stop 6h min in London
arrival: Sun 8/26 8:05 PM
Total: $655
Length: 7 days :: 08/19 - 08/26
Going: JFK to Barcelona [$328] on TAP
departure: Sun 8/19 11:25 PM,
stops: 1 stop 7h 45m in Lisbon
arrival: Mon 8/20 7:20 PM
Return: Madrid to JFK [$392]
depature: Sun 8/26 8:00 AM
stops: 1 stop 7h 40 min in London
arrival: Sun 8/26 8:05 PM
Total: $720
Length: 7 days :: 08/19 - 08/26
Going: JFK to Barcelona [$328] on TAP
departure: Sun 8/19 11:25 PM,
stops: 1 stop 7h 45m in Lisbon
arrival: Mon 8/20 7:20 PM
Return: Barcelona to JFK [$392]
depature: Sun 8/26 9:50 AM
stops: 1 stop 6h min in London
arrival: Sun 8/26 8:05 PM
Total: $720
Length: 7 days :: 08/15 - 08/22
Going: JFK to Madrid [$298] on TAP
departure: Wed 8/15 11:25 PM,
stops: 2 stop 1h 45m in in Lisbon; 6h 15m in Porto
arrival: Thu 8/16 10:25 PM
Return: Madrid to JFK [$344]
depature: Wed 8/22 3:00 PM
stops: 1 Stop 1h in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $642
Length: 7 days :: 08/15 - 08/22
Going: JFK to Madrid [$298] on TAP
departure: Wed 8/15 11:25 PM,
stops: 2 stop 1h 45m in in Lisbon; 6h 15m in Porto
arrival: Thu 8/16 10:25 PM
Return: Barcelona to JFK [$360]
depature: Wed 8/22 12:30 PM
stops: 1 Stop 3h 40m in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $658
9 day trips
====================
Length: 9 days :: 08/15 - 08/24
Going: JFK to Madrid [$298] on TAP
departure: Wed 8/15 11:25 PM,
stops: 2 stop 1h 45m in in Lisbon; 6h 15m in Porto
arrival: Thu 8/16 10:25 PM
Return: Madrid to JFK [$366]
depature: Fri 8/24 10:20 AM
stops: 2 stops 2h in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $664
Length: 9 days :: 08/15 - 08/24
Going: JFK to Madrid [$298] on TAP
departure: Wed 8/15 11:25 PM,
stops: 2 stop 1h 45m in in Lisbon; 6h 15m in Porto
arrival: Thu 8/16 10:25 PM
Return: Barcelona to JFK [$365]
depature: Fri 8/24 10:00 AM
stops: 2 stops 1h 40m in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $663
Length: 9 days :: 08/13 - 08/22
Going: JFK to Seville [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 3:35 PM
Return: Madrid to JFK [$344]
depature: Wed 8/22 3:00 PM
stops: 1 Stop 1h in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $628
Length: 9 days :: 08/13 - 08/22
Going: JFK to Seville [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 3:35 PM
Return: Barcelona to JFK [$360]
depature: Wed 8/22 12:30 PM
stops: 1 Stop 3h 40m in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $644
Length: 9 days :: 08/13 - 08/22
Going: JFK to Madrid [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 9:20 PM
Return: Madrid to JFK [$344]
depature: Wed 8/22 3:00 PM
stops: 1 Stop 1h in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $628
Length: 9 days :: 08/13 - 08/22
Going: JFK to Madrid [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 9:20 PM
Return: Barcelona to JFK [$360]
depature: Wed 8/22 12:30 PM
stops: 1 Stop 3h 40m in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $644
Length: 9 days :: 08/13 - 08/22
Going: JFK to Barcelona [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 10h 15m in Lisbon
arrival: Wed 8/15 12:20 AM
Return: Madrid to JFK [$344]
depature: Wed 8/22 3:00 PM
stops: 1 Stop 1h in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $628
Length: 9 days :: 08/13 - 08/22
Going: JFK to Barcelona [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 10h 15m in Lisbon
arrival: Wed 8/15 12:20 AM
Return: Barcelona to JFK [$360]
depature: Wed 8/22 12:30 PM
stops: 1 Stop 3h 40m in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $644
Length: 9 days :: 08/13 - 08/22
Going: JFK to Barcelona [$329] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 5h 15m in Lisbon
arrival: Tue 8/14 7:20 PM
Return: Madrid to JFK [$344]
depature: Wed 8/22 3:00 PM
stops: 1 Stop 1h in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $673
Length: 9 days :: 08/13 - 08/22
Going: JFK to Barcelona [$329] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 5h 15m in Lisbon
arrival: Tue 8/14 7:20 PM
Return: Barcelona to JFK [$360]
depature: Wed 8/22 12:30 PM
stops: 1 Stop 3h 40m in Copenhagen
arrival: Wed 8/22 9:30 PM
Total: $689
11 day trips
====================
Length: 11 days :: 08/13 - 08/24
Going: JFK to Seville [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 3:35 PM
Return: Madrid to JFK [$366]
depature: Fri 8/24 10:20 AM
stops: 2 stops 2h in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $650
Length: 11 days :: 08/13 - 08/24
Going: JFK to Seville [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 3:35 PM
Return: Barcelona to JFK [$365]
depature: Fri 8/24 10:00 AM
stops: 2 stops 1h 40m in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $649
Length: 11 days :: 08/13 - 08/24
Going: JFK to Madrid [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 9:20 PM
Return: Madrid to JFK [$366]
depature: Fri 8/24 10:20 AM
stops: 2 stops 2h in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $650
Length: 11 days :: 08/13 - 08/24
Going: JFK to Madrid [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 9:20 PM
Return: Barcelona to JFK [$365]
depature: Fri 8/24 10:00 AM
stops: 2 stops 1h 40m in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $649
Length: 11 days :: 08/13 - 08/24
Going: JFK to Barcelona [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 10h 15m in Lisbon
arrival: Wed 8/15 12:20 AM
Return: Madrid to JFK [$366]
depature: Fri 8/24 10:20 AM
stops: 2 stops 2h in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $650
Length: 11 days :: 08/13 - 08/24
Going: JFK to Barcelona [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 10h 15m in Lisbon
arrival: Wed 8/15 12:20 AM
Return: Barcelona to JFK [$365]
depature: Fri 8/24 10:00 AM
stops: 2 stops 1h 40m in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $649
Length: 11 days :: 08/13 - 08/24
Going: JFK to Barcelona [$329] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 5h 15m in Lisbon
arrival: Tue 8/14 7:20 PM
Return: Madrid to JFK [$366]
depature: Fri 8/24 10:20 AM
stops: 2 stops 2h in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $695
Length: 11 days :: 08/13 - 08/24
Going: JFK to Barcelona [$329] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 5h 15m in Lisbon
arrival: Tue 8/14 7:20 PM
Return: Barcelona to JFK [$365]
depature: Fri 8/24 10:00 AM
stops: 2 stops 1h 40m in Porto; 3h 40m in Lisbon
arrival: Fri 8/24 8:00 PM
Total: $694
Length: 11 days :: 08/15 - 08/26
Going: JFK to Madrid [$298] on TAP
departure: Wed 8/15 11:25 PM,
stops: 2 stop 1h 45m in in Lisbon; 6h 15m in Porto
arrival: Thu 8/16 10:25 PM
Return: Madrid to JFK [$392]
depature: Sun 8/26 8:00 AM
stops: 1 stop 7h 40 min in London
arrival: Sun 8/26 8:05 PM
Total: $690
Length: 11 days :: 08/15 - 08/26
Going: JFK to Madrid [$298] on TAP
departure: Wed 8/15 11:25 PM,
stops: 2 stop 1h 45m in in Lisbon; 6h 15m in Porto
arrival: Thu 8/16 10:25 PM
Return: Barcelona to JFK [$392]
depature: Sun 8/26 9:50 AM
stops: 1 stop 6h min in London
arrival: Sun 8/26 8:05 PM
Total: $690
13 day trips
====================
Length: 13 days :: 08/13 - 08/26
Going: JFK to Seville [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 3:35 PM
Return: Madrid to JFK [$392]
depature: Sun 8/26 8:00 AM
stops: 1 stop 7h 40 min in London
arrival: Sun 8/26 8:05 PM
Total: $676
Length: 13 days :: 08/13 - 08/26
Going: JFK to Seville [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 3:35 PM
Return: Barcelona to JFK [$392]
depature: Sun 8/26 9:50 AM
stops: 1 stop 6h min in London
arrival: Sun 8/26 8:05 PM
Total: $676
Length: 13 days :: 08/13 - 08/26
Going: JFK to Madrid [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 9:20 PM
Return: Madrid to JFK [$392]
depature: Sun 8/26 8:00 AM
stops: 1 stop 7h 40 min in London
arrival: Sun 8/26 8:05 PM
Total: $676
Length: 13 days :: 08/13 - 08/26
Going: JFK to Madrid [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 7h in Lisbon
arrival: Tue 8/14 9:20 PM
Return: Barcelona to JFK [$392]
depature: Sun 8/26 9:50 AM
stops: 1 stop 6h min in London
arrival: Sun 8/26 8:05 PM
Total: $676
Length: 13 days :: 08/13 - 08/26
Going: JFK to Barcelona [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 10h 15m in Lisbon
arrival: Wed 8/15 12:20 AM
Return: Madrid to JFK [$392]
depature: Sun 8/26 8:00 AM
stops: 1 stop 7h 40 min in London
arrival: Sun 8/26 8:05 PM
Total: $676
Length: 13 days :: 08/13 - 08/26
Going: JFK to Barcelona [$284] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 10h 15m in Lisbon
arrival: Wed 8/15 12:20 AM
Return: Barcelona to JFK [$392]
depature: Sun 8/26 9:50 AM
stops: 1 stop 6h min in London
arrival: Sun 8/26 8:05 PM
Total: $676
Length: 13 days :: 08/13 - 08/26
Going: JFK to Barcelona [$329] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 5h 15m in Lisbon
arrival: Tue 8/14 7:20 PM
Return: Madrid to JFK [$392]
depature: Sun 8/26 8:00 AM
stops: 1 stop 7h 40 min in London
arrival: Sun 8/26 8:05 PM
Total: $721
Length: 13 days :: 08/13 - 08/26
Going: JFK to Barcelona [$329] on TAP
departure: Mon 8/13 11:25 PM,
stops: 1 stop 5h 15m in Lisbon
arrival: Tue 8/14 7:20 PM
Return: Barcelona to JFK [$392]
depature: Sun 8/26 9:50 AM
stops: 1 stop 6h min in London
arrival: Sun 8/26 8:05 PM
Total: $721
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment