Skip to content

Instantly share code, notes, and snippets.

@hoffmanc
Created May 6, 2010 20:42
Show Gist options
  • Save hoffmanc/392680 to your computer and use it in GitHub Desktop.
Save hoffmanc/392680 to your computer and use it in GitHub Desktop.
from itertools import permutations, imap, product
from datetime import datetime, timedelta
from random import random, choice
import string
basetime = datetime(2010,5,5)
airports = map(lambda a: ''.join(a), permutations(string.ascii_uppercase, 3))
flights = []
for i in xrange(1,500000):
td = timedelta(
days = random() * 10 % 7,
seconds = random() * 100000 % 86399
)
flights.append({
'id': i,
'departs': basetime + td,
'arrives': basetime + td + timedelta(seconds=3600),
'origin': choice(airports),
'dest': choice(airports),
})
def validitinerary(tpl):
(x,y) = tpl
i = {'seg1': x,
'seg2': y,
'cnt': 0}
if(x['dest'] == y['origin'] and
(y['departs'] - x['arrives']).seconds in
range(6000,30000)
): i['cnt'] = 1
return i
itineraries = imap(validitinerary, product(flights, flights))
itinerarycnts = {}
for i in itineraries:
key = (i['seg1']['origin'],i['seg2']['dest'])
if key in itinerarycnts:
itinerarycnts[key] += i['cnt']
else:
itinerarycnts[key] = i['cnt']
print itinerarycnts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment