-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathmath-code-test-b.py
273 lines (212 loc) · 7.93 KB
/
math-code-test-b.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Tests for Foundational Math Cert 2
def step01():
print("Code test Passed")
print("Go on to the next step")
def step02(code):
import re
print(" ")
if re.search("plt\.axis\(\[\-20\,\s*20\,\s*\-20\,\s*20\]\)", code):
print("Code test passed")
print("Go on to the next step")
elif re.search("plt\.axis\(\[\-10\,\s*10\,\s*\-10\,\s*10\]\)", code):
print("Now change the values in plt.axis() and re-run the code.")
else:
print("You should use plt.axis([-20,20,-20,20]) in your code")
def step03(code):
import re
print(" ")
if re.search("xmax\s*=\s*20", code):
print("Code test passed")
print("Go on to the next step")
elif re.search("xmax\s*=\s*10", code):
print("Now change the values xmax to 20 and re-run the code.")
else:
print("You should use xmax = 20 in your code")
def step04(code):
import re
print(" ")
code_count = 0
#plt.plot([xmin,xmax],[0,0],'b') # blue x axis
if re.search("plt\.plot\(\[xmin\,xmax\]\,\[0\,0\]\,\s*\'g\'\)", code):
code_count = code_count+1
elif re.search("plt\.plot\(\[xmin\,xmax\]\,\[0\,0\]\,\s*\'b\'\)", code):
print("Now change the 'b' to 'g' for each axis and re-run the code.")
else:
print("You should use plt.plot([xmin,xmax],[0,0],'g') in your code")
#plt.plot([0,0],[ymin,ymax], 'b') # blue y axis
if re.search("plt\.plot\(\[0\,0\]\,\[ymin\,ymax\]\,\s*\'g\'\)", code):
code_count = code_count+1
elif re.search("plt\.plot\(\[0\,0\]\,\[ymin\,ymax\]\,\s*\'b\'\)", code):
print("Now change the 'b' to 'g' for each axis and re-run the code.")
else:
print("You should use plt.plot([0,0],[ymin,ymax], 'g') in your code")
if code_count==2:
print("Code test passed")
print("Go on to the next step")
def step05(code):
import re
print(" ")
if re.search("plt\.plot\(\[\-5\]\,\s*\[1\]\,\s*\'ro\'\)", code):
print("Code test passed")
print("Go on to the next step")
else:
print("You should use plt.plot([-5],[1], \'ro\') in your code")
def step06(code):
import re
code_count = 0
print(" ")
if re.search("x\s*\=\s*\[4\,\s*1\,\s*2\]", code):
code_count = code_count + 1
else:
print("You should use x = [4,1,2] in your code")
if re.search("y\s*\=\s*\[2\,\s*1\,\s*5\]", code):
code_count = code_count + 1
else:
print("You should use y = [2,1,5] in your code")
if code_count == 2:
print("Code test passed")
print("Go on to the next step")
def step07(code):
import re
code_count = 0
print(" ")
if re.search("plt.plot\(linex\,\s*liney\,\s*\'r\'\)", code):
code_count = code_count + 1
else:
print("You should use plt.plot(linex, liney, \'r\') in your code")
if re.search("plt.plot\(pointx\,\s*pointy\,\s*\'gs\'\)", code):
code_count = code_count + 1
else:
print("You should use plt.plot(pointx, pointy, \'gs\') in your code")
if code_count == 2:
print("Code test passed")
print("Go on to the next step")
def step08(count):
if count==3:
print("You scored 3 out of 3. Good job!")
print("You can go on to the next step")
def step09(code):
import re
print(" ")
if re.search("plt\.plot\(x\,\s*\-x\s*\+\s*3\)", code):
print("Code test passed")
print("Go on to the next step")
elif re.search("plt\.plot\(x\,\s*2\*x\s*\-\s*3\)", code):
print("Now change the equation and re-run the code.")
else:
print("You should use plt.plot(x, -x + 3) in your code.")
# step 10 re-uses step01()
def step11(code):
import re
print(" ")
if re.search("y2\s*\=\s*\-x\s*\-\s*3", code):
print("Code test passed")
print("Go on to the next step")
elif re.search("y2\s*\=\s*x\*\*2\s*\-\s*3", code):
print("Now change y2 and re-run the code.")
else:
print("You should use y2 = -x - 3 in your code.")
def step12(code):
import re
print(" ")
if re.search("linsolve\(\[2\*x\s*\+\s*y\s*\-\s*15\,\s*3\*x\s*\-\s*y\],\s*\(x\,\s*y\)\)", code):
print("Code test passed")
print("Go on to the next step")
elif re.search("linsolve\(\[2\*x\s*\+\s*y\s*\-\s*1\,\s*x\s*\-\s*2\*y\s*\+\s*7\]\,\s*\(x\, y\)\)", code):
print("Now change the equations and re-run the code.")
else:
print("You should use linsolve([2*x + y - 15, 3*x - y], (x,y)) in your code.")
# step 13 uses step01()
def step14():
print(" ")
print("If you didn't get a syntax error, code test passed")
# steps 15 and 16 re-use step01()
def step17(code):
import re
code_count = 0
first_run = 0
if re.search("f\(a\,b\)\:", code):
print("Now change the code and run it again")
first_run = 1
if re.search("f\(a\,\s*b\,\s*c\)\:", code):
code_count = code_count + 1
elif first_run==1:
code_count = 0
else:
print("You should use f(a,b,c): in your code")
# plt.plot(x, a*x**2 + b*x + c)
if re.search("\(x\,\s*a\*x\*\*2\s*\+\s*b\*x\s*\+\s*c\)", code):
code_count = code_count + 1
elif first_run==1:
code_count = 0
else:
print("You should use plt.plot(x, a*x**2 + b*x + c) in your code")
# interactive(f, a=(-9, 9), b=(-9,9), c=(-9,9))
if re.search("\(f\,\s*a\=\(\-9\,\s*9\)\,\s*b\=\(\-9\,\s*9\)\,\s*c\=\(\-9\,\s*9\)\)", code):
code_count = code_count + 1
elif first_run==1:
code_count = 0
else:
print("You should include 'interactive(f, a=(-9, 9), b=(-9,9), c=(-9,9))' in your code")
if code_count == 3:
print("Code test passed")
print("Go on to the next step")
print(" ")
def step18(code):
import re
print(" ")
code_count = 0
if re.search("vx\s*\=\s*\-b\/\(2\*a\)", code):
code_count = code_count + 1
else:
print("You should include vx = -b/(2*a)")
if re.search("vy\s*\=\s*a\*vx\*\*2\s*\+\s*b\*vx\s*\+\s*c", code):
code_count = code_count + 1
else:
print("You should include vy = a*vx**2 + b*vx + c")
if code_count==2:
print("Code test passed")
print("Go on to the next step")
def step19(code):
import re
print(" ")
# vx = -b/(2*a)
# vy = a*vx**2 + b*vx + c
code_count = 0
if re.search("vx\s*\=\s*\-b\/\(2\*a\)", code):
code_count = code_count + 1
else:
print("You should include vx = -b/(2*a)")
if re.search("vy\s*\=\s*a\*vx\*\*2\s*\+\s*b\*vx\s*\+\s*c", code):
code_count = code_count + 1
else:
print("You should include vy = a*vx**2 + b*vx + c")
if code_count==2:
print("Code test passed")
print("Go on to the next step")
# step 20 - no test
def step21(code):
import re
print(" ")
code_count = 0
if re.search("x1\s*\=\s*\(\-b\s*\+\s*math\.sqrt\(b\*\*2\s*\-\s*4\*a\*c\)\)\/\(2\*a\)", code):
code_count = code_count + 1
else:
print("You should include x1 = (-b + math.sqrt(b**2 - 4*a*c))/(2*a)")
if re.search("x2\s*\=\s*\(\-b\s*\-\s*math\.sqrt\(b\*\*2\s*\-\s*4\*a\*c\)\)\/\(2\*a\)", code):
code_count = code_count + 1
else:
print("You should include x2 = (-b - math.sqrt(b**2 - 4*a*c))/(2*a)")
if code_count==2:
print("Code test passed")
print("Go on to the next step")
def step22(code):
import re
print(" ")
if re.search("rows\.append\(\[a\,\s*2\*a\]\)", code):
print("Now change the equation to y = 3x + 2")
elif re.search("rows\.append\(\[a\,\s*3\*a\+2\]\)", code):
print("Code test passed")
print("Go on to the next step")
else:
print("Your code should include rows.append([a, 3*a+2])")