Skip to content

Instantly share code, notes, and snippets.

@rajarsheem
Last active March 23, 2024 18:18
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save rajarsheem/6089309fd5bb9b3c71ab to your computer and use it in GitHub Desktop.
Save rajarsheem/6089309fd5bb9b3c71ab 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.
@rajarsheem
Copy link
Author

Java,C,Cpp.
Handles compile error, runtime error, accepted, wrong, TLE.
Assumes all the files are in the same directory.

@isopropylcyanide
Copy link

isopropylcyanide commented Mar 5, 2017

Hi Rajarsheem, I have updated your code to a more modular object oriented design. Also, regular expression have been used to replace the task of separating file extensions by hand. Here's a look
(https://gist.github.com/isopropylcyanide/fe6a02acad68656a08ddbd57d357cd00)

@fanefaiz
Copy link

How do you integrate with java backend?

@krshubham
Copy link

krshubham commented Dec 6, 2017

The only problem is that its not safe. The code may contain some dangerous function call that can affect the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment