-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_solving_tester.py
165 lines (145 loc) · 6.02 KB
/
problem_solving_tester.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import importlib
import importlib.util
import sys
import traceback
import math
argv = 'python3 problem_solving_tester.py problem_solving.py n10817239'
if len(sys.argv) != 3:
print('''
Error: you must provide a filename for your pegs solution and your student ID
Make sure that this file is located in the same directory as your solution, along with
probability.py if you use it. Open a terminal, navigate to the directory containing this file,
and run
python3 problem_solving_tester.py problem_solving.py n10817239
replace n10817239 with your student ID.
replace problem_solving.py with the name of your solution file if it is different.
''')
sys.exit(1)
try:
filename = sys.argv[1]
studentid = sys.argv[2]
spec = importlib.util.spec_from_file_location('problem_solving', filename)
if spec is None:
print('''Error when loading the solution file. There may be syntax errors, the file might not be valid Python code, or the file might not exist.''')
sys.exit(1)
problem_solving = importlib.util.module_from_spec(spec)
spec.loader.exec_module(problem_solving)
except Exception:
print("Problem parsing the solution:")
traceback.print_exc()
sys.exit(1)
numtopics = 0
censortests = [
('The cat ate a mouse.', '### cat ate # mouse.'),
('I went to the store.', 'I went to ### store.'),
('A cookie is a very nice thing', '# cookie is # very nice thing'),
('THe thing over there is tHe best!', '### thing over there is ### best!'),
('aN otter ate An apple.', '## otter ate ## apple.')
]
if 'censor' in dir(problem_solving):
numtopics += 1
try:
print('==================================================================================')
print('Attempting censor topic')
for text, result in censortests:
print('----------------------------------------------------------------------------------')
sol = problem_solving.censor(text)
if text != result: result = f'{result} <{studentid}>'
feedback = 'Correct!' if sol == result else 'Incorrect.'
print(f'Text: {text}')
print(f'Result: {sol}')
print(f'Desired result: {result}')
print(feedback)
except Exception:
print('Problem running censor function')
traceback.print_exc()
fertilisertests = [
((1.0, 0.0, 0.0, 1.0, 2.0, 2.0), (2.0, 2.0)),
((0.3, 0.2, 0.1, 0.4, 10, 20 ), (20.0, 40.0)),
((0.1, 0.4, 0.3, 0.2, 10, 20 ), (40.0, 20.0)),
((0.5, 0.5, 0.5, 0.3, 10, 2 ), None),
((0.3, 0.3, 0.3, 0.3, 10, 20 ), None)
]
if 'fertiliser' in dir(problem_solving):
numtopics += 1
try:
print('==================================================================================')
print('Attempting fertiliser topic')
for args, result in fertilisertests:
print('----------------------------------------------------------------------------------')
an, ap, bn, bp, n, p = args
print(f'Function arguments: an = {an} ap = {ap} bn = {bn} bp = {bp} n = {n} p = {p} ')
sol = problem_solving.fertiliser(*args)
if sol is None:
print(f'Result: None')
print(f'Desired result: {str(result)}')
if sol == result:
print('Correct!')
else:
print('Incorrect.')
elif type(sol) is tuple:
if result is None:
print(f'Result: {sol}')
print(f'Desired result: {str(result)}')
print('Incorrect.')
continue
if len(sol) != 2:
print(f'Expected a tuple of length two, got {sol}')
else:
a, b = sol
aresult, bresult = result
print(f'Result: a = {a} b = {b}')
print(f'Desired result: a = {aresult} b = {bresult}')
if abs(a - aresult) > 0.001 or abs(b - bresult) > 0.001:
print('Incorrect.')
else:
print('Correct!')
else:
print(f'Expected None or a tuple of length 2. Got {str(sol)}')
except Exception:
print('Problem running fertiliser function')
traceback.print_exc()
if 'makeBet' in dir(problem_solving):
numtopics += 1
try:
print('==================================================================================')
print('Attempting makeBet topic')
print('----------------------------------------------------------------------------------')
import random
random.seed(0)
totalprofit = 0.0
numberOfRounds = 10000
for round in range(numberOfRounds):
if random.randint(0,1) == 0:
headsprob = 0.7
else:
headsprob = 0.4
previousOutcome = None
state = None
profit = 0.0
odds = dict()
for _ in range(100):
odds['heads'] = random.uniform(1, 3)
odds['tails'] = random.uniform(1, 3)
bet, state = problem_solving.makeBet(odds['heads'], odds['tails'], previousOutcome, state)
previousOutcome = 'heads' if random.random() < headsprob else 'tails'
if bet == previousOutcome:
profit += odds[bet] - 1
elif bet != 'no bet':
profit -= 1 # stake lost
#print("Probability of heads was", headsprob, "Profit was", profit)
totalprofit += profit
averageprofit = totalprofit / numberOfRounds
print("Average profit per run:", averageprofit)
marks = math.ceil(7 * averageprofit / 33.5)
print(f"Estimated number of marks: {marks}")
except Exception:
print('Problem running makeBet function')
traceback.print_exc()
# print()
# print('''If the your solution is correct for the first test case, but not for subsequent
# cases then you may have variables that are only initialised once, and not for each time
# each your function is run. This is likely to be a problem if you use global variables.
# ''')
if numtopics < 2:
print("Warning! Fewer than two topics attepmted!")