Last active
April 1, 2023 03:31
-
-
Save qrno/664a3fa57f08c0fd412e3934981a2022 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 numpy as np | |
from time import sleep | |
import matplotlib.pyplot as plt | |
from requests import get | |
def get_color(rating): | |
if rating >= 2400: | |
return 'r' | |
elif rating >= 2100: | |
return 'y' | |
elif rating >= 1900: | |
return 'm' | |
elif rating >= 1600: | |
return 'b' | |
elif rating >= 1400: | |
return 'c' | |
elif rating >= 1200: | |
return 'g' | |
else: | |
return 'b' | |
class UserData: | |
def __init__(self, username): | |
j = get(f'https://codeforces.com/api/user.status?handle={username}&from=1&count=10000') | |
added = set() | |
timestamps = [] | |
solved_by_rating = {} | |
for i in range(0, 4000, 100): | |
solved_by_rating[i] = [0] | |
for sub in reversed(j.json()['result']): | |
if sub['verdict'] == 'OK' and 'rating' in sub['problem']: | |
problem = sub['problem'] | |
sub_time = sub['creationTimeSeconds'] | |
id = str(problem['contestId']) + problem['index'] | |
rating = problem['rating'] | |
if len(timestamps) == 0: | |
timestamps.append(sub_time-1) | |
if id in added: | |
continue | |
added.add(id) | |
timestamps.append(sub_time) | |
for r in solved_by_rating: | |
last = solved_by_rating[r][-1] | |
if rating == r: | |
solved_by_rating[r].append(last+1) | |
else: | |
solved_by_rating[r].append(last) | |
to_remove = [] | |
for r in solved_by_rating: | |
if solved_by_rating[r][-1] == 0 or r < 1600: | |
to_remove.append(r) | |
for r in to_remove: | |
solved_by_rating.pop(r) | |
self.username = username | |
self.ts = timestamps | |
self.v = list(reversed(solved_by_rating.values())) | |
self.l = list(reversed(solved_by_rating.keys())) | |
self.colors = [get_color(r) for r in self.l] | |
usernames = ['leoriether', 'albertotdneto', 'tiagodfs', 'cebolinha', 'eyz', 'vilsu'] | |
userdata = [UserData(user) for user in usernames] | |
fig, axs = plt.subplots(1, len(usernames)) | |
for i in range(1, len(axs)): | |
axs[0].get_shared_y_axes().join(axs[0], axs[i]) | |
axs[0].get_shared_x_axes().join(axs[0], axs[i]) | |
for i in range(len(usernames)): | |
user = userdata[i] | |
axs[i].stackplot(user.ts, user.v, labels=user.l, alpha=0.8, colors=user.colors) | |
axs[i].set_title(user.username) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment