Skip to content

Commit

Permalink
Fix #18: Bug in fetching cases from Codechef
Browse files Browse the repository at this point in the history
  • Loading branch information
coderick14 committed Dec 17, 2019
1 parent 4b71961 commit 7b62aee
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions acedit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,50 +612,36 @@ def __init__(self, args):
self.problem = args['problem']
super(Codechef, self).__init__(args)

def _extract(self, data, marker):
data_low = data.lower()
extracts = []
idx = data_low.find(marker, 0)

while not idx == -1:
start = data_low.find('```', idx)
end = data_low.find('```', start + 3)
extracts += [data[start + 3:end]]
idx = data_low.find(marker, end)

return [extract.strip() for extract in extracts]

def parse_html(self, req):
"""
Method to parse the html and get test cases
from a codechef problem
"""
try:
data = json.loads(req.text)
soup = bs(data['body'], 'html.parser')
data = str(json.loads(req.text)['body'])
except (KeyError, ValueError):
print('Problem not found..')
Utilities.handle_kbd_interrupt(
self.site, self.contest, self.problem)
sys.exit(0)

test_cases = soup.findAll('pre')
formatted_inputs, formatted_outputs = [], []

input_list = [
'<pre>(.|\n)*<b>Input:?</b>:?',
'<b>Output:?</b>(.|\n)+</pre>'
]

output_list = [
'<pre>(.|\n)+<b>Output:?</b>:?',
'</pre>'
]

input_regex = re.compile('(%s)' % '|'.join(input_list))
output_regex = re.compile('(%s)' % '|'.join(output_list))

for case in test_cases:
inp = input_regex.sub('', str(case))
out = output_regex.sub('', str(case))

inp = re.sub('<[^<]+?>', '', inp)
out = re.sub('<[^<]+?>', '', out)
inputs = self._extract(data, 'example input')
outputs = self._extract(data, 'example output')

formatted_inputs += [inp.strip()]
formatted_outputs += [out.strip()]

# print 'Inputs', formatted_inputs
# print 'Outputs', formatted_outputs

return formatted_inputs, formatted_outputs
return inputs, outputs

def get_problem_links(self, req):
"""
Expand Down

0 comments on commit 7b62aee

Please sign in to comment.