|
| 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