-
Notifications
You must be signed in to change notification settings - Fork 3
/
stats.py
198 lines (150 loc) · 4.35 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import datetime
import gspread
import pandas as pd
from constants import BASE_ED_DATE
from gsheet import SHEET_URL
def get_scores_df():
"""Return scores dataframe from Google Sheets or local csv file.
Returns
-------
pandas.DataFrame
"""
if SHEET_URL:
gc = gspread.service_account(filename="service_account.json")
sh = gc.open_by_url(SHEET_URL)
df = pd.DataFrame(sh.sheet1.get_all_records())
else:
df = pd.read_csv("scores.csv")
return df
def calculate_points(x):
"""Return points equivalent of a Wordle score.
The following table shows how much points a Wordle score is worth.
| score | points |
|:-----:|:------:|
| 1/6 | 6 |
| 2/6 | 5 |
| 3/6 | 4 |
| 4/6 | 3 |
| 5/6 | 2 |
| 6/6 | 1 |
| X/6 | 0.5 |
Parameters
----------
x : str
Wordle score (e.g., "4/6")
Returns
-------
int or float
"""
try:
p = abs(int(x[0]) - 6) + 1
except ValueError:
p = 0.5
return p
def convert_points_to_wordle_score(p):
"""Return Wordle score equivalent of points.
If the given points is not within 1 through 6 or 0.5, None is returned.
Parameters
----------
p : int or float
Points to be converted to Wordle score
Returns
-------
str or None
Wordle score (e.g., "4/6")
"""
x = None
if p in range(1, 7):
x = f"{str(abs(int(p) - 6) + 1)}/6"
elif p == 0.5:
x = "X/6"
return x
def get_total_points(df):
"""Calculate the total points of each user.
Parameters
----------
df : pandas.DataFrame
Dataframe of Wordle scores
Returns
-------
pandas.DataFrame
"""
df["points"] = df["score"].apply(lambda x: calculate_points(x))
agg = df.groupby("username").agg(
points=("points", "sum"), count=("username", "count")
)
return agg
def get_top_n_users(df, n=5):
"""Return the top n users from the scores.
If there are ties at the nth position, users with the same score as the nth user
will be included.
Parameters
----------
df : pandas.DataFrame
Dataframe of Wordle scores
n : int, default 5
Number of positions to include
Returns
-------
pandas.DataFrame
"""
top_df = get_total_points(df).sort_values("points", ascending=False)
# Handle ties
if len(top_df) > n:
p_bound = top_df["points"].iloc[n - 1]
top_df = top_df[top_df["points"] >= p_bound]
return top_df
def _get_recap_df(df):
"""Get top users and return Wordle scores.
Parameters
----------
df : pandas.DataFrame
Dataframe of Wordle scores of a Wordle edition
Returns
-------
pandas.DataFrame
"""
df = get_top_n_users(df)
# Filter out X/6
df = df[df["points"] != 0.5].copy()
df["score"] = df["points"].apply(lambda x: convert_points_to_wordle_score(x))
return df[["score"]]
def get_recap():
"""Get leaderboards of the last two editions.
Returns
-------
tuple of (int, pandas.DataFrame, int, pandas.DataFrame)
Corresponds to (latest-1) edition, (latest-1) leaderboard, latest edition,
latest leaderboard
"""
df = get_scores_df()
latest = df["wordle"].drop_duplicates().nlargest(2).values
ed1 = latest[1]
ed2 = latest[0]
lb1 = _get_recap_df(df[df["wordle"] == ed1].copy())
lb2 = _get_recap_df(df[df["wordle"] == ed2].copy())
return ed1, lb1, ed2, lb2
def resolve_weekly_edition():
"""Return starting and ending editions for the past week.
Returns
-------
tuple of (int, int)
Corresponds to beginning edition, ending edition
"""
yday = datetime.datetime.utcnow().date() - datetime.timedelta(days=1)
ed2 = BASE_ED_DATE[0] + (yday - BASE_ED_DATE[1]).days
ed1 = ed2 - 6
return ed1, ed2
def get_weekly():
"""Get leaderboard of the past week.
Included editions start from yesterday and 6 days prior.
Returns
-------
tuple of (int, int, pandas.DataFrame)
Corresponds to starting edition, ending edition, leaderboard
"""
ed1, ed2 = resolve_weekly_edition()
df = get_scores_df()
df = df[df["wordle"].between(ed1, ed2)].copy()
lb = get_total_points(df).sort_values("points", ascending=False)
return ed1, ed2, lb