Skip to content

Commit 4760d9c

Browse files
committed
Fix no-contest bug for SPOJ
1 parent 44abbf9 commit 4760d9c

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

ACedIt/util.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ def create_workdir_structure(site, contest):
130130
"""
131131
Method to create the working directory structure
132132
"""
133+
134+
# No need to create directories for SPOJ as it does not have contests
135+
if site == 'spoj':
136+
return
137+
133138
try:
134139
with open(os.path.join(Utilities.cache_dir, 'constants.json'), 'r') as f:
135140
data = f.read()
@@ -157,6 +162,9 @@ def check_cache(site, contest, problem):
157162
contest))
158163
return False
159164

165+
# Handle case for SPOJ specially as it does not have contests
166+
contest = '' if site == 'spoj' else contest
167+
160168
if os.path.isdir(os.path.join(Utilities.cache_dir, site, contest, problem)):
161169
return True
162170
else:
@@ -169,6 +177,10 @@ def store_files(site, contest, problem, inputs, outputs):
169177
"""
170178
Method to store the test cases in files
171179
"""
180+
181+
# Handle case for SPOJ specially as it does not have contests
182+
contest = '' if site == 'spoj' else contest
183+
172184
for i, inp in enumerate(inputs):
173185
filename = os.path.join(
174186
Utilities.cache_dir, site, contest, problem, 'Input' + str(i))
@@ -251,6 +263,9 @@ def handle_kbd_interrupt(site, contest, problem):
251263
from shutil import rmtree
252264
print 'Cleaning up...'
253265

266+
# Handle case for SPOJ specially as it does not have contests
267+
contest = '' if site == 'spoj' else contest
268+
254269
if problem is not None:
255270
path = os.path.join(Utilities.cache_dir, site, contest, problem)
256271
if os.path.isdir(path):
@@ -275,8 +290,11 @@ def run_solution(problem):
275290
print 'ERROR : No such file'
276291
sys.exit(0)
277292

293+
# For SPOJ, go up two directory levels as it does not have contests
294+
up_dir_level = 2 if problem_path.split('/')[-2] == 'spoj' else 3
295+
278296
testcases_path = os.path.join(
279-
Utilities.cache_dir, *problem_path.split('/')[-3:])
297+
Utilities.cache_dir, *problem_path.split('/')[-up_dir_level:])
280298

281299
if os.path.isdir(testcases_path):
282300
num_cases = len(os.listdir(testcases_path)) / 2
@@ -401,15 +419,26 @@ def run_solution(problem):
401419

402420
else:
403421
print 'Test cases not found locally...'
404-
args = {
405-
'site': testcases_path.split('/')[-3],
406-
'contest': testcases_path.split('/')[-2],
407-
'problem': testcases_path.split('/')[-1],
408-
'force': True
409-
}
422+
423+
# Handle case for SPOJ specially as it does not have contests
424+
if problem_path.split('/')[-2] == 'spoj':
425+
args = {
426+
'site': 'spoj',
427+
'contest': None,
428+
'problem': testcases_path.split('/')[-1],
429+
'force': True
430+
}
431+
else:
432+
args = {
433+
'site': testcases_path.split('/')[-3],
434+
'contest': testcases_path.split('/')[-2],
435+
'problem': testcases_path.split('/')[-1],
436+
'force': True
437+
}
438+
410439
Utilities.download_problem_testcases(args)
411440

412-
print 'Done. Running your solution against sample cases...'
441+
print 'Running your solution against sample cases...'
413442
Utilities.run_solution(problem + '.' + extension)
414443

415444
@staticmethod
@@ -715,6 +744,13 @@ def parse_html(self, req):
715744
soup = bs(req.text, 'html.parser')
716745

717746
test_cases = soup.findAll('pre')
747+
748+
if test_cases is None or len(test_cases) == 0:
749+
print 'Problem not found..'
750+
Utilities.handle_kbd_interrupt(
751+
self.site, self.contest, self.problem)
752+
sys.exit(0)
753+
718754
formatted_inputs, formatted_outputs = [], []
719755

720756
input_list = [

0 commit comments

Comments
 (0)