Skip to content

Commit 5704006

Browse files
committed
feat: Add function to get info for a problem
1 parent 0147ef1 commit 5704006

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

codeforces/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from . import error
22
from . import api
3+
from . import problem

codeforces/problem.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import urllib.request
3+
import urllib.error
4+
from bs4 import BeautifulSoup
5+
6+
__all__ = ['get_info']
7+
8+
CODEFORCES_URL = "https://codeforces.com"
9+
10+
11+
def get_info(contest_id, index, lang='en'):
12+
"""
13+
Get info for a contest problem.
14+
15+
Parameters
16+
----------
17+
contest_id: int
18+
Id of the contest. It is not the round number. It can be seen in
19+
contest URL. For example: /contest/**566**/status
20+
index: str
21+
Usually a letter of a letter, followed by a digit, that
22+
represent a problem index in a contest. It can be seen in
23+
problem URL. For example: /contest/566/**A**
24+
25+
Returns
26+
-------
27+
title: str
28+
Title of the problem.
29+
time_limit: str
30+
Time limit specification for the problem.
31+
memory_limit: str
32+
Memory limit specification for the problem.
33+
sample_tests: zip
34+
Sample tests given for the problem.
35+
"""
36+
37+
problem_url = os.path.join(
38+
CODEFORCES_URL,
39+
"contest/%d/problem/%s?lang=%s" % (contest_id, index, lang)
40+
)
41+
42+
with urllib.request.urlopen(problem_url) as res:
43+
soup = BeautifulSoup(res.read(), 'html.parser')
44+
45+
title = soup.find_all("div", class_="title")[0].text
46+
47+
time_limit = soup.find_all("div", class_="time-limit")[0].text[19:]
48+
memory_limit = soup.find_all("div", class_="memory-limit")[0].text[21:]
49+
50+
i = [i.pre.string[1:] for i in soup.find_all("div", class_="input")]
51+
o = [i.pre.string[1:] for i in soup.find_all("div", class_="output")]
52+
53+
sample_tests = zip(i, o)
54+
55+
return (title, time_limit, memory_limit, sample_tests)

0 commit comments

Comments
 (0)