-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfrate.py
233 lines (192 loc) · 6.67 KB
/
cfrate.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import json
import requests
import sys
import time
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
is_plot = False
manual_input = True
n = len(sys.argv)
if n > 1:
handles = []
for arg in sys.argv[1:]:
if(arg == "-g"):
is_plot = True
else:
manual_input = False
handles.append(arg)
try:
if(n > 1 and len(handles) > 1):
is_plot = False
except:
pass
try:
import matplotlib.pyplot as plt
except:
is_plot = False
#YOU CAN CHANGE THAT VALUE
Border_of_low_rating = 1100 #All problems under that value are defined as low rating problems
##########################
def getInfoByResponse(response):
try:
response = requests.get(response)
except:
sys.stderr.write("---------------------------------\n")
sys.stderr.write("ERROR:\n")
sys.stderr.write("Something went wrong when we tried to get data from codeforces! No responce!\n")
sys.stderr.write("---------------------------------\n")
return
data = json.loads(response.text)
return data
def checkData(data):
if data["status"] != "OK":
try:
sys.stderr.write("---------------------------------\n")
sys.stderr.write("ERROR:\n")
sys.stderr.write("Something went wrong when we tried to get data from codeforces! The end data is wrong!\n")
sys.stderr.write("Codeforces comment about error is:", data["comment"],"\n")
sys.stderr.write("---------------------------------\n")
return
except:
sys.stderr.write("Something went wrong with getting codeforces comment\n")
sys.stderr.write("---------------------------------\n")
return
def fancyOutput(message, first, second):
numberForPerc = first / second
percentage = "{:.1%}".format(numberForPerc)
print(bcolors.BOLD + message + bcolors.ENDC + str(first) + " = " + bcolors.OKBLUE + str(first) + bcolors.ENDC + "/" + bcolors.OKGREEN + str(second) + bcolors.WARNING + " (" + str(percentage) + ")" + bcolors.ENDC)
def fancyOutputAdv(message, first, second, third):
numberForPerc = second / third
percentage = "{:.1%}".format(numberForPerc)
print(bcolors.BOLD + message + bcolors.ENDC + str(first) + " = " + bcolors.OKBLUE + str(second) + bcolors.ENDC + "/" + bcolors.OKGREEN + str(third) + bcolors.WARNING + " (" + str(percentage) + ")" + bcolors.ENDC)
def Process_handle(handle_num=0):
if(manual_input):
print("Write your handle: ", end="")
handle = input()
else:
handle = handles[handle_num]
data = getInfoByResponse("https://codeforces.com/api/user.status?handle=" + handle)
names = {}
high_rates = {}
low_rates = {}
tags = {}
tagsAll = {}
ratingsAll = {}
total = 0;
total_low = 0
total_high = 0
checkData(data)
for t in data["result"]:
if(t["verdict"] != "OK"):
continue
try:
rate = t["problem"]["rating"]
except KeyError:
continue
key = str(t["problem"]["contestId"]) + t["problem"]["index"]
if key in names:
continue
else:
names[key] = 1
for tag in t["problem"]["tags"]:
if tag in tags:
tags[tag] += 1
else:
tags[tag] = 1
high_rate = True
if(rate < Border_of_low_rating):
high_rate = False
if(high_rate):
total_high += 1
if rate in high_rates:
high_rates[rate] += 1
else:
high_rates[rate] = 1
else:
total_low += 1
if rate in low_rates:
low_rates[rate] += 1
else:
low_rates[rate] = 1
total += 1
data = getInfoByResponse("https://codeforces.com/api/problemset.problems")
checkData(data)
for t in data["result"]["problems"]:
try:
rate = t["rating"]
for tag in t["tags"]:
if tag in tagsAll:
tagsAll[tag] += 1
else:
tagsAll[tag] = 1
if t["rating"] in ratingsAll:
ratingsAll[t["rating"]] += 1
else:
ratingsAll[t["rating"]] = 1
except:
pass
totalLowFromAll = 0
totalHighFromAll = 0
for k, v in sorted(ratingsAll.items(), reverse=True):
if(int(k) < Border_of_low_rating):
totalLowFromAll += v
else:
totalHighFromAll += v
#Outputing data
print("Information about", handle)
numberForTotal = numberForPerc = total / (totalLowFromAll + totalHighFromAll)
percentageTotal = "{:.1%}".format(numberForTotal)
fancyOutput("Total: ", total, totalLowFromAll + totalHighFromAll)
print("--------------------")
numberForTotal = numberForPerc = total_high / totalHighFromAll
percentageTotal = "{:.1%}".format(numberForTotal)
fancyOutput("Total high ratings: ", total_high, totalHighFromAll)
for k, v in sorted(high_rates.items(), reverse=True):
fancyOutputAdv("Rating: ", k, v, ratingsAll[k])
print("--------------------")
for k, v in sorted(low_rates.items(), reverse=True):
fancyOutputAdv("Rating: ", k, v, ratingsAll[k])
numberForTotal = numberForPerc = total_low / totalLowFromAll
percentageTotal = "{:.1%}".format(numberForTotal)
fancyOutput("Total low ratings: ", total_low, totalLowFromAll)
print("--------------------")
print()
print("By tags:")
for k, v in sorted(tagsAll.items(), reverse=True):
tagSolved = 0
try:
tagSolved = tags[k]
except:
tagSolved = 0
fancyOutputAdv("--: ", k, tagSolved, tagsAll[k])
if is_plot:
data_for_plot = sorted(list(high_rates.items()) + list(low_rates.items()), reverse=True)
x = []
y = []
for pair in data_for_plot:
x.append(pair[0])
y.append(pair[1])
plt.plot(x, y)
plt.xlabel('x - ratings of problems')
plt.ylabel('y - number of solved problems')
plt.title('Codeforces graph for ' + handle)
plt.show()
handle_num = 0
if(__name__ == "__main__"):
print("[[rating < ", Border_of_low_rating, " is low rate]]", sep="")
print("[[Problems without rating are not counted]]")
print()
if(manual_input):
Process_handle()
else:
for i in range(0, len(handles)):
time.sleep(0.5)
Process_handle(i)