-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_problem.py
executable file
·74 lines (65 loc) · 1.75 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
#!/usr/bin/python3
from urllib.request import urlopen
from sys import argv
from html.parser import HTMLParser
# This denotes the end of a test case, mandatory after every test case
BREAK = "END OF TESTCASE\n"
class problem_parser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.reading = False
self.input = []
self.output = []
self.buffer = ""
self.xr = 0
def handle_starttag(self, tag, attrs):
if tag == "pre":
self.xr ^= 1
self.reading = True
def handle_endtag(self, tag):
if tag == "pre":
self.reading = False
if self.xr == 1:
self.input.append(self.buffer)
else:
self.output.append(self.buffer)
self.buffer = ""
def handle_data(self, data):
if self.reading:
self.buffer += data + '\n'
#This is contest no given in the url of the problem
problem_no = argv[1]
#This is the problem no (A, B, C, D, E..)
problem_letter = argv[2]
#This is the URL of the problem
url = 'https://codeforces.com/problemset/problem/' + problem_no + '/' + problem_letter
problem = urlopen(url)
flag = 0
if problem.geturl() != url:
flag = 1
print("URL RELATED ERROR\n")
else:
print("Fetching Problem", problem_no, problem_letter)
text = problem.read()
parser = problem_parser()
parser.feed(text.decode('utf-8'))
if len(parser.input) != len(parser.output):
flag = 1
print("Error in Parsing the problem")
else:
inp = open('prac.txt', 'w')
inp.write(str(len(parser.input)) + '\n')
for test_case in parser.input:
inp.write(test_case)
inp.write(BREAK)
inp.close()
out = open('prac.out', 'w')
out.write(str(len(parser.output)) + '\n')
for test_case in parser.output:
out.write(test_case)
out.write(BREAK)
out.close()
if flag == 0:
print("Fetch successful")
else:
print("Fetch unsuccessful")