diff options
author | raylu <raylu@cmu.edu> | 2011-06-04 01:36:18 -0700 |
---|---|---|
committer | raylu <raylu@cmu.edu> | 2011-06-04 01:36:18 -0700 |
commit | 8c26371331f08446690885438c3ea36b2eeee52c (patch) | |
tree | efb7fcf0503f3fc38a7cccaed931b672b4fbd0be /graders | |
parent | 4c2c021adc2e60b0cdd0517250350a43a5ba8e31 (diff) | |
download | pyc-8c26371331f08446690885438c3ea36b2eeee52c.tar.xz |
Lab 3 (pizza)
Diffstat (limited to 'graders')
-rw-r--r-- | graders/genpizza.py | 53 | ||||
-rw-r--r-- | graders/handout.piz | 4 | ||||
-rw-r--r-- | graders/invalid.piz | 5 | ||||
-rwxr-xr-x | graders/lab3.py | 99 |
4 files changed, 161 insertions, 0 deletions
diff --git a/graders/genpizza.py b/graders/genpizza.py new file mode 100644 index 0000000..82f63a2 --- /dev/null +++ b/graders/genpizza.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +handout = [ + {'toppings': [('pepperoni', 0)], 'size': 12}, + { + 'toppings': [ + ('tomato', 1), + ('sauce', 2), + ], + 'size': 8 + }, + { + 'toppings': [ + ('some stuff', 0), + ('other stuff', 1), + ('even more stuff', 1), + ], + 'size': 10 + }, + { + 'toppings': [ + ('tomato', 1), + ('sauce', 2), + ('', 0), + ('food', 0), + ], + 'size': 6 + }, + ] +invalid = [ + {'toppings': [('pepperoni', 0)], 'size': 10}, + {'toppings': [('biribiri', 0)]}, + {'size': 6}, + {'toppings': [('pepperoni', 0)], 'size': 9}, + {'toppings': [('cheese', 1)], 'size': 12}, + ] + +def write_pizza(f, pizza): + if 'toppings' in pizza: + for topping in pizza['toppings']: + f.write('%s,%d,' % (topping[0], topping[1])) + if 'size' in pizza: + f.write(str(pizza['size'])) + f.write('\n') + +def write_pizfile(filename, pizzas): + f = open(filename, 'w') + for pizza in pizzas: + write_pizza(f, pizza) + f.close() + +write_pizfile('handout.piz', handout) +write_pizfile('invalid.piz', invalid) diff --git a/graders/handout.piz b/graders/handout.piz new file mode 100644 index 0000000..ff9a8d5 --- /dev/null +++ b/graders/handout.piz @@ -0,0 +1,4 @@ +pepperoni,0,12 +tomato,1,sauce,2,8 +some stuff,0,other stuff,1,even more stuff,1,10 +tomato,1,sauce,2,,0,food,0,6 diff --git a/graders/invalid.piz b/graders/invalid.piz new file mode 100644 index 0000000..a3176c1 --- /dev/null +++ b/graders/invalid.piz @@ -0,0 +1,5 @@ +pepperoni,0,10 +biribiri,0, +6 +pepperoni,0,9 +cheese,1,12 diff --git a/graders/lab3.py b/graders/lab3.py new file mode 100755 index 0000000..5d4fdb0 --- /dev/null +++ b/graders/lab3.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python + +import sys +sys.path.append('/home/dotcloud/env/lib/python2.6/site-packages') +from sandbox import Sandbox, SandboxConfig +import os, traceback + +importname = sys.argv[1] +if importname.endswith('.py'): + importname = importname[:len(importname)-3] +else: + print >> sys.stderr, 'Filename did not end with .py for some reason...' + sys.exit(2) + +os.chdir(sys.path[0]) +sys.path.insert(0, os.path.expanduser('~/submissions')) +sys.dont_write_bytecode = True + +from random import seed, randint +seed(1) +def wins_best_record(teams, games): + wins = [[0] * teams for _ in range(teams)] + for i in xrange(teams): + for j in xrange(teams): + wins[i][j] = randint(0, games) + wins[j][i] = games - wins[i][j] + sums = map(sum, wins) + largest_idx = 0 + for i in xrange(1, len(sums)): + if sums[i] > sums[largest_idx]: + largest_idx = i + return wins, largest_idx, sums[largest_idx] + +pizfiles = [('handout.piz', 3950), ('invalid.piz', 1050)] + +output = '' +score = 0 +def run(): + exec("import %s as submission" % importname) + global output, score + + pizzas = [ + {'num_toppings': 1, 'toppings': [('topping', 0)], 'size': 10}, + {'num_toppings': 2, 'toppings': [('one', 1), ('two', 2)], 'size': 12}, + {'num_toppings': 0, 'toppings': [], 'size': 8}, + {'num_toppings': 1, 'toppings': [('topping', 0)], 'size': 7}, + {'num_toppings': 2, 'toppings': [('topping one', 1), ('topping two', 1)], 'size': 6}, + {'num_toppings': 3, 'toppings': [('one', 2), ('two', 1), ('three', 0)], 'size': 12}, + {'toppings': [('', 0)], 'size': 10}, + {'num_toppings': 1, 'toppings': [('topping', 0)]}, + ] + validity = [True, True, False, False, False, True, False, False] + for i in range(len(pizzas)): + if submission.valid_pizza(pizzas[i]) == validity[i]: + score += 1 + else: + output += '%s should be %s\n' % (pizzas[i], 'valid' if validity[i] else 'invalid') + + wbr_args = [(5, 3), (5, 3), (16, 5)] + for args in wbr_args: + wins, b_team, b_number = wins_best_record(*args) + team, number = submission.best_record(wins) + if team == b_team: + score += 1 + else: + output += 'Best record of\n' + for l in wins: + output += ' %s\n' % l + output += ' should be team #%d\n' % b_team + if number == b_number: + score += 1 + else: + output += 'Best record of\n' + for l in wins: + output += ' %s\n' % l + output += ' should be %d wins\n' % b_number + + for pizfile in pizfiles: + result = submission.order_pizza(pizfile[0]) + if result == pizfile[1]: + score += 3 + else: + output += 'Order was incorrect for %s; got %d (answer is hidden)\n' % (pizfile[0], result) + + return score + +config = SandboxConfig() +config.timeout = 5 +config.allowModule(importname, 'valid_pizza', 'best_record', 'order_pizza') +for pizfile in pizfiles: + config.allowPath(pizfile[0]) +sandbox = Sandbox(config) +try: + score = sandbox.call(run) +except: + traceback.print_exc(20, sys.stderr) + print >> sys.stderr +print score +print >> sys.stderr, output |