#### ΜΗ ΠΕΙΡΑΖΕΤΕ ΑΥΤΟ ΤΟ ΑΡΧΕΙΟ #### #### #### Πρόγραμμα ελέγχου του προγράμματος που γράφουν οι φοιτητές. Μη πειράζετε αυτό το πρόγραμμα. #### Γράφετε το πρόγραμμά σας στο αρχείο user.py και μόνο εκεί. #### import re import sys import os import subprocess # Το πρόγραμμα του χρήστη βρίσκεται στο "tst_userfilename" tst_userfilename="user.py" # Αν ο χρήστης έχει δώσει ένα όρισμα στην κλήση του προγράμματος τότε αυτό είναι το όνομα # του προγράμματος του χρήστη που θέλουμε να ελεγχθεί και όχι απαραίτητα το 'user.py' if len(sys.argv)>1: tst_userfilename=sys.argv[1] # Τα προγράμματα ελέγχου που γράφονται αυτόματα έχουν το παρακάτω πρόθεμα tst_testerfilename="__testcode__" # Αυτή η συνάρτηση φτιάχνει το πρόγραμμα python που ελέγχει το πρόγραμμα του χρήστη στην είσοδο inp και # έξοδο outp. # firstline είναι ο αριθμός της πρώτης γραμμής του κώδικα του χρήστη και lastline ο αριθμός της τελευταίας # fname είναι το όνομα του αρχείου του χρήστη def maketestprog(inp, outp, firstline, lastline, fname): f = open(fname, 'w+') print("#### Checking file: {uf}".format(uf=tst_userfilename), file=f) print("import sys, math, os, signal", file=f) print("LLL={LLL}".format(LLL=inp), file=f) #### Change this (especially for string args) print("_L=LLL", file=f) #### Change this print("N=LLL[0]; F=LLL[1]; x=LLL[2]", file=f) #### Change this print(""" def _signal_handler(signum, frame): raise Exception("Timed out!") if hasattr(signal, 'SIGALRM'): signal.signal(signal.SIGALRM, _signal_handler) signal.alarm(3) # 3 seconds """, file=f) # print(""" #def _bad(*args): # print("*** You used a forbidden function ***") # return 0 # #__builtins__.max = _bad #__builtins__.min = _bad #""", file=f) print("# start user code", file=f) for i in range(firstline, lastline+1): print(lines[i], file=f, end='') print("# end user code", file=f) print(""" uuu = result #### Change this if abs(uuu - {uuu}) > 1e-5: #### Change this line. Use quotes to compare strings. print("Σφάλμα στην είσοδο {LLL}") print("Το αποτέλεσμα του προγράμματος ήταν ", uuu, ". Έπρεπε να είναι {uuu}.") sys.exit(1) else: print("OK") sys.exit(0) """.format(LLL=inp, uuu=outp), file=f) return fname # Στις λίστες inputs, outputs βρίσκονται κάποια inputs και τα αντίστοιχα σωστά outputs inputs = [ [5, [1.1, -1, 2.3, 4.1, 0.8], 0.45], [5, [1.1, -1, 2.3, 4.1, 0.8], 0.35], [100, 100*[0.33], 0.345], [100, 50*[0.33, 0.44], 0.355], [200, 200*[0.19], 0.7], [200, 100*[0.32]+100*[0.9], 0.7], [300, 100*[0.32]+100*[0.9]+100*[0.6], 0.5], ] outputs = [ 2.3, -1, 0.33, 0.44, 0.19, 0.9, 0.9, ] # lines = open(tst_userfilename).readlines() for i,l in enumerate(lines): if re.match('####START', l): #print("START found at line %d, [%s]"% (i, l)) firstline = i+1 if re.match('####STOP', l): #print("STOP found at line %d, [%s]"% (i, l)) lastline = i-1 errors=0 for i,inp in enumerate(inputs): print print("-------------------Case No %d------------------"% i) filename = maketestprog(inputs[i], outputs[i], firstline, lastline, tst_testerfilename+str(i)+".py") if os.name=='posix': exit_code = subprocess.call(["timeout", "3s", "python3", filename]) else: exit_code = subprocess.call(["python3", filename]) exit_code = subprocess.call(["timeout", "3s", "python3", filename]) if exit_code == 0: print("---Case No %d: OK"% i) else: errors += 1 print("---Case No %d: ERROR"% i) print() if errors>0: print("****** The program has run in error in some cases.") sys.exit(1) else: print("****** The program has run correctly in all cases.") sys.exit(0)