@@ -278,10 +278,12 @@ def handle_kbd_interrupt(site, contest, problem):
278
278
print 'Done. Exiting gracefully.'
279
279
280
280
@staticmethod
281
- def run_solution (problem ):
281
+ def run_solution (args ):
282
282
"""
283
283
Method to run and test the user's solution against sample cases
284
284
"""
285
+ problem = args ['source' ]
286
+
285
287
extension = problem .split ('.' )[- 1 ]
286
288
problem = problem .split ('.' )[0 ]
287
289
problem_path = os .path .join (os .getcwd (), problem )
@@ -290,11 +292,19 @@ def run_solution(problem):
290
292
print 'ERROR : No such file'
291
293
sys .exit (0 )
292
294
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
+ if args ['problem' ]:
296
+ # Check if problem code has been specified explicitly
297
+ args ['contest' ] = '' if args ['site' ] == 'spoj' else args ['contest' ]
298
+ testcases_path = os .path .join (Utilities .cache_dir , args ['site' ], args [
299
+ 'contest' ], args ['problem' ])
300
+ else :
301
+ # Take arguments from path
302
+
303
+ # For SPOJ, go up two directory levels as it does not have contests
304
+ up_dir_level = 2 if problem_path .split ('/' )[- 2 ] == 'spoj' else 3
295
305
296
- testcases_path = os .path .join (
297
- Utilities .cache_dir , * problem_path .split ('/' )[- up_dir_level :])
306
+ testcases_path = os .path .join (
307
+ Utilities .cache_dir , * problem_path .split ('/' )[- up_dir_level :])
298
308
299
309
if os .path .isdir (testcases_path ):
300
310
num_cases = len (os .listdir (testcases_path )) / 2
@@ -311,7 +321,7 @@ def run_solution(problem):
311
321
Utilities .colors ['YELLOW' ] + 'TLE' + Utilities .colors ['ENDC' ]]
312
322
313
323
elif status == 0 :
314
-
324
+ # Ran successfully
315
325
with open ('temp_output' + str (i ), 'r' ) as temp_handler , open (os .path .join (testcases_path , 'Output' + str (i )), 'r' ) as out_handler :
316
326
expected_output = out_handler .read ().strip ().split ('\n ' )
317
327
user_output = temp_handler .read ().strip ().split ('\n ' )
@@ -345,6 +355,8 @@ def run_solution(problem):
345
355
compiler + ' ' + problem_path + '.cpp' )
346
356
347
357
if compile_status == 0 :
358
+
359
+ # Compiled successfully
348
360
for i in xrange (num_cases ):
349
361
status = os .system ('timeout 2s ./a.out < ' + os .path .join (
350
362
testcases_path , 'Input' + str (i )) + ' > temp_output' + str (i ))
@@ -354,7 +366,7 @@ def run_solution(problem):
354
366
'YELLOW' ] + 'TLE' + Utilities .colors ['ENDC' ]]
355
367
356
368
elif status == 0 :
357
-
369
+ # Ran successfully
358
370
with open ('temp_output' + str (i ), 'r' ) as temp_handler , open (os .path .join (testcases_path , 'Output' + str (i )), 'r' ) as out_handler :
359
371
expected_output = out_handler .read ().strip ().split ('\n ' )
360
372
user_output = temp_handler .read ().strip ().split ('\n ' )
@@ -388,7 +400,7 @@ def run_solution(problem):
388
400
sys .exit (0 )
389
401
390
402
else :
391
- print 'Supports only C++ and Python as of now. Support for Java coming soon.'
403
+ print 'Supports only C, C ++ and Python as of now. Support for Java coming soon.'
392
404
sys .exit (0 )
393
405
394
406
from terminaltables import AsciiTable
@@ -420,26 +432,31 @@ def run_solution(problem):
420
432
else :
421
433
print 'Test cases not found locally...'
422
434
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
- }
435
+ if args ['problem' ] is None :
436
+ # Handle case for SPOJ specially as it does not have contests
437
+ if problem_path .split ('/' )[- 2 ] == 'spoj' :
438
+ args = {
439
+ 'site' : 'spoj' ,
440
+ 'contest' : None ,
441
+ 'problem' : testcases_path .split ('/' )[- 1 ],
442
+ 'force' : True ,
443
+ 'source' : problem + '.' + extension
444
+ }
445
+ else :
446
+ args = {
447
+ 'site' : testcases_path .split ('/' )[- 3 ],
448
+ 'contest' : testcases_path .split ('/' )[- 2 ],
449
+ 'problem' : testcases_path .split ('/' )[- 1 ],
450
+ 'force' : True ,
451
+ 'source' : problem + '.' + extension
452
+ }
453
+ elif args ['site' ] == 'spoj' :
454
+ args ['contest' ] = None
438
455
439
456
Utilities .download_problem_testcases (args )
440
457
441
458
print 'Running your solution against sample cases...'
442
- Utilities .run_solution (problem + '.' + extension )
459
+ Utilities .run_solution (args )
443
460
444
461
@staticmethod
445
462
def get_html (url ):
0 commit comments