math-exercises 3.0.184 → 3.0.186
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/exercises/exercise.d.ts +1 -0
- package/lib/exercises/exercise.d.ts.map +1 -1
- package/lib/exercises/math/derivation/derivative/exp/index.d.ts +2 -0
- package/lib/exercises/math/derivation/derivative/exp/index.d.ts.map +1 -1
- package/lib/exercises/math/derivation/derivative/exp/index.js +2 -0
- package/lib/exercises/math/derivation/derivative/index.d.ts +3 -0
- package/lib/exercises/math/derivation/derivative/index.d.ts.map +1 -1
- package/lib/exercises/math/derivation/derivative/index.js +3 -0
- package/lib/exercises/math/derivation/derivativeNumber/index.d.ts +2 -0
- package/lib/exercises/math/derivation/derivativeNumber/index.d.ts.map +1 -1
- package/lib/exercises/math/derivation/derivativeNumber/index.js +2 -0
- package/lib/exercises/math/derivation/index.d.ts +1 -0
- package/lib/exercises/math/derivation/index.d.ts.map +1 -1
- package/lib/exercises/math/derivation/index.js +1 -0
- package/lib/exercises/math/derivation/tangent/index.d.ts +9 -0
- package/lib/exercises/math/derivation/tangent/index.d.ts.map +1 -1
- package/lib/exercises/math/derivation/tangent/index.js +9 -0
- package/lib/exercises/math/derivation/variations/index.d.ts +8 -0
- package/lib/exercises/math/derivation/variations/index.d.ts.map +1 -1
- package/lib/exercises/math/derivation/variations/index.js +8 -0
- package/lib/exercises/math/derivation/variations/signVarTable3dDegree.d.ts +10 -0
- package/lib/exercises/math/derivation/variations/signVarTable3dDegree.d.ts.map +1 -1
- package/lib/exercises/math/derivation/variations/signVarTable3dDegree.js +235 -231
- package/lib/exercises/math/functions/exponential/algebraic/approxExpAPlusB.d.ts.map +1 -1
- package/lib/exercises/math/functions/exponential/algebraic/approxExpAPlusB.js +91 -38
- package/lib/exercises/math/probaStat/trees/index.d.ts +1 -1
- package/lib/exercises/math/probaStat/trees/index.d.ts.map +1 -1
- package/lib/exercises/math/probaStat/trees/index.js +1 -3
- package/lib/exercises/math/probaStat/trees/probabilityTree.d.ts +9 -3
- package/lib/exercises/math/probaStat/trees/probabilityTree.d.ts.map +1 -1
- package/lib/exercises/math/probaStat/trees/probabilityTree.js +419 -45
- package/lib/exercises/math/python/index.d.ts +1 -0
- package/lib/exercises/math/python/index.d.ts.map +1 -1
- package/lib/exercises/math/python/index.js +1 -0
- package/lib/exercises/math/python/list/index.d.ts +11 -0
- package/lib/exercises/math/python/list/index.d.ts.map +1 -0
- package/lib/exercises/math/python/list/index.js +10 -0
- package/lib/exercises/math/python/list/pyListAppend.d.ts +29 -0
- package/lib/exercises/math/python/list/pyListAppend.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListAppend.js +280 -0
- package/lib/exercises/math/python/list/pyListCreateListByComprehension.d.ts +11 -0
- package/lib/exercises/math/python/list/pyListCreateListByComprehension.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListCreateListByComprehension.js +231 -0
- package/lib/exercises/math/python/list/pyListCreateListInstructionCompletion.d.ts +9 -0
- package/lib/exercises/math/python/list/pyListCreateListInstructionCompletion.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListCreateListInstructionCompletion.js +232 -0
- package/lib/exercises/math/python/list/pyListElemAtIndex.d.ts +9 -0
- package/lib/exercises/math/python/list/pyListElemAtIndex.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListElemAtIndex.js +292 -0
- package/lib/exercises/math/python/list/pyListMutationMix.d.ts +29 -0
- package/lib/exercises/math/python/list/pyListMutationMix.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListMutationMix.js +455 -0
- package/lib/exercises/math/python/list/pyListRemove.d.ts +29 -0
- package/lib/exercises/math/python/list/pyListRemove.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListRemove.js +375 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndex.d.ts +29 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndex.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndex.js +331 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndexInstructionCompletion.d.ts +9 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndexInstructionCompletion.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndexInstructionCompletion.js +346 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndexWaterfall.d.ts +29 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndexWaterfall.d.ts.map +1 -0
- package/lib/exercises/math/python/list/pyListSetElemAtIndexWaterfall.js +394 -0
- package/lib/exercises/math/sequences/arithmetic/situations/arithmeticFindRankFromSituation.d.ts.map +1 -1
- package/lib/exercises/math/sequences/arithmetic/situations/arithmeticFindRankFromSituation.js +3 -3
- package/lib/exercises/math/sequences/arithmetic/situations/arithmeticFindThresholdFromSituation.d.ts.map +1 -1
- package/lib/exercises/math/sequences/arithmetic/situations/arithmeticFindThresholdFromSituation.js +2 -2
- package/lib/exercises/math/sequences/arithmetic/situations/index.d.ts +2 -0
- package/lib/exercises/math/sequences/arithmetic/situations/index.d.ts.map +1 -1
- package/lib/exercises/math/sequences/arithmetic/situations/index.js +2 -2
- package/lib/geogebra/geogebraConstructor.d.ts +1 -0
- package/lib/geogebra/geogebraConstructor.d.ts.map +1 -1
- package/lib/geogebra/geogebraConstructor.js +3 -0
- package/lib/index.d.ts +293 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/math/geometry/line.js +1 -1
- package/lib/math/utils/sequences/situations/seqArithmeticSituations.d.ts +2 -0
- package/lib/math/utils/sequences/situations/seqArithmeticSituations.d.ts.map +1 -1
- package/lib/math/utils/sequences/situations/seqArithmeticSituations.js +150 -128
- package/lib/math/utils/sequences/situations/seqArithmeticUtils.d.ts +1 -1
- package/lib/math/utils/sequences/situations/seqArithmeticUtils.d.ts.map +1 -1
- package/lib/math/utils/sequences/situations/seqArithmeticUtils.js +4 -1
- package/lib/tests/questionTest.d.ts.map +1 -1
- package/lib/tests/questionTest.js +1 -0
- package/lib/tree/nodes/functions/sqrtNode.d.ts +1 -1
- package/lib/tree/nodes/functions/sqrtNode.d.ts.map +1 -1
- package/lib/tree/nodes/functions/sqrtNode.js +5 -3
- package/package.json +1 -1
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { addValidProp, propWhile, shuffleProps, tryToAddWrongProp, } from "../../../../exercises/exercise.js";
|
|
2
|
+
import { getDistinctQuestions } from "../../../../exercises/utils/getDistinctQuestions.js";
|
|
3
|
+
import { randint } from "../../../../math/utils/random/randint.js";
|
|
4
|
+
import { coinFlip } from "../../../../utils/alea/coinFlip.js";
|
|
5
|
+
import { random, randomMany } from "../../../../utils/alea/random.js";
|
|
6
|
+
import { zip } from "../../../../utils/arrays/arrayZip.js";
|
|
7
|
+
import { mdTable } from "../../../../utils/markdown/mdTable.js";
|
|
8
|
+
const getStrPythonInstruction = (instruction) => {
|
|
9
|
+
const { nameList, value } = instruction;
|
|
10
|
+
switch (instruction.type) {
|
|
11
|
+
case "append":
|
|
12
|
+
return `${nameList}.append(${strPythonForListElem(value)})`;
|
|
13
|
+
case "remove":
|
|
14
|
+
return `${nameList}.remove(${strPythonForListElem(value)})`;
|
|
15
|
+
case "inject": {
|
|
16
|
+
const { index } = instruction;
|
|
17
|
+
return `${nameList}[${index}] = ${strPythonForListElem(value)}`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const codify = (strPython) => {
|
|
22
|
+
return `\`\`\`python
|
|
23
|
+
${strPython}
|
|
24
|
+
\`\`\``;
|
|
25
|
+
};
|
|
26
|
+
const codifyChunk = (strPython) => {
|
|
27
|
+
return `\`${strPython}\``;
|
|
28
|
+
};
|
|
29
|
+
const strPythonForList = (list) => {
|
|
30
|
+
return `[${list.map((elem) => strPythonForListElem(elem)).join(", ")}]`;
|
|
31
|
+
};
|
|
32
|
+
const strPythonForListElem = (elem) => {
|
|
33
|
+
if (typeof elem === "string") {
|
|
34
|
+
if (elem === "None") {
|
|
35
|
+
return `${elem}`;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return `'${elem}'`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return `${elem}`;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const getStrPythonProgram = (identifiers) => {
|
|
46
|
+
const { nameList, listInitial, arrInstruction } = identifiers;
|
|
47
|
+
return `${nameList} = ${strPythonForList(listInitial)}
|
|
48
|
+
${arrInstruction.map(getStrPythonInstruction).join(`
|
|
49
|
+
|
|
50
|
+
`)}
|
|
51
|
+
print(${nameList})
|
|
52
|
+
`;
|
|
53
|
+
};
|
|
54
|
+
const getFunctionPythonInstruction = (instruction) => {
|
|
55
|
+
return (l) => {
|
|
56
|
+
const { nameList, value } = instruction;
|
|
57
|
+
switch (instruction.type) {
|
|
58
|
+
case "append":
|
|
59
|
+
{
|
|
60
|
+
const lWork = [...l];
|
|
61
|
+
lWork.push(value);
|
|
62
|
+
return lWork;
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
case "remove":
|
|
66
|
+
{
|
|
67
|
+
const lWork = [...l];
|
|
68
|
+
const indexFound = lWork.findIndex((v) => v === value);
|
|
69
|
+
if (indexFound === -1) {
|
|
70
|
+
throw new Error(`ValueError: ${nameList}.remove(x): x not in list`);
|
|
71
|
+
}
|
|
72
|
+
lWork.splice(indexFound, 1);
|
|
73
|
+
return lWork;
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
case "inject":
|
|
77
|
+
{
|
|
78
|
+
const { index } = instruction;
|
|
79
|
+
if (index < -l.length || l.length <= index) {
|
|
80
|
+
throw new Error(`IndexError: list assignment index out of range`);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const lWork = [...l];
|
|
84
|
+
const indexPositive = index >= 0 ? index : l.length + index;
|
|
85
|
+
lWork[indexPositive] = value;
|
|
86
|
+
return lWork;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const getFunctionForPythonProgram = (identifiers) => {
|
|
94
|
+
const { nameList, listInitial, arrInstruction } = identifiers;
|
|
95
|
+
return (..._otherArgs) => {
|
|
96
|
+
const outDict = {};
|
|
97
|
+
//init
|
|
98
|
+
let list = listInitial;
|
|
99
|
+
outDict[0] = {
|
|
100
|
+
[nameList]: list,
|
|
101
|
+
};
|
|
102
|
+
//main
|
|
103
|
+
{
|
|
104
|
+
arrInstruction.forEach((instruction, i) => {
|
|
105
|
+
let err;
|
|
106
|
+
try {
|
|
107
|
+
list = getFunctionPythonInstruction(instruction)(list);
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
err = e;
|
|
111
|
+
}
|
|
112
|
+
outDict[i + 1] = {
|
|
113
|
+
[nameList]: list,
|
|
114
|
+
err,
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
//convenience: store last step in -1
|
|
119
|
+
outDict[-1] = outDict[Math.max(...Object.keys(outDict).map((k) => +k))];
|
|
120
|
+
return outDict;
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
const getInstruction = (identifiers) => {
|
|
124
|
+
return `Qu'affichera le script en Python suivant ?
|
|
125
|
+
|
|
126
|
+
${codify(getStrPythonProgram(identifiers))}
|
|
127
|
+
|
|
128
|
+
`;
|
|
129
|
+
};
|
|
130
|
+
const getAnswer = (identifiers) => {
|
|
131
|
+
const { nameList } = identifiers;
|
|
132
|
+
const stepDictEnd = getFunctionForPythonProgram(identifiers)()[-1];
|
|
133
|
+
if (stepDictEnd.err) {
|
|
134
|
+
return `${stepDictEnd.err.message}`;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
return strPythonForList(stepDictEnd[nameList]);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const getHint = (identifiers) => {
|
|
141
|
+
const { nameList, arrInstruction } = identifiers;
|
|
142
|
+
const [instructionInject] = arrInstruction;
|
|
143
|
+
const { index } = instructionInject;
|
|
144
|
+
return `Pour affecter la valeur $\\text{'toto'}$ à la variable $nom$, on écrit ${codifyChunk("nom = 'toto'")}.
|
|
145
|
+
|
|
146
|
+
Ici, on veut affecter une valeur à la variable $${nameList}[${index}]$ (si tant est que cet emplacement mémoire existe).`;
|
|
147
|
+
};
|
|
148
|
+
const getCorrection = (identifiers) => {
|
|
149
|
+
const { nameList, arrInstruction } = identifiers;
|
|
150
|
+
const dictSteps = getFunctionForPythonProgram(identifiers)();
|
|
151
|
+
const maxLength = Math.max(...Object.entries(dictSteps).map(([_, { [nameList]: l }]) => l.length));
|
|
152
|
+
const err = dictSteps[-1].err;
|
|
153
|
+
if (err === undefined) {
|
|
154
|
+
return `Voici l'évolution de la liste $${nameList}$ au fil des instructions.
|
|
155
|
+
|
|
156
|
+
${mdTable([
|
|
157
|
+
(() => {
|
|
158
|
+
const l = dictSteps[0][nameList];
|
|
159
|
+
return [
|
|
160
|
+
"\\text{Initialement}",
|
|
161
|
+
...[...Array(maxLength).keys()].map((i) => l.length > i ? `\\text{${l[i]}}` : "\\text{}"),
|
|
162
|
+
];
|
|
163
|
+
})(),
|
|
164
|
+
...arrInstruction.map((instruction, i) => {
|
|
165
|
+
const l = dictSteps[i + 1][nameList];
|
|
166
|
+
return [
|
|
167
|
+
`\\text{Après ${getStrPythonInstruction(instruction)}}`,
|
|
168
|
+
...[...Array(maxLength).keys()].map((i) => l.length > i ? `\\text{${l[i]}}` : "\\text{}"),
|
|
169
|
+
];
|
|
170
|
+
}),
|
|
171
|
+
], true)}
|
|
172
|
+
|
|
173
|
+
Le programme affichera donc :
|
|
174
|
+
|
|
175
|
+
${codify(getAnswer(identifiers))}
|
|
176
|
+
|
|
177
|
+
`;
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
const [instructionInject] = arrInstruction;
|
|
181
|
+
const { index } = instructionInject;
|
|
182
|
+
return `Quand l'instruction ${codifyChunk(getStrPythonInstruction(instructionInject))} va être exécutée,
|
|
183
|
+
|
|
184
|
+
étant donné qu'aucun élément de $${nameList}$ n'existe à l'indice $${index}$
|
|
185
|
+
, l'exécution ne va pas pouvoir s'effectuer correctement.
|
|
186
|
+
|
|
187
|
+
Un message d'erreur va donc s'afficher.`;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
const getPropositions = (n, { answer, ...identifiers }) => {
|
|
191
|
+
const propositions = [];
|
|
192
|
+
addValidProp(propositions, codifyChunk(answer), "raw");
|
|
193
|
+
const { arrInstruction } = identifiers;
|
|
194
|
+
const [instructionInject] = arrInstruction;
|
|
195
|
+
const { index, value } = instructionInject;
|
|
196
|
+
function createAnswerWrong(funcIndexWrong, funcValueWrong = (v) => v) {
|
|
197
|
+
const identifiersWrong = {
|
|
198
|
+
...identifiers,
|
|
199
|
+
arrInstruction: arrInstruction.map((instruction) => {
|
|
200
|
+
if (instruction.type === "inject") {
|
|
201
|
+
return {
|
|
202
|
+
...instruction,
|
|
203
|
+
index: funcIndexWrong(instruction.index),
|
|
204
|
+
value: funcValueWrong(instruction.value),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
return instruction;
|
|
209
|
+
}
|
|
210
|
+
}),
|
|
211
|
+
};
|
|
212
|
+
return getAnswer(identifiersWrong);
|
|
213
|
+
}
|
|
214
|
+
//indexAsked-1
|
|
215
|
+
{
|
|
216
|
+
tryToAddWrongProp(propositions, codifyChunk(createAnswerWrong((index) => index - 1)), "raw");
|
|
217
|
+
}
|
|
218
|
+
//indexAsked+1
|
|
219
|
+
{
|
|
220
|
+
tryToAddWrongProp(propositions, codifyChunk(createAnswerWrong((index) => index + 1)), "raw");
|
|
221
|
+
}
|
|
222
|
+
//student: lst[index]=value -> lst[value]=index
|
|
223
|
+
if (typeof value === "number") {
|
|
224
|
+
tryToAddWrongProp(propositions, codifyChunk(createAnswerWrong(() => value, () => index)), "raw");
|
|
225
|
+
}
|
|
226
|
+
//-indexAsked
|
|
227
|
+
{
|
|
228
|
+
tryToAddWrongProp(propositions, codifyChunk(createAnswerWrong((index) => -index)), "raw");
|
|
229
|
+
}
|
|
230
|
+
//append
|
|
231
|
+
{
|
|
232
|
+
const identifiersWrong = {
|
|
233
|
+
...identifiers,
|
|
234
|
+
arrInstruction: arrInstruction.map((instruction) => {
|
|
235
|
+
return {
|
|
236
|
+
...instruction,
|
|
237
|
+
type: "append",
|
|
238
|
+
value: instruction.value,
|
|
239
|
+
};
|
|
240
|
+
}),
|
|
241
|
+
};
|
|
242
|
+
tryToAddWrongProp(propositions, codifyChunk(getAnswer(identifiersWrong)), "raw");
|
|
243
|
+
}
|
|
244
|
+
//filler
|
|
245
|
+
{
|
|
246
|
+
propWhile(propositions, n, () => {
|
|
247
|
+
const identifiersWrong = createRandomIdentifiers();
|
|
248
|
+
tryToAddWrongProp(propositions, codifyChunk(getAnswer(identifiersWrong)), "raw");
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return shuffleProps(propositions, n);
|
|
252
|
+
};
|
|
253
|
+
const createRandomIdentifiers = () => {
|
|
254
|
+
const poolNameList = ["l", "lst", "liste"];
|
|
255
|
+
const nameList = random(poolNameList);
|
|
256
|
+
function createRandomListInitial(minLength, maxLength) {
|
|
257
|
+
const length = randint(minLength, maxLength + 1);
|
|
258
|
+
const poolNumber = [...Array(61).keys()].map((i) => i - 30);
|
|
259
|
+
const subPoolNumber = randomMany(poolNumber, Math.max(Math.floor(length * 1.5), length + 2));
|
|
260
|
+
const listNumber = [...Array(length).keys()].map(() => random(subPoolNumber));
|
|
261
|
+
const poolLetter = "abcdefghijklmnopqrstuvwxyz".split("");
|
|
262
|
+
const subPoolLetters = randomMany(poolLetter, Math.max(Math.floor(length * 1.5), length + 2));
|
|
263
|
+
const listLetters = [...Array(length).keys()].map(() => random(subPoolLetters));
|
|
264
|
+
const rand = randint(0, 4);
|
|
265
|
+
switch (rand) {
|
|
266
|
+
case 0:
|
|
267
|
+
return listNumber;
|
|
268
|
+
case 1:
|
|
269
|
+
return listLetters;
|
|
270
|
+
case 2: {
|
|
271
|
+
return [...Array(length).keys()].map(() => {
|
|
272
|
+
const word = [...Array(randint(1, 4)).keys()]
|
|
273
|
+
.map(() => random(poolLetter))
|
|
274
|
+
.join("");
|
|
275
|
+
return word;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
case 3:
|
|
279
|
+
return zip(listNumber, listLetters).map(([n, letter]) => random([n, letter]));
|
|
280
|
+
default:
|
|
281
|
+
throw new Error("Unsupported rand: " + rand);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const [valueEx, ...listInitial] = createRandomListInitial(3, 7);
|
|
285
|
+
const arrInstruction = [
|
|
286
|
+
(() => {
|
|
287
|
+
const index = coinFlip()
|
|
288
|
+
? randint(0, listInitial.length)
|
|
289
|
+
: randint(-listInitial.length, 0);
|
|
290
|
+
const instruction = {
|
|
291
|
+
type: "inject",
|
|
292
|
+
nameList,
|
|
293
|
+
value: valueEx,
|
|
294
|
+
index,
|
|
295
|
+
};
|
|
296
|
+
return instruction;
|
|
297
|
+
})(),
|
|
298
|
+
];
|
|
299
|
+
const identifiers = { nameList, listInitial, arrInstruction };
|
|
300
|
+
return identifiers;
|
|
301
|
+
};
|
|
302
|
+
const getPyListSetElemAtIndexQuestion = () => {
|
|
303
|
+
const identifiers = createRandomIdentifiers();
|
|
304
|
+
return getQuestionFromIdentifiers(identifiers);
|
|
305
|
+
};
|
|
306
|
+
const getQuestionFromIdentifiers = (identifiers, opts) => {
|
|
307
|
+
const question = {
|
|
308
|
+
answer: getAnswer(identifiers, opts),
|
|
309
|
+
instruction: getInstruction(identifiers, opts),
|
|
310
|
+
keys: [],
|
|
311
|
+
answerFormat: "tex",
|
|
312
|
+
identifiers,
|
|
313
|
+
hint: getHint(identifiers, opts),
|
|
314
|
+
correction: getCorrection(identifiers, opts),
|
|
315
|
+
options: opts,
|
|
316
|
+
};
|
|
317
|
+
return question;
|
|
318
|
+
};
|
|
319
|
+
export const pyListSetElemAtIndex = {
|
|
320
|
+
id: "pyListSetElemAtIndex",
|
|
321
|
+
label: "Affecter une valeur à un élément dans une liste en Python",
|
|
322
|
+
isSingleStep: true,
|
|
323
|
+
generator: (nb, opts) => getDistinctQuestions(() => getPyListSetElemAtIndexQuestion(opts), nb),
|
|
324
|
+
qcmTimer: 60,
|
|
325
|
+
freeTimer: 60,
|
|
326
|
+
getPropositions,
|
|
327
|
+
subject: "Mathématiques",
|
|
328
|
+
getQuestionFromIdentifiers,
|
|
329
|
+
hasHintAndCorrection: true,
|
|
330
|
+
answerType: "QCU",
|
|
331
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Exercise } from "../../../../exercises/exercise.js";
|
|
2
|
+
type Identifiers = {
|
|
3
|
+
nameList: string;
|
|
4
|
+
listInitial: unknown[];
|
|
5
|
+
listTarget: unknown[];
|
|
6
|
+
};
|
|
7
|
+
export declare const pyListSetElemAtIndexInstructionCompletion: Exercise<Identifiers>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=pyListSetElemAtIndexInstructionCompletion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pyListSetElemAtIndexInstructionCompletion.d.ts","sourceRoot":"","sources":["../../../../../src/exercises/math/python/list/pyListSetElemAtIndexInstructionCompletion.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EAYT,MAAM,6BAA6B,CAAC;AA6DrC,KAAK,WAAW,GAAG;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,OAAO,EAAE,CAAC;IACvB,UAAU,EAAE,OAAO,EAAE,CAAC;CACvB,CAAC;AAkYF,eAAO,MAAM,yCAAyC,EAAE,QAAQ,CAAC,WAAW,CAmBzE,CAAC"}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { addValidProp, shuffleProps, tryToAddWrongProp, } from "../../../../exercises/exercise.js";
|
|
2
|
+
import { getDistinctQuestions } from "../../../../exercises/utils/getDistinctQuestions.js";
|
|
3
|
+
import { randint } from "../../../../math/utils/random/randint.js";
|
|
4
|
+
import { coinFlip } from "../../../../utils/alea/coinFlip.js";
|
|
5
|
+
import { probaLawFlip } from "../../../../utils/alea/probaLawFlip.js";
|
|
6
|
+
import { random } from "../../../../utils/alea/random.js";
|
|
7
|
+
import { zip } from "../../../../utils/arrays/arrayZip.js";
|
|
8
|
+
import { mdTable } from "../../../../utils/markdown/mdTable.js";
|
|
9
|
+
const getStrPythonInstruction = (instruction) => {
|
|
10
|
+
const { nameList, value } = instruction;
|
|
11
|
+
switch (instruction.type) {
|
|
12
|
+
case "inject": {
|
|
13
|
+
const { index } = instruction;
|
|
14
|
+
return `${nameList}[${index}] = ${strPythonForListElem(value)}`;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const codify = (strPython) => {
|
|
19
|
+
return `\`\`\`python
|
|
20
|
+
${strPython}
|
|
21
|
+
\`\`\``;
|
|
22
|
+
};
|
|
23
|
+
const codifyChunk = (strPython) => {
|
|
24
|
+
return `\`${strPython}\``;
|
|
25
|
+
};
|
|
26
|
+
const strPythonForList = (list) => {
|
|
27
|
+
return `[${list.map((elem) => strPythonForListElem(elem)).join(", ")}]`;
|
|
28
|
+
};
|
|
29
|
+
const strPythonForListElem = (elem) => {
|
|
30
|
+
if (typeof elem === "string") {
|
|
31
|
+
if (elem === "None") {
|
|
32
|
+
return `${elem}`;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
return `'${elem}'`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return `${elem}`;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const getStrPythonProgram = (identifiers) => {
|
|
43
|
+
const { nameList, listInitial } = identifiers;
|
|
44
|
+
return `${nameList} = ${strPythonForList(listInitial)}
|
|
45
|
+
...
|
|
46
|
+
print(${nameList})
|
|
47
|
+
`;
|
|
48
|
+
};
|
|
49
|
+
const getInstruction = (identifiers) => {
|
|
50
|
+
const { listTarget } = identifiers;
|
|
51
|
+
return `Par quelle(s) instruction(s) peut-on remplacer les pointillés pour que le programme Python ci-dessous affiche ${codifyChunk(strPythonForList(listTarget))} ?
|
|
52
|
+
|
|
53
|
+
${codify(getStrPythonProgram(identifiers))}
|
|
54
|
+
|
|
55
|
+
`;
|
|
56
|
+
};
|
|
57
|
+
const getHint = () => {
|
|
58
|
+
return `Détermine à quel indice dans la liste doit s'opérer la mutation.
|
|
59
|
+
|
|
60
|
+
L'instruction valide doit affecter la bonne valeur au bon indice.`;
|
|
61
|
+
};
|
|
62
|
+
const getCorrection = (identifiers) => {
|
|
63
|
+
const { nameList, listInitial, listTarget } = identifiers;
|
|
64
|
+
const [_valueInitial, valueTarget, indexTarget] = zip(listInitial, listTarget)
|
|
65
|
+
.map(([v1, v2], i) => [v1, v2, i])
|
|
66
|
+
.find(([v1, v2]) => v1 !== v2);
|
|
67
|
+
const instructionInjectAtPositiveIndex = {
|
|
68
|
+
nameList,
|
|
69
|
+
type: "inject",
|
|
70
|
+
value: valueTarget,
|
|
71
|
+
index: indexTarget,
|
|
72
|
+
};
|
|
73
|
+
const instructionInjectAtNegativeIndex = {
|
|
74
|
+
nameList,
|
|
75
|
+
type: "inject",
|
|
76
|
+
value: valueTarget,
|
|
77
|
+
index: indexTarget - listInitial.length,
|
|
78
|
+
};
|
|
79
|
+
return `Voici un tableau récapitulatif :
|
|
80
|
+
|
|
81
|
+
${mdTable([
|
|
82
|
+
["\\text{Indices}", ...listInitial.map((_, i) => `${i}`)],
|
|
83
|
+
[
|
|
84
|
+
"\\text{Avant la mutation}",
|
|
85
|
+
...listInitial.map((v) => `\\text{${strPythonForListElem(v)}}`),
|
|
86
|
+
],
|
|
87
|
+
[
|
|
88
|
+
"\\text{Après la mutation}",
|
|
89
|
+
...listTarget.map((v) => `\\text{${strPythonForListElem(v)}}`),
|
|
90
|
+
],
|
|
91
|
+
], true)}
|
|
92
|
+
|
|
93
|
+
L'indice où l'on veut affecter la nouvelle valeur (${codifyChunk(strPythonForListElem(valueTarget))}) pour la liste nommée $${nameList}$ est donc $${indexTarget}$.
|
|
94
|
+
|
|
95
|
+
Les instructions qui conviennent sont :
|
|
96
|
+
|
|
97
|
+
${codify(getStrPythonInstruction(instructionInjectAtPositiveIndex))}
|
|
98
|
+
|
|
99
|
+
et
|
|
100
|
+
|
|
101
|
+
${codify(getStrPythonInstruction(instructionInjectAtNegativeIndex))}
|
|
102
|
+
|
|
103
|
+
`;
|
|
104
|
+
};
|
|
105
|
+
const getPropositions = (n, { answer, ...identifiers }) => {
|
|
106
|
+
const propositions = [];
|
|
107
|
+
const { nameList, listInitial, listTarget } = identifiers;
|
|
108
|
+
const [valueInitial, valueTarget, indexTarget] = zip(listInitial, listTarget)
|
|
109
|
+
.map(([v1, v2], i) => [v1, v2, i])
|
|
110
|
+
.find(([v1, v2]) => v1 !== v2);
|
|
111
|
+
const instructionInjectAtPositiveIndex = {
|
|
112
|
+
nameList,
|
|
113
|
+
type: "inject",
|
|
114
|
+
value: valueTarget,
|
|
115
|
+
index: indexTarget,
|
|
116
|
+
};
|
|
117
|
+
const instructionInjectAtNegativeIndex = {
|
|
118
|
+
nameList,
|
|
119
|
+
type: "inject",
|
|
120
|
+
value: valueTarget,
|
|
121
|
+
index: indexTarget - listInitial.length,
|
|
122
|
+
};
|
|
123
|
+
//
|
|
124
|
+
//valid
|
|
125
|
+
//
|
|
126
|
+
{
|
|
127
|
+
const arrInstructionValid = probaLawFlip([
|
|
128
|
+
[
|
|
129
|
+
[instructionInjectAtPositiveIndex, instructionInjectAtNegativeIndex],
|
|
130
|
+
0.5,
|
|
131
|
+
],
|
|
132
|
+
[[instructionInjectAtPositiveIndex], 0.4],
|
|
133
|
+
[[instructionInjectAtNegativeIndex], 0.1],
|
|
134
|
+
]);
|
|
135
|
+
arrInstructionValid.forEach((instructionValid) => {
|
|
136
|
+
addValidProp(propositions, codifyChunk(getStrPythonInstruction(instructionValid)), "raw");
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
//
|
|
140
|
+
//invalid
|
|
141
|
+
//
|
|
142
|
+
//indexAsked-1
|
|
143
|
+
{
|
|
144
|
+
const arrInstructionWrong = [
|
|
145
|
+
instructionInjectAtPositiveIndex,
|
|
146
|
+
instructionInjectAtNegativeIndex,
|
|
147
|
+
].map((instruction) => {
|
|
148
|
+
return { ...instruction, index: instruction.index - 1 };
|
|
149
|
+
});
|
|
150
|
+
arrInstructionWrong.forEach((instructionWrong) => {
|
|
151
|
+
tryToAddWrongProp(propositions, codifyChunk(getStrPythonInstruction(instructionWrong)), "raw");
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
//indexAsked+1
|
|
155
|
+
{
|
|
156
|
+
const arrInstructionWrong = [
|
|
157
|
+
instructionInjectAtPositiveIndex,
|
|
158
|
+
instructionInjectAtNegativeIndex,
|
|
159
|
+
].map((instruction) => {
|
|
160
|
+
return { ...instruction, index: instruction.index + 1 };
|
|
161
|
+
});
|
|
162
|
+
arrInstructionWrong.forEach((instructionWrong) => {
|
|
163
|
+
tryToAddWrongProp(propositions, codifyChunk(getStrPythonInstruction(instructionWrong)), "raw");
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
//student: write initial value
|
|
167
|
+
{
|
|
168
|
+
const arrInstructionWrong = [
|
|
169
|
+
instructionInjectAtPositiveIndex,
|
|
170
|
+
instructionInjectAtNegativeIndex,
|
|
171
|
+
].map((instruction) => {
|
|
172
|
+
return { ...instruction, value: valueInitial };
|
|
173
|
+
});
|
|
174
|
+
arrInstructionWrong.forEach((instructionWrong) => {
|
|
175
|
+
tryToAddWrongProp(propositions, codifyChunk(getStrPythonInstruction(instructionWrong)), "raw");
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
//easy terror: wrong list name
|
|
179
|
+
if (coinFlip()) {
|
|
180
|
+
const poolNameList = ["l", "lst", "liste"];
|
|
181
|
+
const nameListWrong = random(poolNameList.filter((name) => name !== nameList));
|
|
182
|
+
//indexAsked
|
|
183
|
+
{
|
|
184
|
+
const arrInstructionWrong = [
|
|
185
|
+
instructionInjectAtPositiveIndex,
|
|
186
|
+
instructionInjectAtNegativeIndex,
|
|
187
|
+
].map((instruction) => {
|
|
188
|
+
return {
|
|
189
|
+
...instruction,
|
|
190
|
+
nameList: nameListWrong,
|
|
191
|
+
};
|
|
192
|
+
});
|
|
193
|
+
arrInstructionWrong.forEach((instructionWrong) => {
|
|
194
|
+
tryToAddWrongProp(propositions, codifyChunk(getStrPythonInstruction(instructionWrong)), "raw");
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
//indexAsked-1
|
|
198
|
+
{
|
|
199
|
+
const arrInstructionWrong = [
|
|
200
|
+
instructionInjectAtPositiveIndex,
|
|
201
|
+
instructionInjectAtNegativeIndex,
|
|
202
|
+
].map((instruction) => {
|
|
203
|
+
return {
|
|
204
|
+
...instruction,
|
|
205
|
+
index: instruction.index - 1,
|
|
206
|
+
nameList: nameListWrong,
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
arrInstructionWrong.forEach((instructionWrong) => {
|
|
210
|
+
tryToAddWrongProp(propositions, codifyChunk(getStrPythonInstruction(instructionWrong)), "raw");
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
//indexAsked+1
|
|
214
|
+
{
|
|
215
|
+
const arrInstructionWrong = [
|
|
216
|
+
instructionInjectAtPositiveIndex,
|
|
217
|
+
instructionInjectAtNegativeIndex,
|
|
218
|
+
].map((instruction) => {
|
|
219
|
+
return {
|
|
220
|
+
...instruction,
|
|
221
|
+
index: instruction.index + 1,
|
|
222
|
+
nameList: nameListWrong,
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
arrInstructionWrong.forEach((instructionWrong) => {
|
|
226
|
+
tryToAddWrongProp(propositions, codifyChunk(getStrPythonInstruction(instructionWrong)), "raw");
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
//student: write initial value
|
|
230
|
+
{
|
|
231
|
+
const arrInstructionWrong = [
|
|
232
|
+
instructionInjectAtPositiveIndex,
|
|
233
|
+
instructionInjectAtNegativeIndex,
|
|
234
|
+
].map((instruction) => {
|
|
235
|
+
return { ...instruction, value: valueInitial, nameList: nameListWrong };
|
|
236
|
+
});
|
|
237
|
+
arrInstructionWrong.forEach((instructionWrong) => {
|
|
238
|
+
tryToAddWrongProp(propositions, codifyChunk(getStrPythonInstruction(instructionWrong)), "raw");
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return shuffleProps(propositions, n);
|
|
243
|
+
};
|
|
244
|
+
const createRandomIdentifiers = () => {
|
|
245
|
+
const poolNameList = ["l", "lst", "liste"];
|
|
246
|
+
const nameList = random(poolNameList);
|
|
247
|
+
function createRandomListInitialNumber(listTarget, index, poolNumber) {
|
|
248
|
+
const listWork = [...listTarget];
|
|
249
|
+
const valueTarget = listTarget[index];
|
|
250
|
+
const valueInitial = random(poolNumber.filter((v) => v != valueTarget));
|
|
251
|
+
listWork[index] = valueInitial;
|
|
252
|
+
return listWork;
|
|
253
|
+
}
|
|
254
|
+
function createRandomArrList(minLength, maxLength) {
|
|
255
|
+
const rand = randint(0, 4);
|
|
256
|
+
const length = randint(minLength, maxLength + 1);
|
|
257
|
+
const index = randint(0, length);
|
|
258
|
+
switch (rand) {
|
|
259
|
+
case 0: {
|
|
260
|
+
const poolLetter = "abcdefghijklmnopqrstuvwxyz".split("");
|
|
261
|
+
const poolWords = [
|
|
262
|
+
...["oui", "non"],
|
|
263
|
+
...["vrai", "faux", "math", "live", "test", "cool", "bien"],
|
|
264
|
+
...["raoul", "final", "grand", "train", "suite"],
|
|
265
|
+
];
|
|
266
|
+
const wordTarget = random(poolWords.filter((word) => word.length === length));
|
|
267
|
+
const listTarget = wordTarget.split("");
|
|
268
|
+
const listInitial = (() => {
|
|
269
|
+
const listWork = [...listTarget];
|
|
270
|
+
const letterTarget = listTarget[index];
|
|
271
|
+
const letterInitial = random(poolLetter.filter((letter) => letter != letterTarget));
|
|
272
|
+
listWork[index] = letterInitial;
|
|
273
|
+
return listWork;
|
|
274
|
+
})();
|
|
275
|
+
return [listInitial, listTarget];
|
|
276
|
+
}
|
|
277
|
+
case 1: {
|
|
278
|
+
//constant list
|
|
279
|
+
const poolNumber = [...Array(61).keys()].map((i) => i - 30);
|
|
280
|
+
const listTarget = (() => {
|
|
281
|
+
const value = random(poolNumber);
|
|
282
|
+
return [...Array(length).keys()].map(() => value);
|
|
283
|
+
})();
|
|
284
|
+
const listInitial = createRandomListInitialNumber(listTarget, index, poolNumber);
|
|
285
|
+
return [listInitial, listTarget];
|
|
286
|
+
}
|
|
287
|
+
case 2: {
|
|
288
|
+
//arithmetic list
|
|
289
|
+
const poolNumber = [...Array(61).keys()].map((i) => i - 30);
|
|
290
|
+
const listTarget = (() => {
|
|
291
|
+
const value = random(poolNumber);
|
|
292
|
+
const reason = randint(1, 10);
|
|
293
|
+
return [...Array(length).keys()].map((i) => value + i * reason);
|
|
294
|
+
})();
|
|
295
|
+
const listInitial = createRandomListInitialNumber(listTarget, index, poolNumber);
|
|
296
|
+
return [listInitial, listTarget];
|
|
297
|
+
}
|
|
298
|
+
case 3: {
|
|
299
|
+
//geometric list
|
|
300
|
+
const poolNumber = [...Array(61).keys()].map((i) => i - 30);
|
|
301
|
+
const listTarget = (() => {
|
|
302
|
+
const value = random(poolNumber);
|
|
303
|
+
const reason = randint(2, 5);
|
|
304
|
+
return [...Array(length).keys()].map((i) => value * Math.pow(reason, i));
|
|
305
|
+
})();
|
|
306
|
+
const listInitial = createRandomListInitialNumber(listTarget, index, poolNumber);
|
|
307
|
+
return [listInitial, listTarget];
|
|
308
|
+
}
|
|
309
|
+
default:
|
|
310
|
+
throw new Error("Unsupported rand: " + rand);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const [listInitial, listTarget] = createRandomArrList(3, 5);
|
|
314
|
+
const identifiers = { nameList, listInitial, listTarget };
|
|
315
|
+
return identifiers;
|
|
316
|
+
};
|
|
317
|
+
const getPyListSetElemAtIndexInstructionCompletionQuestion = () => {
|
|
318
|
+
const identifiers = createRandomIdentifiers();
|
|
319
|
+
return getQuestionFromIdentifiers(identifiers);
|
|
320
|
+
};
|
|
321
|
+
const getQuestionFromIdentifiers = (identifiers, opts) => {
|
|
322
|
+
const question = {
|
|
323
|
+
instruction: getInstruction(identifiers, opts),
|
|
324
|
+
keys: [],
|
|
325
|
+
answerFormat: "tex",
|
|
326
|
+
identifiers,
|
|
327
|
+
hint: getHint(identifiers, opts),
|
|
328
|
+
correction: getCorrection(identifiers, opts),
|
|
329
|
+
options: opts,
|
|
330
|
+
};
|
|
331
|
+
return question;
|
|
332
|
+
};
|
|
333
|
+
export const pyListSetElemAtIndexInstructionCompletion = {
|
|
334
|
+
id: "pyListSetElemAtIndexInstructionCompletion",
|
|
335
|
+
label: "Affecter une valeur à un élément dans une liste en Python (compléter le programme)",
|
|
336
|
+
isSingleStep: true,
|
|
337
|
+
generator: (nb, opts) => getDistinctQuestions(() => getPyListSetElemAtIndexInstructionCompletionQuestion(opts), nb),
|
|
338
|
+
qcmTimer: 60,
|
|
339
|
+
freeTimer: 60,
|
|
340
|
+
getPropositions,
|
|
341
|
+
subject: "Mathématiques",
|
|
342
|
+
getQuestionFromIdentifiers,
|
|
343
|
+
hasHintAndCorrection: true,
|
|
344
|
+
answerType: "QCM",
|
|
345
|
+
isQCM: true,
|
|
346
|
+
};
|