@@ -286,7 +286,7 @@ def run_solution(args):
286286 print 'ERROR : No such file'
287287 sys .exit (0 )
288288
289- problem_code = args ['problem' ] if args ['problem' ] else problem
289+ problem_code = args ['problem' ] if args ['problem' ] else basename
290290 contest_code = '' if args ['site' ] == 'spoj' else args ['contest' ]
291291
292292 testcases_path = os .path .join (Utilities .cache_dir , args [
@@ -296,89 +296,74 @@ def run_solution(args):
296296 num_cases = len (os .listdir (testcases_path )) / 2
297297 results , expected_outputs , user_outputs = [], [], []
298298
299- if extension == 'py' :
300-
301- for i in xrange (num_cases ):
302- status = os .system ('cat ' + os .path .join (testcases_path , 'Input' + str (
303- i )) + ' | timeout 3s python \' ' + problem_path + '.py\' > temp_output' + str (i ))
304- if status == 124 :
305- # Time Limit Exceeded
306- results += [Utilities .colors ['BOLD' ] +
307- Utilities .colors ['YELLOW' ] + 'TLE' + Utilities .colors ['ENDC' ]]
308-
309- elif status == 0 :
310- # Ran successfully
311- with open ('temp_output' + str (i ), 'r' ) as temp_handler , open (os .path .join (testcases_path , 'Output' + str (i )), 'r' ) as out_handler :
312- expected_output = out_handler .read ().strip ().split ('\n ' )
313- user_output = temp_handler .read ().strip ().split ('\n ' )
314-
315- expected_output = '\n ' .join (
316- [line .strip () for line in expected_output ])
317- user_output = '\n ' .join (
318- [line .strip () for line in user_output ])
319-
320- expected_outputs += [expected_output ]
321- user_outputs += [user_output ]
322-
323- if expected_output == user_output :
324- # All Correct
325- results += [Utilities .colors ['BOLD' ] + Utilities .colors [
326- 'GREEN' ] + 'AC' + Utilities .colors ['ENDC' ]]
327- else :
328- # Wrong Answer
329- results += [Utilities .colors ['BOLD' ] +
330- Utilities .colors ['RED' ] + 'WA' + Utilities .colors ['ENDC' ]]
331-
332- else :
333- # Runtime Error
334- results += [Utilities .colors ['BOLD' ] +
335- Utilities .colors ['RED' ] + 'RTE' + Utilities .colors ['ENDC' ]]
336-
337- elif extension in ['c' , 'cpp' , 'java' ]:
338-
339- compiler = {'c' : 'gcc' , 'cpp' : 'g++' , 'java' : 'javac -d .' }[extension ]
340- execute_command = {'c' : './a.out' , 'cpp' : './a.out' , 'java' : 'java ' + basename }[extension ]
341- compile_status = os .system (
342- compiler + ' \' ' + problem_path + '.' + extension + '\' ' )
299+ if extension in ['c' , 'cpp' , 'java' , 'py' , 'hs' , 'rb' ]:
300+
301+ # Compiler flags taken from http://codeforces.com/blog/entry/79
302+ compiler = {
303+ 'hs' : 'ghc --make -O -dynamic -o ' + basename ,
304+ 'py' : None ,
305+ 'rb' : None ,
306+ 'c' : 'gcc -static -DONLINE_JUDGE -fno-asm -lm -s -O2 -o ' + basename ,
307+ 'cpp' : 'g++ -static -DONLINE_JUDGE -lm -s -x c++ -O2 -std=c++14 -o ' + basename ,
308+ 'java' : 'javac -d .'
309+ }[extension ]
310+
311+ execute_command = {
312+ 'py' : 'python \' ' + problem_path + '.' + extension + '\' ' ,
313+ 'rb' : 'ruby \' ' + problem_path + '.' + extension + '\' ' ,
314+ 'hs' : './' + basename ,
315+ 'c' : './' + basename ,
316+ 'cpp' : './' + basename ,
317+ 'java' : 'java -DONLINE_JUDGE=true -Duser.language=en -Duser.region=US -Duser.variant=US ' + basename
318+ }[extension ]
319+
320+ if compiler is None :
321+ compile_status = 0
322+ else :
323+ compile_status = os .system (
324+ compiler + ' \' ' + problem_path + '.' + extension + '\' ' )
343325
344326 if compile_status == 0 :
345327
346328 # Compiled successfully
347329 for i in xrange (num_cases ):
348330 status = os .system ('timeout 2s ' + execute_command + ' < ' + os .path .join (
349331 testcases_path , 'Input' + str (i )) + ' > temp_output' + str (i ))
350- if status == 124 :
351- # Time Limit Exceeded
352- results += [Utilities .colors ['BOLD' ] + Utilities .colors [
353- 'YELLOW' ] + 'TLE' + Utilities .colors ['ENDC' ]]
354-
355- elif status == 0 :
356- # Ran successfully
357- with open ('temp_output' + str (i ), 'r' ) as temp_handler , open (os .path .join (testcases_path , 'Output' + str (i )), 'r' ) as out_handler :
358- expected_output = out_handler .read ().strip ().split ('\n ' )
359- user_output = temp_handler .read ().strip ().split ('\n ' )
360-
361- expected_output = '\n ' .join (
362- [line .strip () for line in expected_output ])
363- user_output = '\n ' .join (
364- [line .strip () for line in user_output ])
365-
366- expected_outputs += [expected_output ]
367- user_outputs += [user_output ]
368-
369- if expected_output == user_output :
370- # All Correct
371- results += [Utilities .colors ['BOLD' ] + Utilities .colors [
372- 'GREEN' ] + 'AC' + Utilities .colors ['ENDC' ]]
373- else :
374- # Wrong Answer
332+
333+ with open (os .path .join (testcases_path , 'Output' + str (i )), 'r' ) as out_handler :
334+ expected_output = out_handler .read ().strip ().split ('\n ' )
335+ expected_output = '\n ' .join (
336+ [line .strip () for line in expected_output ])
337+ expected_outputs += [expected_output ]
338+
339+ if status == 31744 :
340+ # Time Limit Exceeded
375341 results += [Utilities .colors ['BOLD' ] + Utilities .colors [
376- 'RED' ] + 'WA' + Utilities .colors ['ENDC' ]]
342+ 'YELLOW' ] + 'TLE' + Utilities .colors ['ENDC' ]]
343+ user_outputs += ['' ]
344+
345+ elif status == 0 :
346+ # Ran successfully
347+ with open ('temp_output' + str (i ), 'r' ) as temp_handler :
348+ user_output = temp_handler .read ().strip ().split ('\n ' )
349+ user_output = '\n ' .join (
350+ [line .strip () for line in user_output ])
351+ user_outputs += [user_output ]
352+
353+ if expected_output == user_output :
354+ # All Correct
355+ results += [Utilities .colors ['BOLD' ] + Utilities .colors [
356+ 'GREEN' ] + 'AC' + Utilities .colors ['ENDC' ]]
357+ else :
358+ # Wrong Answer
359+ results += [Utilities .colors ['BOLD' ] + Utilities .colors [
360+ 'RED' ] + 'WA' + Utilities .colors ['ENDC' ]]
377361
378- else :
379- # Runtime Error
380- results += [Utilities .colors ['BOLD' ] +
381- Utilities .colors ['RED' ] + 'RTE' + Utilities .colors ['ENDC' ]]
362+ else :
363+ # Runtime Error
364+ results += [Utilities .colors ['BOLD' ] +
365+ Utilities .colors ['RED' ] + 'RTE' + Utilities .colors ['ENDC' ]]
366+ user_outputs += ['' ]
382367 else :
383368 # Compilation error occurred
384369 message = Utilities .colors ['BOLD' ] + Utilities .colors [
@@ -387,7 +372,7 @@ def run_solution(args):
387372 sys .exit (0 )
388373
389374 else :
390- print 'Supports only C, C++, Python and Java as of now.'
375+ print 'Supports only C, C++, Python, Java, Ruby and Haskell as of now.'
391376 sys .exit (0 )
392377
393378 from terminaltables import AsciiTable
0 commit comments