Skip to content

Commit 7b62aee

Browse files
committed
Fix #18: Bug in fetching cases from Codechef
1 parent 4b71961 commit 7b62aee

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

acedit/util.py

+17-31
Original file line numberDiff line numberDiff line change
@@ -612,50 +612,36 @@ def __init__(self, args):
612612
self.problem = args['problem']
613613
super(Codechef, self).__init__(args)
614614

615+
def _extract(self, data, marker):
616+
data_low = data.lower()
617+
extracts = []
618+
idx = data_low.find(marker, 0)
619+
620+
while not idx == -1:
621+
start = data_low.find('```', idx)
622+
end = data_low.find('```', start + 3)
623+
extracts += [data[start + 3:end]]
624+
idx = data_low.find(marker, end)
625+
626+
return [extract.strip() for extract in extracts]
627+
615628
def parse_html(self, req):
616629
"""
617630
Method to parse the html and get test cases
618631
from a codechef problem
619632
"""
620633
try:
621-
data = json.loads(req.text)
622-
soup = bs(data['body'], 'html.parser')
634+
data = str(json.loads(req.text)['body'])
623635
except (KeyError, ValueError):
624636
print('Problem not found..')
625637
Utilities.handle_kbd_interrupt(
626638
self.site, self.contest, self.problem)
627639
sys.exit(0)
628640

629-
test_cases = soup.findAll('pre')
630-
formatted_inputs, formatted_outputs = [], []
631-
632-
input_list = [
633-
'<pre>(.|\n)*<b>Input:?</b>:?',
634-
'<b>Output:?</b>(.|\n)+</pre>'
635-
]
636-
637-
output_list = [
638-
'<pre>(.|\n)+<b>Output:?</b>:?',
639-
'</pre>'
640-
]
641-
642-
input_regex = re.compile('(%s)' % '|'.join(input_list))
643-
output_regex = re.compile('(%s)' % '|'.join(output_list))
644-
645-
for case in test_cases:
646-
inp = input_regex.sub('', str(case))
647-
out = output_regex.sub('', str(case))
648-
649-
inp = re.sub('<[^<]+?>', '', inp)
650-
out = re.sub('<[^<]+?>', '', out)
641+
inputs = self._extract(data, 'example input')
642+
outputs = self._extract(data, 'example output')
651643

652-
formatted_inputs += [inp.strip()]
653-
formatted_outputs += [out.strip()]
654-
655-
# print 'Inputs', formatted_inputs
656-
# print 'Outputs', formatted_outputs
657-
658-
return formatted_inputs, formatted_outputs
644+
return inputs, outputs
659645

660646
def get_problem_links(self, req):
661647
"""

0 commit comments

Comments
 (0)