# -*- coding: utf-8 -*- #### ΜΗ ΠΕΙΡΑΖΕΤΕ ΑΥΤΟ ΤΟ ΑΡΧΕΙΟ #### #### #### Πρόγραμμα ελέγχου του προγράμματος που γράφουν οι φοιτητές. Μη πειράζετε αυτό το πρόγραμμα. #### Γράφετε το πρόγραμμά σας στο αρχείο user.py και μόνο εκεί. #### import re import sys import os import subprocess # Το πρόγραμμα του χρήστη βρίσκεται στο "userfilename" userfilename="user.py" # Τα προγράμματα ελέγχου που γράφονται αυτόματα έχουν το παρακάτω πρόθεμα testerfilename="__testcode__" # Αυτή η συνάρτηση φτινει το πρόγραμμα python που ελέγχει το πρόγραμμα του χρήστη στην είσοδο inp και # έξοδο outp. # firstline είναι ο αριθμός της πρώτης γραμμής του κώδικα του χρήστη και lastline ο αριθμός της τελευταίας # fname είναι το όνομα του αρχείου του χρήστη def maketestprog(inp, outp, firstline, lastline, fname): f = open(fname, 'w+') print >> f, "# -*- coding: utf-8 -*-" print >> f, "import sys, math" print >> f, "a = {a}; b = {b}; c = {c}; r1 = 0; r2 = 0; s = 0".format(a=inp[0], b=inp[1], c=inp[2]) print >> f, "# start user code" for i in range(firstline, lastline+1): print >> f, lines[i] print >> f, "# end user code" # r1 = outp[0]; r2 = outp[1]; s = outp[2] print >>f, """ if abs(s-{s})>=1e-6: print \"ERROR με είσοδο a={a}, b={b}, c={c}\" sys.exit(1) if (s==1) and ( (abs(r1-{r1})>=1e-6) or (abs(r2-{r2})>=1e-6) ): print \"ERROR με είσοδο a={a}, b={b}, c={c}\" sys.exit(1) else: print "OK" sys.exit(0) """.format(a=inp[0], b=inp[1], c=inp[2], r1=outp[0], r2=outp[1], s=outp[2]) #% (s, inp[0], inp[1], inp[2], r1, r2, inp[0], inp[1], inp[2]) return fname # Στις λίστες inputs, outputs βρίσκονται κάποια inputs και τα αντίστοιχα σωστά outputs inputs = [ [1,-1,1], [4, -3, 2], [1, -4, -5], [-1, 2.2, 3], [-4, 3, 8] ] outputs = [ [0, 0, 0], [0, 0, 0], [-1, 5, 1], [-0.951828, 3.151828, 1], [-1.088087, 1.838087, 1] ] # lines = open(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 "-------------------Δοκιμάζουμε περίπτωση %d------------------"% i filename = maketestprog(inputs[i], outputs[i], firstline, lastline, testerfilename+str(i)+".py") exit_code = subprocess.call(["python", filename]) if exit_code == 0: print "---Περίπτωση %d OK"% i else: errors += 1 print "---Περίπτωση %d ERROR"% i print if errors>0: print "****** Το πρόγραμμά σας εμφάνισε σφάλμα σε κάποιες από τις περιπτώσεις ελέγχου" else: print "****** Το πρόγραμμά σας εκτελέστηκε σωστά σε όλες τις περιπτώσεις ελέγχου"