CoCalc -- Collaborative Calculation in the Cloud
Sharedsupport / 2016-10-11-071807-exercise.sagewsOpen in CoCalc

Examples for support purposes...

%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
Interact: please open in CoCalc
%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"

Interact: please open in CoCalc
exercise?
File: /projects/sage/sage-7.3/local/lib/python2.7/site-packages/smc_sagews/sage_salvus.py
Signature : exercise()
Docstring :
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 + ... + %s$ of the first %s positive integers?"%(n,n)
   answer   = n*(n+1)//2

Transpose a matrix:

   %exercise
   title    = r"Transpose a $2 x 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"