-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
exercise.py
274 lines (230 loc) · 10.2 KB
/
exercise.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
274
from interact_sagecell import interact,Button,HtmlBox, UpdateButton
#################
# The following code is from https://github.com/sagemath/cloud/blob/master/sage_salvus.py
# copyright William Stein, distributed under the GPL v2+
# it doesn't quite work yet.
##########################################################
# A "%exercise" cell mode -- a first step toward
# automated homework.
##########################################################
class Exercise:
def __init__(self, question, answer, check=None, hints=None):
import sage.all, sage.matrix.all
if not (isinstance(answer, (tuple, list)) and len(answer) == 2):
if sage.matrix.all.is_Matrix(answer):
default = sage.all.parent(answer)(0)
else:
default = ''
answer = [answer, default]
if check is None:
R = sage.all.parent(answer[0])
def check(attempt):
return R(attempt) == answer[0]
if hints is None:
hints = ['','','',"The answer is %s."%answer[0]]
self._question = question
self._answer = answer
self._check = check
self._hints = hints
def _check_attempt(self, attempt):
from sage.misc.all import walltime
response = "<div class='well'>"
correct=False
try:
r = self._check(attempt)
if isinstance(r, tuple) and len(r)==2:
correct = r[0]
comment = r[1]
else:
correct = bool(r)
comment = ''
except TypeError as msg:
response += "<h3 style='color:darkgreen'>Huh? -- %s (attempt=%s)</h3>"%(msg, attempt)
else:
if correct:
response += "<h1 style='color:blue'>RIGHT!</h1>"
if self._start_time:
response += "<h2 class='lighten'>Time: %.1f seconds</h2>"%(walltime()-self._start_time,)
if self._number_of_attempts == 1:
response += "<h3 class='lighten'>You got it first try!</h3>"
else:
response += "<h3 class='lighten'>It took you %s attempts.</h3>"%(self._number_of_attempts,)
else:
response += "<h3 style='color:darkgreen'>Not correct yet...</h3>"
if self._number_of_attempts == 1:
response += "<h4 style='lighten'>(first attempt)</h4>"
else:
response += "<h4 style='lighten'>(%s attempts)</h4>"%self._number_of_attempts
if self._number_of_attempts > len(self._hints):
hint = self._hints[-1]
else:
hint = self._hints[self._number_of_attempts-1]
if hint:
response += "<span class='lighten'>(HINT: %s)</span>"%(hint,)
if comment:
response += '<h4>%s</h4>'%comment
response += "</div>"
#interact.feedback = response#HtmlBox(response,label='')
return correct, response
def ask(self, cb):
from sage.misc.all import walltime
self._start_time = walltime()
self._number_of_attempts = 0
attempts = []
@interact(layout=[[('question',12)],[('attempt',12)], [('submit',12)],[('feedback',12)]])
def f(fself, question = ("Question:", HtmlBox(self._question)),
attempt = ('Answer:',self._answer[1]),
submit = UpdateButton('Submit'),
feedback = HtmlBox('')):
if 'attempt' in fself._changed and attempt != '':
attempts.append(attempt)
if self._start_time == 0:
self._start_time = walltime()
self._number_of_attempts += 1
correct, fself.feedback = self._check_attempt(attempt)
if correct:
cb({'attempts':attempts, 'time':walltime()-self._start_time})
def exercise(code):
r"""
Use the %exercise cell decorator to create interactive exercise
sets. Put %exercise at the top of the cell, then write Sage code
in the cell that defines the following (all are optional):
- a ``question`` variable, as an HTML string with math in dollar
signs
- an ``answer`` variable, which can be any object, or a pair
(correct_value, interact control) -- see the docstring for
interact for controls.
- an optional callable ``check(answer)`` that returns a boolean or
a 2-tuple
(True or False, message),
where the first argument is True if the answer is correct, and
the optional second argument is a message that should be
displayed in response to the given answer. NOTE: Often the
input "answer" will be a string, so you may have to use Integer,
RealNumber, or sage_eval to evaluate it, depending
on what you want to allow the user to do.
- hints -- optional list of strings to display in sequence each
time the user enters a wrong answer. The last string is
displayed repeatedly. If hints is omitted, the correct answer
is displayed after three attempts.
NOTE: The code that defines the exercise is executed so that it
does not impact (and is not impacted by) the global scope of your
variables elsewhere in your session. Thus you can have many
%exercise cells in a single worksheet with no interference between
them.
The following examples further illustrate how %exercise works.
An exercise to test your ability to sum the first $n$ integers::
%exercise
title = "Sum the first n integers, like Gauss did."
n = randint(3, 100)
question = "What is the sum $1 + 2 + \\cdots + %s$ of the first %s positive integers?"%(n,n)
answer = n*(n+1)//2
Transpose a matrix::
%exercise
title = r"Transpose a $2 \times 2$ Matrix"
A = random_matrix(ZZ,2)
question = "What is the transpose of $%s?$"%latex(A)
answer = A.transpose()
Add together a few numbers::
%exercise
k = randint(2,5)
title = "Add %s numbers"%k
v = [randint(1,10) for _ in range(k)]
question = "What is the sum $%s$?"%(' + '.join([str(x) for x in v]))
answer = sum(v)
The trace of a matrix::
%exercise
title = "Compute the trace of a matrix."
A = random_matrix(ZZ, 3, x=-5, y = 5)^2
question = "What is the trace of $$%s?$$"%latex(A)
answer = A.trace()
Some basic arithmetic with hints and dynamic feedback::
%exercise
k = randint(2,5)
title = "Add %s numbers"%k
v = [randint(1,10) for _ in range(k)]
question = "What is the sum $%s$?"%(' + '.join([str(x) for x in v]))
answer = sum(v)
hints = ['This is basic arithmetic.', 'The sum is near %s.'%(answer+randint(1,5)), "The answer is %s."%answer]
def check(attempt):
c = Integer(attempt) - answer
if c == 0:
return True
if abs(c) >= 10:
return False, "Gees -- not even close!"
if c < 0:
return False, "too low"
if c > 0:
return False, "too high"
Another example::
%exercise
title = r""
rank = randint(2,4)
A = random_matrix(QQ,5,algorithm='echelonizable', rank=rank,upper_bound=10)
kernel = A.T.kernel()
question = "Find a basis for the nullspace of $%s$. Your answer should be a list of vectors (e.g., '[(1,2,3), (3,2,1)]' )"%latex(A)
def check(a):
try:
a = sage_eval(a)
i = [vector(QQ,j) for j in a]
except:
return False, "There was an error parsing your answer. Your answer should be a list of vectors (e.g., '[(1,2,3), (3,2,1)]' )."
v = span(i)
if v.dimension()!=len(i):
return False, "Are your vectors linearly independent?"
elif v != kernel:
return False, "You are missing some vectors"
else:
return True, "Great job!"
hints = ["The RREF is $%s$."%latex(A.rref())]
hints.append(" ".join(hints)+" The nullity is %d."%kernel.dimension())
"""
f = closure(code)
def g():
x = f()
return x.get('title',''), x.get('question', ''), x.get('answer',''), x.get('check',None), x.get('hints',None)
title, question, answer, check, hints = g()
obj = {}
obj['E'] = Exercise(question, answer, check, hints)
obj['title'] = title
title_html = '<h3>%s</h3>'
the_times = []
@interact(layout=[[('go',1), ('title',11,'')],[('_output')], [('times',12, "<b>Times:</b>")]])#, flicker=True)
def h(self, go = Button(text="New Question", label=''),
title = HtmlBox(title_html%title),
times = HtmlBox('No times')):
c = self._changed
if 'go' in c or 'another' in c:
self.title = title
def cb(obj):
the_times.append("%.1f"%obj['time'])
h.times = ', '.join(the_times)
obj['E'].ask(cb)
title, question, answer, check, hints = g() # get ready for next time.
#print title, question, answer, check, hints
obj['title'] = title_html%title
obj['E'] = Exercise(question, answer, check, hints)
def closure(code):
"""
Wrap the given code block (a string) in a closure, i.e., a
function with an obfuscated random name.
When called, the function returns locals().
"""
import uuid
# TODO: strip string literals first
code = ' ' + ('\n '.join(code.splitlines()))
fname = "__" + str(uuid.uuid4()).replace('-','_')
closure = "def %s():\n%s\n return locals()"%(fname, code)
class Closure:
def __call__(self):
return self._f()
c = Closure()
get_ipython().run_cell(closure)
import sys
c._f = sys._sage_.namespace[fname]
del sys._sage_.namespace[fname]
return c
#######################
## end salvus code
#######################
imports = {'exercise': exercise}