Last active
October 25, 2019 18:51
-
-
Save meooow25/7ab1022fc11909ebc39b9e66cf1ad73b to your computer and use it in GitHub Desktop.
Solved plot for duck
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 argparse | |
import json | |
import urllib.request | |
from matplotlib import pyplot as plt | |
STATUS_URL_FMT = 'https://codeforces.com/api/user.status?handle={}' | |
CONTESTS_URL = 'https://codeforces.com/api/contest.list' | |
DEFAULT_LO, DEFAULT_HI = 500, 3800 | |
STEP = 100 | |
FIGSIZE = (12, 6) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('handle', help='CF handle') | |
parser.add_argument('-r', default=f'{DEFAULT_LO},{DEFAULT_HI}', help='rating range in the form of lo,hi') | |
args = parser.parse_args() | |
handle = args.handle | |
try: | |
lo, hi = map(int, args.r.split(',')) | |
except ValueError: | |
raise ValueError(f'Range argument must be of the form lo,hi') | |
status_resp = json.loads(urllib.request.urlopen(STATUS_URL_FMT.format(handle)).read()) | |
contests_resp = json.loads(urllib.request.urlopen(CONTESTS_URL.format(handle)).read()) | |
contest_map = {c['id']: c['startTimeSeconds'] for c in contests_resp['result']} | |
solved = set() | |
by_type = {'C': [], 'O': [], 'V': [], 'P': []} | |
for sub in sorted(status_resp['result'], key=lambda sub: sub['creationTimeSeconds']): | |
prob = sub['problem'] | |
typ = sub['author']['participantType'][0] | |
if not (sub['verdict'] == 'OK' and typ in by_type and prob.get('contestId') in contest_map | |
and 'rating' in prob and lo <= prob['rating'] <= hi): | |
continue | |
key = prob['name'], contest_map.get(prob['contestId']) | |
if key not in solved: | |
solved.add(key) | |
by_type[typ].append(prob) | |
all_ratings = [[prob['rating'] for prob in v] for v in by_type.values()] | |
nice_names = ['Contest: {}', 'Unofficial: {}', 'Virtual: {}', 'Practice: {}'] | |
labels = [name.format(len(ratings)) for name, ratings in zip(nice_names, all_ratings)] | |
total = sum(map(len, all_ratings)) | |
hist_bins = list(range(lo - STEP, hi + 2 * STEP, STEP)) | |
plt.figure(figsize=FIGSIZE) | |
plt.hist(all_ratings, stacked=True, bins=hist_bins, label=labels) | |
plt.xlabel('Problem rating') | |
plt.ylabel('Number solved') | |
plt.legend(title=f'Total: {total}', title_fontsize=plt.rcParams['legend.fontsize'], | |
loc='upper right') | |
plt.title(f'Rated problems solved by {handle} in range [{lo}, {hi}]') | |
plt.show() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment