Skip to content

Instantly share code, notes, and snippets.

@Franceshe
Forked from rajarsheem/littlechecker.py
Created June 8, 2021 09:01
Show Gist options
  • Save Franceshe/13baa9ad6dcdf7c602cefd5ce0d491dc to your computer and use it in GitHub Desktop.
Save Franceshe/13baa9ad6dcdf7c602cefd5ce0d491dc to your computer and use it in GitHub Desktop.
A little code checker tool in python for Java,C,Cpp. Handles compile error, runtime error, accepted, wrong, TLE.
import os, filecmp
codes = {200:'success',404:'file not found',400:'error',408:'timeout'}
def compile(file,lang):
if lang == 'java':
class_file = file[:-4]+"class"
elif lang == 'c':
class_file = file[:-2]
elif lang=='cpp':
class_file = file[:-4]
if (os.path.isfile(class_file)):
os.remove(class_file)
if (os.path.isfile(file)):
if lang == 'java':
os.system('javac '+file)
elif lang == 'c' or lang == 'cpp':
os.system('gcc -o '+class_file+' '+file)
if (os.path.isfile(class_file)):
return 200
else:
return 400
else:
return 404
def run(file,input,timeout,lang):
if lang == 'java':
cmd = 'java '+file
elif lang=='c' or lang=='cpp':
cmd = './'+file
r = os.system('timeout '+timeout+' '+cmd+' < '+input+' > out.txt')
if lang == 'java':
os.remove(file+'.class')
elif lang == 'c' or lang == 'cpp':
os.remove(file)
if r==0:
return 200
elif r==31744:
os.remove('out.txt')
return 408
else:
os.remove('out.txt')
return 400
def match(output):
if os.path.isfile('out.txt') and os.path.isfile(output):
b = filecmp.cmp('out.txt',output)
#os.remove('out.txt')
return b
else:
return 404
file = 'add.c'
lang = 'c'
testin = 'testin.txt'
testout = 'testout.txt'
timeout = '1' # secs
print(codes[compile(file,'c')])
print (codes[run('add',testin,timeout,lang)])
print (match(testout)) # True implies that code is accepted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment