-
Notifications
You must be signed in to change notification settings - Fork 2
/
PyClz00.py
99 lines (78 loc) · 2.85 KB
/
PyClz00.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 28 10:38:03 2021
@author: cghiaus
Creates PyClz00.xml file for the following problem in which
the input data is between * *
the embedded answers are between { }:
-------------------------------------------------------------------------------
John Smith has 3 children: *name_1*, age *age_1*, *name_2*, age *age_2*,
*name_3*, age *age_3*.
The mean age of the children is μ ={:NUMERICAL:}.
The eldest child is {:MULTICHOICE:}.
The child with the longest name is {:SHORTANSWER:}.
-------------------------------------------------------------------------------
The input data for the problem:
name = np.array([['Antoinette', 'Lawrence', 'Sebastian'],
['Anastasia', 'Catherine', 'Quentin']])
age = np.array([[10.2, 12.9, 9.7],
[23.4, 12.5, 17.2],
[4.9, 14.1, 8.5]])
To see the question: import PyClz00.xml in Moodle.
To see the .xml file: open PyClz00.xml in a browser.
"""
# 1. Import modules
import numpy as np
import MdlClz
# 2. Create a function which solves the problem
def problem_fun(x):
"""
Function solving the problem
x : dict
inputs: data for the quiz
Returns
y : dict
outputs: embedded answers in the quiz
"""
mean_age = x['age'].mean()
oldest = len(x['age']) * [""] # list of 3 ""
oldest[np.argmax(x['age'])] = "=" # "=" to the oldest
names_length = sorted(x['name'], key=len)
longest_name = names_length[-1]
y = {'mean_age': mean_age,
'oldest': oldest,
'longest_name': longest_name}
return y
# 3. Define the input space
x_ranges = {'name': np.array([['Antoinette', 'Lawrence', 'Sebastian'],
['Anastasia', 'Catherine', 'Quentin']]),
'age': np.array([[10.2, 12.9, 9.7],
[23.4, 12.5, 17.2],
[4.9, 14.1, 8.5]])}
# 4. Write the text of the cloze question in Markdown
text = """
John Smith has 3 children:
{name[0]:s}, age {age[0]:.1f},
{name[1]:s}, age {age[1]:.1f},
{name[2]:s}, age {age[2]:.1f}.
The mean age of the children is
$$\\mu$$ = {{1:NUMERICAL:={mean_age:.1f}:0.5}} (±0.5).
The eldest child is :
{{1:MULTICHOICE:{oldest[0]:s} {name[0]:s}
~{oldest[1]:s} {name[1]:s}
~{oldest[2]:s} {name[2]:s}}}.
The child with the longest name is {{1:SHORTANSWER:~={longest_name:s}}}.
"""
# 5. Generate the quiz in Moodle - cloze format and save .xml file
question_name = "PyClz00" # will be followed by the question number
quiz = MdlClz.generate_quiz(question_name, problem_fun, x_ranges, text)
# 6. Show the inputs and outputs of all tests
test_nr = 0
for x in MdlClz.cprod(x_ranges):
print("Test: ", test_nr)
print("Inputs:")
print(x)
print("Outputs:")
print(problem_fun(x), "\n")
test_nr += 1