Skip to content

Commit afd857d

Browse files
committed
Merge branch 'problem_difficulty' of github.com:MipsaPatel/stopstalk-deployment
2 parents 3931926 + eabd2dd commit afd857d

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

models/db.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ def _count_custom_users_lambda(row):
599599
Field("total_submissions", "integer", default=0),
600600
Field("user_ids", "text", default=""),
601601
Field("custom_user_ids", "text", default=""),
602+
Field("difficulty", "float"),
602603
Field.Virtual("user_count", _count_users_lambda),
603604
Field.Virtual("custom_user_count", _count_custom_users_lambda),
604605
format="%(name)s %(id)s")
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""
2+
Copyright (c) 2015-2020 Raj Patel(raj454raj@gmail.com), StopStalk
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
"""
22+
23+
from gluon import current
24+
from collections import defaultdict
25+
import time
26+
27+
db = current.db
28+
29+
pdtable = db.problem_difficulty
30+
ptable = db.problem
31+
sttable = db.suggested_tags
32+
ttable = db.tag
33+
34+
codeforces_difficulty = {
35+
"A": 1,
36+
"B": 2,
37+
"C": 3,
38+
"D": 4,
39+
"E": 5
40+
}
41+
42+
tag_difficulty = {
43+
"Ad-hoc": 1,
44+
"Easy": 2,
45+
"Medium": 3,
46+
"Hard": 4.5
47+
}
48+
49+
tags = tag_difficulty.keys()
50+
51+
difficulty = defaultdict(list)
52+
53+
# Compute problem difficulty for CodeForces problems.
54+
query = (ptable.link.contains("codeforces")) & \
55+
~(ptable.link.contains("gymProblem"))
56+
rows = db(query).select(ptable.id, ptable.link)
57+
58+
for row in rows:
59+
tag = row.link.split("/")[-1][0]
60+
pid = row.id
61+
62+
if tag in codeforces_difficulty:
63+
difficulty[pid].append(codeforces_difficulty[tag])
64+
65+
# Problems with tags easy, medium, difficult etc.
66+
query = (ttable.value.belongs(tags)) & (ttable.id == sttable.tag_id)
67+
rows = db(query).select(sttable.problem_id, ttable.value)
68+
for row in rows:
69+
pid = row.suggested_tags.problem_id
70+
tag = row.tag.value
71+
72+
difficulty[pid].append(tag_difficulty[tag])
73+
74+
# User suggested problem difficulty.
75+
rows = db(pdtable).select(pdtable.problem_id, distinct=True)
76+
pids = [str(x.problem_id) for x in rows]
77+
78+
sql_query = """
79+
SELECT problem_id, avg(score)
80+
FROM problem_difficulty
81+
WHERE problem_id IN (%(pids)s)
82+
GROUP BY problem_id
83+
HAVING count(*) >= 3
84+
""" % ({"pids": ",".join(pids)})
85+
86+
res = db.executesql(sql_query)
87+
for pid, score in res:
88+
difficulty[pid].append(float(score))
89+
90+
# Compute average difficulty from all types of problem difficulties.
91+
write_count = 0
92+
for pid, difficulties in difficulty.items():
93+
if write_count >= 100:
94+
write_count = 0
95+
time.sleep(0.5)
96+
97+
write_count += 1
98+
avg_difficulty = sum(difficulties) / float(len(difficulties))
99+
update = ptable(pid).update_record(difficulty=avg_difficulty)

0 commit comments

Comments
 (0)