@@ -278,10 +278,12 @@ def handle_kbd_interrupt(site, contest, problem):
278278 print 'Done. Exiting gracefully.'
279279
280280 @staticmethod
281- def run_solution (problem ):
281+ def run_solution (args ):
282282 """
283283 Method to run and test the user's solution against sample cases
284284 """
285+ problem = args ['source' ]
286+
285287 extension = problem .split ('.' )[- 1 ]
286288 problem = problem .split ('.' )[0 ]
287289 problem_path = os .path .join (os .getcwd (), problem )
@@ -290,11 +292,19 @@ def run_solution(problem):
290292 print 'ERROR : No such file'
291293 sys .exit (0 )
292294
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
295305
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 :])
298308
299309 if os .path .isdir (testcases_path ):
300310 num_cases = len (os .listdir (testcases_path )) / 2
@@ -311,7 +321,7 @@ def run_solution(problem):
311321 Utilities .colors ['YELLOW' ] + 'TLE' + Utilities .colors ['ENDC' ]]
312322
313323 elif status == 0 :
314-
324+ # Ran successfully
315325 with open ('temp_output' + str (i ), 'r' ) as temp_handler , open (os .path .join (testcases_path , 'Output' + str (i )), 'r' ) as out_handler :
316326 expected_output = out_handler .read ().strip ().split ('\n ' )
317327 user_output = temp_handler .read ().strip ().split ('\n ' )
@@ -345,6 +355,8 @@ def run_solution(problem):
345355 compiler + ' ' + problem_path + '.cpp' )
346356
347357 if compile_status == 0 :
358+
359+ # Compiled successfully
348360 for i in xrange (num_cases ):
349361 status = os .system ('timeout 2s ./a.out < ' + os .path .join (
350362 testcases_path , 'Input' + str (i )) + ' > temp_output' + str (i ))
@@ -354,7 +366,7 @@ def run_solution(problem):
354366 'YELLOW' ] + 'TLE' + Utilities .colors ['ENDC' ]]
355367
356368 elif status == 0 :
357-
369+ # Ran successfully
358370 with open ('temp_output' + str (i ), 'r' ) as temp_handler , open (os .path .join (testcases_path , 'Output' + str (i )), 'r' ) as out_handler :
359371 expected_output = out_handler .read ().strip ().split ('\n ' )
360372 user_output = temp_handler .read ().strip ().split ('\n ' )
@@ -388,7 +400,7 @@ def run_solution(problem):
388400 sys .exit (0 )
389401
390402 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.'
392404 sys .exit (0 )
393405
394406 from terminaltables import AsciiTable
@@ -420,26 +432,31 @@ def run_solution(problem):
420432 else :
421433 print 'Test cases not found locally...'
422434
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
438455
439456 Utilities .download_problem_testcases (args )
440457
441458 print 'Running your solution against sample cases...'
442- Utilities .run_solution (problem + '.' + extension )
459+ Utilities .run_solution (args )
443460
444461 @staticmethod
445462 def get_html (url ):
0 commit comments