-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_problem.py
308 lines (236 loc) · 10.5 KB
/
get_problem.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import requests
from requests import Response
import json, os, errno, sys
import re, fire, datetime
from random import choice
from bs4 import BeautifulSoup, Tag, ResultSet, NavigableString
from shutil import copyfile
from typing import List, Dict, Optional
def fix_html_notation(html_tag: Tag) -> str:
# Transform html tag to pretty-print string
html_text: str = html_tag.prettify()
# Transform $$$ bold notation to html bold notation
html_text = re.sub(r"\$\$\$([^$]+)\$\$\$", r"<strong>\1</strong>", html_text) + "\n"
# Transform le/leq notation to html less or equal notation
html_text = re.sub(r" (\\le|\\leq) ", r" <= ", html_text)
# Transform ge/geq notation to html greater or equal notation
html_text = re.sub(r" (\\ge|\\geq) ", r" >= ", html_text)
return html_text
def update() -> Dict:
problems_endpoint = "https://codeforces.com/api/problemset.problems"
try:
res = requests.get(problems_endpoint)
except Exception as e:
print("Error with request to codeforce's api.")
print(f"Message:", e.args[0])
sys.exit()
if int(res.status_code) != 200:
print(f"Request to {problems_endpoint} failed")
print(f"Status code: {res.status_code}")
print(f"Reason: {res.reason}")
sys.exit()
jres = json.loads(res.text)
problems = jres["result"]["problems"]
problems_dict = {
"date": datetime.datetime.today().strftime('%d-%m-%Y'),
"problems": problems
}
with open("problems.json", "w") as file:
file.write(json.dumps(problems_dict))
return problems_dict
def fetch_problems() -> List:
try:
with open("problems.json", "r") as file:
problems = json.load(file)
except FileNotFoundError:
problems = update()
today = datetime.datetime.today()
problems_date = datetime.datetime.strptime(problems["date"], "%d-%m-%Y")
if today - problems_date > datetime.timedelta(days=7):
problems = update()
return problems["problems"]
def filter_problems(
problems: List,
min_rating: Optional[int] = None,
max_rating: Optional[int] = None
) -> List[Dict]:
"""
Returns a list of problems between the specified rating
Args:
problems: Complete list of problems
min_rating: The minimum difficulty rate
max_rating: The maximum difficulty rate
Returns:
desired: List with the filtered problems
"""
desired = []
for p in problems:
# IS IT RATED???
if "rating" in p:
if min_rating:
if max_rating:
if min_rating <= p["rating"] <= max_rating:
# Add problem if is between specified range
desired.append(p)
else:
# Add problem if only min is specified
if min_rating <= p["rating"]:
desired.append(p)
else:
if max_rating:
# Add problem if only min is specified
if p["rating"] <= max_rating:
desired.append(p)
else:
# Add problem it's not rated and there is no rating limit
if not (min_rating or max_rating):
desired.append(p)
return desired
def create_problem_directory_and_files(title_underline: str) -> None:
try:
md_filename = f"./{title_underline}/{title_underline}.md"
os.makedirs(os.path.dirname(md_filename))
open(f"{title_underline}/__init__.py", 'a').close()
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
def choose_new_problem(desired: List) -> Dict:
"""
Selects a random problem from the desired list
Args:
desired: List of the desired problems
Returns:
new_problem: The chosen problem
"""
while True:
# Randomly chooses a new problem
new_problem = choice(desired)
title = new_problem["name"]
title_underline = title.replace(" ", "_")
# Create the problem directory and returns the problem
# if its a new problem (directory with the problem's name doesn't exist)
if not os.path.exists(f"./{title_underline}"):
create_problem_directory_and_files(title_underline)
return new_problem
def main(
min_rating: int = 700,
max_rating: int = 900,
problem_url: Optional[str] = None
) -> None:
if not problem_url:
problems = fetch_problems()
desired = filter_problems(problems, min_rating, max_rating)
new_problem = choose_new_problem(desired)
c_id = new_problem["contestId"]
c_ind = new_problem["index"]
rating = new_problem["rating"]
problem_url = f"https://codeforces.com/problemset/problem/{c_id}/{c_ind}"
else:
divided_url = problem_url.split("/")
c_id = divided_url[-2]
c_ind = divided_url[-1]
rating = "unknown"
# Fetch problem page
response: Response = requests.get(problem_url)
soup = BeautifulSoup(response.content, "html.parser")
problem_statement: Tag = soup.find("div", class_="problem-statement")
header: Tag = problem_statement.find("div", class_="header")
# Find title html tag
title_tag = header.find("div", class_="title")
# Removes index from title
raw_title = title_tag.text[3:]
title_underline = raw_title.replace(" ", "_")
md_filename = f"./{title_underline}/{title_underline}.md"
create_problem_directory_and_files(title_underline)
time_limit = header.find("div", class_="time-limit").next_element
time_limit_title = time_limit.next_element
time_limit_text: str = time_limit_title.next_element.split(" ")
time_limit_value = int(time_limit_text[0])
memory_limit = header.find("div", class_="memory-limit")
memory_limit_title = memory_limit.contents[1].text
memory_limit_value = memory_limit.contents[2].string
input_file = header.find("div", class_="input-file")
input_file_title = input_file.contents[1].text
input_file_value = input_file.contents[2].string
output_file = header.find("div", class_="output-file")
output_file_title = output_file.contents[1].text
output_file_value = output_file.contents[2].string
description_div: Tag = header.next_sibling
while isinstance(description_div, NavigableString):
description_div = description_div.next_sibling
description = fix_html_notation(description_div)
input_specification_div: Tag = soup.find("div", class_="input-specification")
input_title_div: Tag = input_specification_div.find("div", class_="section-title")
input_title: str = input_title_div.text
input_title_div.decompose()
input_specification = f"\n### {input_title}\n\n"
input_specification += fix_html_notation(input_specification_div)
output_specification_div = soup.find("div", class_="output-specification")
output_title_div = output_specification_div.find("div", class_="section-title")
output_title: str = output_title_div.text
output_title_div.decompose()
output_specification = f"\n### {output_title}\n\n"
output_specification += fix_html_notation(output_specification_div)
sample_tests_div: Tag = soup.find("div", class_="sample-tests")
sample_tests_title_div = sample_tests_div.find(class_="section-title")
sample_tests_title = sample_tests_title_div.text
sample_tests_title_div.decompose()
sample_tests = f"\n## {sample_tests_title}\n\n"
sample_tests += fix_html_notation(sample_tests_div)
sample_tests_elements: Tag = sample_tests_div.find(class_="sample-test")
input_elements: ResultSet = sample_tests_elements.find(class_="input").find_all("pre")
output_elements: ResultSet = sample_tests_elements.find(class_="output").find_all("pre")
file_tests_input = ""
file_tests_output = ""
number_of_examples = 0
copyfile("./test_template_solution.py", f"./{title_underline}/test_solution.py")
with open(f"./{title_underline}/test_solution.py", "a+") as file:
for input_child, output_child in zip(input_elements, output_elements):
input_value = ""
for v in input_child.children:
if v.name == "br":
continue
else:
file_tests_input += v.strip() + "\n"
input_value += re.sub(r"\n", r"\\n", v.strip() + "\n")
output_value = ""
for v in output_child.children:
if v.name == "br":
continue
else:
file_tests_output += v.strip() + "\n"
output_value += re.sub(r"\n", r"\\n", v.strip() + "\n")
file.write(
f'\n\[email protected]({time_limit_value})\ndef test_solve_{number_of_examples}():\n\tassert solve("{input_value}") == "{output_value}"\n'
)
note_elem_div: Tag = soup.find("div", class_="note")
note_elem: str = ""
if note_elem_div:
note_elem_title_div: Tag = note_elem_div.find("div", class_="section-title")
note_elem_title = note_elem_title_div.text
note_elem_title_div.decompose()
note_elem = f"\n### {note_elem_title}\n\n "
note_elem += fix_html_notation(note_elem_div)
with open(md_filename, "w") as file:
file.write(f"# [{raw_title} #{c_id} - {c_ind}]({problem_url})\n")
file.write(f"### Rating: {rating}\n")
file.write(f"\n{memory_limit_title}: {memory_limit_value}\n")
file.write(f"\n{input_file_title}: {input_file_value}\n")
file.write(f"\n{output_file_title}: {output_file_value}\n")
file.write(f"##Description {description} \n")
file.write(input_specification)
file.write(output_specification)
file.write(sample_tests)
file.write(note_elem)
with open(f"./{title_underline}/input.txt", "w") as file:
file.write(file_tests_input)
with open(f"./{title_underline}/expected_output.txt", "w") as file:
file.write(file_tests_output)
with open(f"./{title_underline}/solution_output.txt", "w") as file:
# Creates an empty solution output file
# (this helps when configuring pycharm to redirect output)
file.write("")
copyfile("./template_solution.py", f"./{title_underline}/solution.py")
print(f"New problem on ./{title_underline}")
if __name__ == "__main__":
fire.Fire(main)