-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstats.py
139 lines (97 loc) · 3.42 KB
/
stats.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from __future__ import division
from collections import defaultdict
import json
import math
from operator import itemgetter
import sys
def player_records(data):
"""Count the number of wins and losses for each player.
Args:
data: JSON representing the results of the competitions
Returns:
list of (player, (#wins, #losses, %wins)) pairs sorted descending by %wins
"""
counter = defaultdict(lambda: defaultdict(int))
for day in data:
for player, successful in day['results'].iteritems():
if successful:
counter[player]['wins'] += 1
else:
counter[player]['losses'] += 1
ret = [(player, counts['wins'], counts['losses'],
counts['wins'] / (counts['wins'] + counts['losses']))
for player, counts in counter.iteritems()]
def _key(thing):
return (thing[3], -thing[1] - thing[2])
ret.sort(key=_key, reverse=True)
return ret
def money_by_player(data, include_losers=False):
"""Count the amount of money each player has won.
Args:
data: JSON representing the results of the competitions
include_losers: whether players with 0 wins should be included
Returns: a list of (player, #wins) pairs sorted descending by #wins.
"""
counter = defaultdict(int)
money_in_the_pot = 0
for day in data:
money_in_the_pot += len(day['results'])
winners = [player for (player, success) in day['results'].iteritems()
if success]
if winners:
money_won = money_in_the_pot / len(winners)
for player in winners:
counter[player] += money_won
money_in_the_pot = 0
if include_losers:
for player in day['results'].iterkeys():
counter[player] += 0
return sorted(counter.iteritems(), key=itemgetter(1), reverse=True)
def players_per_game(data):
"""
Args:
data: JSON data representing results from games.
Returns: a 4-tuple (min, max, mean, std)
"""
nums = [len(day['results']) for day in data]
return (min(nums), max(nums), mean(nums), stdev(nums))
def current_pot(data):
current = 0
for day in data:
if any(day['results'].values()):
current = 0
else:
current += len(day['results'])
return current
def mean(xxs):
xs = list(xxs)
return sum(xs) / len(xs)
def stdev(xs):
mn = mean(xs)
return math.sqrt(mean((x - mn) ** 2 for x in xs))
def main(args):
if not args:
raise Exception('First arg must be path to data file.')
data_fp = args[0]
with open(data_fp, 'r') as f:
data = json.load(f)
print "#### Players' records ####"
for player, wins, losses, win_percentage in player_records(data):
print '* %d of %d (%.0f%%) %s' % \
(wins, wins + losses, win_percentage * 100, player.title())
print
print '#### Money by player ####'
for player, money_won in money_by_player(data):
print '* [$%.2f] %s' % (money_won, player.title())
print
print '#### Players per game ####'
mini, maxi, avg, std = players_per_game(data)
print '* Average: %.2f' % avg
print '* Std dev: %.2f' % std
print '* Minimum: %d' % mini
print '* Maximum: %d' % maxi
print
print '### Money in the pot ###'
print '* $%d' % current_pot(data)
if __name__ == '__main__':
main(sys.argv[1:])