︠1979c1e3-8d87-4590-beba-bd8c99b808a5s︠
%hide
%exercise
title = "Sum the first n integers, like Gauss did."
n = randint(3, 100)
question = "What is the sum $1 + 2 + ... + %s$ of the first %s positive integers?"%(n,n)
answer = n*(n+1)//2
︡1fa01d64-0a73-47b8-bd6d-f14aba03ee49︡{"hide":"input"}︡{"interact":{"controls":[{"classes":"btn-large btn-success","control_type":"button","default":" Go ","icon":"fa-refresh","label":"","var":"go","width":null},{"classes":null,"control_type":"text","default":"
Sum the first n integers, like Gauss did.
","label":"title","var":"title"},{"classes":null,"control_type":"text","default":"","label":"times","var":"times"}],"flicker":true,"id":"afd86e87-2418-4e12-bf8a-8b64bce47e1b","layout":[[["go",1,null],["title",11,""]],[["",12,null]],[["times",12,"Times:"]]],"style":"None"}}︡{"done":true}︡
︠79d7090d-6630-4fb0-957c-1ee5d69bec8b︠
%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"
︡1ff20c47-85f8-43da-a973-a446493258d1︡{"interact":{"controls":[{"classes":"btn-large btn-success","control_type":"button","default":" Go ","icon":"fa-refresh","label":"","var":"go","width":null},{"classes":null,"control_type":"text","default":"Add 3 numbers
","label":"title","var":"title"},{"classes":null,"control_type":"text","default":"","label":"times","var":"times"}],"flicker":true,"id":"03c34ff8-8391-4053-b9d7-a91ae3c44892","layout":[[["go",1,null],["title",11,""]],[["",12,null]],[["times",12,"Times:"]]],"style":"None"}}︡{"done":true}︡
︠90d5e06b-a9fc-43db-a840-ce905af64cd0s︠
exercise?
︡b4c2ce1b-7354-4ea0-9fc4-54c0269f8ba8︡{"code":{"filename":null,"lineno":-1,"mode":"text/x-rst","source":"File: /projects/sage/sage-7.3/local/lib/python2.7/site-packages/smc_sagews/sage_salvus.py\nSignature : exercise()\nDocstring :\nUse the %exercise cell decorator to create interactive exercise\nsets. Put %exercise at the top of the cell, then write Sage code\nin the cell that defines the following (all are optional):\n\n* a \"question\" variable, as an HTML string with math in dollar\n signs\n\n* an \"answer\" variable, which can be any object, or a pair\n (correct_value, interact control) -- see the docstring for\n interact for controls.\n\n* an optional callable \"check(answer)\" that returns a boolean or\n a 2-tuple\n\n (True or False, message),\n\n where the first argument is True if the answer is correct, and\n the optional second argument is a message that should be\n displayed in response to the given answer. NOTE: Often the input\n \"answer\" will be a string, so you may have to use Integer,\n RealNumber, or sage_eval to evaluate it, depending on what you\n want to allow the user to do.\n\n* hints -- optional list of strings to display in sequence each\n time the user enters a wrong answer. The last string is\n displayed repeatedly. If hints is omitted, the correct answer is\n displayed after three attempts.\n\nNOTE: The code that defines the exercise is executed so that it\ndoes not impact (and is not impacted by) the global scope of your\nvariables elsewhere in your session. Thus you can have many\n%exercise cells in a single worksheet with no interference between\nthem.\n\nThe following examples further illustrate how %exercise works.\n\nAn exercise to test your ability to sum the first n integers:\n\n %exercise\n title = \"Sum the first n integers, like Gauss did.\"\n n = randint(3, 100)\n question = \"What is the sum $1 + 2 + ... + %s$ of the first %s positive integers?\"%(n,n)\n answer = n*(n+1)//2\n\nTranspose a matrix:\n\n %exercise\n title = r\"Transpose a $2 x 2$ Matrix\"\n A = random_matrix(ZZ,2)\n question = \"What is the transpose of $%s?$\"%latex(A)\n answer = A.transpose()\n\nAdd together a few numbers:\n\n %exercise\n k = randint(2,5)\n title = \"Add %s numbers\"%k\n v = [randint(1,10) for _ in range(k)]\n question = \"What is the sum $%s$?\"%(' + '.join([str(x) for x in v]))\n answer = sum(v)\n\nThe trace of a matrix:\n\n %exercise\n title = \"Compute the trace of a matrix.\"\n A = random_matrix(ZZ, 3, x=-5, y = 5)^2\n question = \"What is the trace of $$%s?$$\"%latex(A)\n answer = A.trace()\n\nSome basic arithmetic with hints and dynamic feedback:\n\n %exercise\n k = randint(2,5)\n title = \"Add %s numbers\"%k\n v = [randint(1,10) for _ in range(k)]\n question = \"What is the sum $%s$?\"%(' + '.join([str(x) for x in v]))\n answer = sum(v)\n hints = ['This is basic arithmetic.', 'The sum is near %s.'%(answer+randint(1,5)), \"The answer is %s.\"%answer]\n def check(attempt):\n c = Integer(attempt) - answer\n if c == 0:\n return True\n if abs(c) >= 10:\n return False, \"Gees -- not even close!\"\n if c < 0:\n return False, \"too low\"\n if c > 0:\n return False, \"too high\""}}︡{"done":true}︡
︠c2d414ca-436e-43b5-8d74-70d5307d71a0︠