-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbobby_s_code.py
More file actions
109 lines (71 loc) · 2.66 KB
/
bobby_s_code.py
File metadata and controls
109 lines (71 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
CHRIS_Q3_ANSWER = "00000000005923C3B356E155C7E450A488BBB0C4A6DF187820954E1C35436D61"
def main():
objs = read_parse_file('trunc_log.json')
print "1a: %f" % q1a(objs)
print "1a (3 decimal places only): %.3f" % q1a(objs)
print ""
print "1b: %f" % q1b(objs)
print ""
print "1c: %f" % q1c(objs)
# If a fn returns two values you can accept them into two different variables like this:
# If the fn only returns one value it will raise an exception
winning_board, count = q2(objs)
print "q2, board %d wins with %d shares" % (winning_board, count)
# If it returns two vars and you only accept 1, it returns them as a tuple (an immutable list)
print "shorthand print for q2: board %d wins with %d shares" % q2(objs)
print ""
original_hash, reversed_words_hash = q3(objs)
print "q3 original: %s" % original_hash
print "q3 reversed: %s" % reversed_words_hash
if CHRIS_Q3_ANSWER == reversed_words_hash:
print "Chris q3 was right!"
else:
print "Chris q3 was wrong!"
def read_parse_file(filename):
# read the whole file, and split it on newlines
list_of_strings = open(filename).read().split('\n')
"""
for each string in the list of strings do one of two things
if the string == '', then remove it from the list
else replace the string with json.loads(string)
The catch to remove '' is because there is a trailing newline, example:
>>> string = 'a b c '
>>> split(string)
['a', 'b', 'c', '']
"""
list_of_objects = [json.loads(string) for string in list_of_strings if string != '']
return list_of_objects
def get_timestamp_diff(obj):
return obj['time'] - int(obj['timestamp'], 16)
def q1a(objs):
return get_timestamp_diff(objs[0])
def q1b(objs):
# python treats array indices as if the array is wrapped around in a circle
# so, 0 is the first thing, 1 is the second, and -1 is the last
return get_timestamp_diff(objs[-1])
def q1c(objs):
total = 0
for obj in objs:
total += get_timestamp_diff(obj)
return total
def q2(objs):
board_counts = {}
for obj in objs:
bid = obj['board_id']
if bid in board_counts:
board_counts[bid] += 1
else:
board_counts[bid] = 1
winning_board = max(board_counts)
# returning two values:
return winning_board, board_counts[winning_board]
def q3(objs):
original_hash = objs[1]['hash']
reversed_words_hash = ''
for i in xrange(0, 8):
start = 64 - (i + 1) * 8
end = 64 - i * 8
reversed_words_hash += original_hash[start:end]
return original_hash, reversed_words_hash
main()