Created
October 17, 2012 01:38
-
-
Save lukesteensen/3903248 to your computer and use it in GitHub Desktop.
My solution to the Priceonomics Summer 2012 puzzle
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
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