Skip to content

Instantly share code, notes, and snippets.

@lukesteensen
Created October 17, 2012 01:38
Show Gist options
  • Save lukesteensen/3903248 to your computer and use it in GitHub Desktop.
Save lukesteensen/3903248 to your computer and use it in GitHub Desktop.
My solution to the Priceonomics Summer 2012 puzzle
import json
from itertools import groupby
from operator import itemgetter
def hour_func(x):
# strip off hours and minutes
return x['timestamp'][:-6]
raw_json = json.loads(open('readings_formatted.json', 'r').read())
# sort by location then by hour to enable grouping
inital_sorted = sorted(raw_json, key=hour_func)
data = sorted(inital_sorted, key=itemgetter('location'))
groups = {}
for k, g in groupby(data, itemgetter('location')):
group = {}
for kk, gg in groupby(g, hour_func):
temps = [x["temperature"] for x in gg]
group[kk] = sum(temps) / float(len(temps))
groups[k] = group
a = groups["NRT"]["2012-06-15 13"]
b = groups["IAD"]["2012-06-16 14"]
c = groups["SFO"]["2012-06-17 13"]
print a, b, c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment