In [3]:
%pylab inline
from moodle import *
In [8]:
import random
random.seed(123)
parameters = [random.randint(100,1000) for i in range(10)]
Write the function, that generates the text of the question. You can use the following syntax to add different inputs to
question string q:
q = q + str(x)q = q + str(1+2*x)q = q + (num_q(answer, precision)) We use <a name="data.csv" href="data:text/csv;base64,...> tag to embed arbitrary file.
In [9]:
import io
import base64
import numpy
import random
def question_text(parameter):
seed = parameter # parameter contains random seed
random.seed(seed)
mu = random.randint(10,100)
sigma = random.randint(10,50)
sequence = [random.gauss(mu,sigma) for x in range(random.randint(5000,6000))]
string = "\n".join(["%f"% x for x in sequence])
q = random.randint(1,99)
data = base64.b64encode(string.encode('ascii')).decode()
perc = numpy.percentile(sequence, q)
q_text = """<p>Find %d-th percentile for data in the file
<a name="data.csv" href="data:text/csv;base64,%s" />data.csv</a>.</p>
What is %d-th percentile? %s
""" % (q,data,q,num_q(perc,0.1))
return q_text
In [10]:
# display the first question
from IPython.display import HTML
HTML(question_text(123))
Out[10]:
In [11]:
# Write the questions to a file
name = "percentiles"
questions = []
for param in parameters:
b = question_text(param)
questions.append(b)
file = open(name + ".xml","w",encoding="utf8")
moodle_xml(name,questions,cloze_question,category = 'precentile/', iostream = file)
file.close()
print("Questions were saved in " + name + ".xml, that can be imported into Moodle")