summaryrefslogtreecommitdiffstats
path: root/graders/lab3.py
blob: 0a3300d23fd3e25ee43c34379cfd2d9837dfc51a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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():
	submission = __import__(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