/
main.py
99 lines (65 loc) · 2.58 KB
/
main.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
#!/usr/bin/env python3
"""
In this example we are loading only unsolved Div2.C problems
"""
from collections import defaultdict
from itertools import groupby
import os
import sys
from codeforces import CodeforcesAPI
from codeforces import VerdictType
from codeforces import Problem
from codeforces import Contest
def first_or_default(lst, f):
return next(filter(f, lst), None)
def get_contest_id(x):
assert isinstance(x, (Problem, Contest))
if isinstance(x, Problem):
return x.contest_id
else:
return x.id
def group_by_contest_id(iterable):
res = defaultdict(list)
for k, vs in groupby(iterable, get_contest_id):
res[k].extend(vs)
return res
def make_url(problem):
return 'http://codeforces.com/contest/{}/problem/{}'.format(problem.contest_id, problem.index)
def filter_div2(iterable):
return filter(lambda contest: 'Div. 2' in contest.name, iterable)
def filter_c(iterable):
return filter(lambda problem: 'C' in problem.index, iterable)
def filter_accepted(iterable):
return filter(lambda submission: submission.verdict is not None and submission.verdict == VerdictType.ok, iterable)
def main(argv):
assert len(argv) == 2
api = CodeforcesAPI()
print('Loading your submissions')
handle = argv[1]
submissions = filter_accepted(api.user_status(handle))
solved_problems = filter_c(submission.problem for submission in submissions)
solved_problems = set(solved_problems)
print('Loaded {} solved C problems'.format(len(solved_problems)))
print('Loading contests...')
contests = group_by_contest_id(filter_div2(api.contest_list()))
print('Loaded {} Div.2 contests'.format(len(contests)))
print('Loading problemset...')
problemset = api.problemset_problems()
problems = group_by_contest_id(filter_c(problemset['problems']))
stats = problemset['problemStatistics']
stats = filter_c(stats)
stats = filter(lambda s: s.contest_id in contests, stats)
stats = filter(lambda s: problems[s.contest_id][0] not in solved_problems, stats)
stats = sorted(stats, key=lambda s: s.solved_count, reverse=True)
print()
print('{:30}{:15}{}'.format('Name', 'Solved count', 'Url'))
for stat in stats[:10]:
problem = problems[stat.contest_id][0]
print('{:30}{:<15}{}'.format(problem.name, stat.solved_count, make_url(problem)))
if __name__ == '__main__':
if len(sys.argv) == 2:
main(sys.argv)
else:
print('Invalid number of arguments')
print('Usage: python3 {} [user handle]'.format(os.path.basename(sys.argv[0])))
sys.exit(1)