You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/getting-started/03_revisions.qmd
+92-7Lines changed: 92 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -178,7 +178,7 @@ For once, the exercises are done directly on this page rather than via _notebook
178
178
{{< include "03_revisions/_exo1_solutions.qmd" >}}
179
179
180
180
181
-
181
+
:::: {.content-visible when-profile="fr"}
182
182
## Exercice 2
183
183
184
184
Les _advent of code_ sont d'excellents problèmes pratiques pour apprendre l'algorithmique. Avec un problème par jour de difficulté croissante entre le 1er décembre et le jour de Noël, on devient très vite à l'aise avec les nombreuses structures de données qu'offre `Python`.
@@ -190,7 +190,21 @@ Il serait pertinent d'utiliser `Numpy` pour le prochain problème. Mais c'est l'
190
190
:::
191
191
192
192
Voici les objets dont nous aurons besoin pour cet exercice:
193
+
::::
194
+
195
+
:::: {.content-visible when-profile="en"}
196
+
## Exercise 2
197
+
198
+
Advent of Code challenges are excellent practical problems for learning algorithms. With one problem per day of increasing difficulty between 1 December and Christmas Day, you will quickly become comfortable with the many data structures offered by Python.
199
+
200
+
The next exercise proposes solving the first part of day 1 (year 2022) using only basic Python.
201
+
202
+
::: {.callout-warning}
203
+
It would be appropriate to use Numpy for the next problem. But the purpose of the next chapter is to show how this package simplifies basic numerical operations.
204
+
:::
193
205
206
+
Here are the objects we will need for this exercise:
@@ -258,23 +272,56 @@ Cet exercice vise à vous guider pour résoudre le problème [sur cette page](ht
258
272
3. En déduire l'elfe portant le plus de calories. Vérifier que vous obtenez bien sur l'exemple les calories évoquées dans la consigne.
259
273
4. Généraliser pour résoudre le problème
260
274
:::
275
+
::::
276
+
277
+
:::: {.content-visible when-profile=“en”}
278
+
::: {.callout-tip}
279
+
## Exercise 2
261
280
281
+
This exercise aims to guide you in solving the problem [on this page](https://adventofcode.com/2022/day/1) (Advent of Code 2022, day 1, part one). You will need to find a more suitable data structure than the initial one.
282
+
283
+
1. Read the instructions.
284
+
2. Working step by step, try to find the number of calories carried by each elf in the example.
285
+
3. Deduce which elf is carrying the most calories. Check that you have obtained the number of calories mentioned in the instructions for the example.
286
+
4. Generalize to solve the problem.
287
+
:::
288
+
289
+
::::
290
+
291
+
:::: {.content-visible when-profile="fr"}
262
292
Vous pouvez dans cette cellule écrire votre code pour la question 2:
293
+
::::
294
+
295
+
:::: {.content-visible when-profile="en"}
296
+
You can write your code for question 2 in this cell:
297
+
::::
263
298
264
299
```{pyodide}
265
300
#| exercise: ex_2_q2
266
-
# Tester ici de manière itérative votre solution à la question 2
301
+
# Test here your solution to question 2
267
302
```
268
303
304
+
::::: {.content-visible when-profile="fr"}
269
305
:::: { .hint exercise="ex_2_q2"}
270
306
::: { .callout-note collapse="false"}
271
307
## Indice 1
272
308
273
309
Il va falloir itérer sur chaque valeur de `elves_example`: comment transformer la chaîne de caractère en objet plus maniable ?
310
+
:::
311
+
::::
312
+
:::::
313
+
314
+
::::: {.content-visible when-profile="en"}
315
+
:::: { .hint exercise="ex_2_q2"}
316
+
::: { .callout-note collapse="false"}
317
+
## Hint 1
274
318
319
+
We will need to iterate over each value of `elves_example`: how can we transform the character string into a more manageable object?
275
320
:::
276
321
::::
322
+
:::::
277
323
324
+
::::: {.content-visible when-profile="fr"}
278
325
:::: { .hint exercise="ex_2_q2"}
279
326
::: { .callout-note collapse="false"}
280
327
## Indice 2
@@ -283,21 +330,45 @@ Attention au type d'objet que vous allez obtenir, n'oubliez pas de convertir dan
283
330
284
331
:::
285
332
::::
333
+
:::::
286
334
335
+
::::: {.content-visible when-profile="en"}
336
+
:::: { .hint exercise="ex_2_q2"}
337
+
::: { .callout-note collapse="false"}
338
+
## Hint 2
339
+
340
+
Be careful with the type of object you will obtain. Don't forget to convert it to a numeric format if you want to perform arithmetic operations.
341
+
342
+
:::
343
+
::::
344
+
:::::
287
345
346
+
347
+
::::: {.content-visible when-profile="fr"}
288
348
:::: { .hint exercise="ex_2_q2"}
289
349
::: { .callout-note collapse="false"}
290
350
## Indice 3
291
351
292
352
Vous devriez finir avec la liste
293
353
294
-
```{ojs}
295
-
calories_example
296
-
```
354
+
:::
355
+
::::
356
+
:::::
357
+
358
+
::::: {.content-visible when-profile=“en”}
359
+
:::: { .hint exercise="ex_2_q2"}
360
+
::: { .callout-note collapse="false"}
361
+
## Hint 3
362
+
363
+
You should end up with the list
297
364
298
365
:::
299
366
::::
367
+
:::::
300
368
369
+
```{ojs}
370
+
calories_example
371
+
```
301
372
302
373
```{python}
303
374
# Question 2
@@ -313,7 +384,14 @@ for idx, elf in enumerate(elves_example):
313
384
ojs_define(calories_example = calories_example)
314
385
```
315
386
387
+
:::: {.content-visible when-profile="fr"}
316
388
Vous pouvez dans cette cellule écrire votre code pour la question 3:
389
+
::::
390
+
391
+
:::: {.content-visible when-profile="en"}
392
+
You can write your code for question 3 in this cell:
393
+
::::
394
+
317
395
318
396
```{pyodide}
319
397
#| exercise: ex_2_q3
@@ -339,13 +417,19 @@ data = process_calories(elves)
339
417
calories_total = [sum(d) for d in data]
340
418
```
341
419
420
+
:::: {.content-visible when-profile="fr"}
342
421
Pour finir, après avoir généralisé, vous devriez retrouver les valeurs suivantes:
422
+
::::
423
+
424
+
:::: {.content-visible when-profile="en"}
425
+
Finally, after generalizing, you should find the following values:
Voici le type de motif final vers lequel vous désirez aboutir à la question 1 et 2.
5
+
6
+
```
7
+
On 17 March 2022, Nadia (34) reported an income of $33,333.33.
8
+
```
9
+
:::
10
+
11
+
::: {.content-visible when-profile="en"}
12
+
## Exercise 3: strings
13
+
14
+
This is the final pattern you should end up in question 1 and 2
15
+
16
+
```
17
+
On 17 March 2022, Nadia (34) reported an income of $33,333.33.
18
+
```
19
+
:::
20
+
21
+
:::: {.content-visible when-profile="fr"}
22
+
23
+
::: {.callout-tip}
24
+
## Exercice 3
25
+
26
+
1. A partir de la méthode `format`, essayez d'obtenir le résultat attendu.
27
+
2. Faites de même avec les _f-strings_.
28
+
3. Modifiez le message pour créer un revenu mensuel arrondi à 1 chiffre. Utiliser l'espace comme séparateur de milliers plutôt que le séparateur par défaut (la virgule).
29
+
4. Voici un chemin fictif `C:\Users\Nadia\Documents\cours\chapitre_03\notes.txt` vers un fichier. Faire un _print_ correctif de celui-ci.
30
+
:::
31
+
32
+
::::
33
+
34
+
:::: {.content-visible when-profile="en"}
35
+
## Exercise 3
36
+
37
+
1. Using the `format` method, try to obtain the expected result.
38
+
2. Do the same with _f-strings_.
39
+
3. Modify the message to create a monthly income rounded to 1 digit. Use a space as the thousands separator rather than the default separator (the comma).
40
+
4. Here is a fictitious path `C:\Users\Nadia\Documents\cours\chapitre_03\notes.txt` to a file. Make a correct print of it.
41
+
::::
42
+
43
+
::: {.content-visible when-format="html"}
44
+
45
+
```{pyodide}
46
+
#| autorun: true
47
+
import datetime
48
+
49
+
date = datetime.datetime(2022, 3, 17)
50
+
prenom = "Nadia"
51
+
age = 34
52
+
revenu = 100000/3
53
+
```
54
+
55
+
:::
56
+
57
+
```{python}
58
+
import datetime
59
+
60
+
date = datetime.datetime(2022, 3, 17)
61
+
prenom = "Nadia"
62
+
age = 34
63
+
revenu = 100000/3
64
+
```
65
+
66
+
:::: {.content-visible when-profile="fr"}
67
+
Dans la première partie de cet exercice, vous devriez avoir ces résultats.
68
+
::::
69
+
70
+
:::: {.content-visible when-profile="en"}
71
+
In the first part of this exercise, you should have these results.
72
+
::::
73
+
74
+
```{python}
75
+
# Question 1
76
+
msg_format = "On {date:%d %B %Y}, {prenom} ({age}) reported an income of ${revenu:,.2f}.".format(
77
+
date=date, prenom=prenom, age=age, revenu=revenu
78
+
)
79
+
80
+
print("Question 1:")
81
+
print(msg_format)
82
+
83
+
# Question 2
84
+
msg_fstring = f"On {date:%d %B %Y}, {prenom} ({age}) reported an income of ${revenu:,.2f}."
85
+
86
+
print("Question 2:")
87
+
print(msg_fstring)
88
+
89
+
# Question 3
90
+
msg_fstring = f"On {date:%d %B %Y}, {prenom} ({age}) reported an income of ${revenu/12:,.1f}.".replace(',', ' ')
91
+
92
+
print("Question 3:")
93
+
print(msg_fstring)
94
+
```
95
+
96
+
::: {.content-visible when-format="html"}
97
+
98
+
:::: {.content-visible when-profile="fr"}
99
+
Vous pouvez tester ci-dessous votre solution
100
+
::::
101
+
102
+
:::: {.content-visible when-profile="en"}
103
+
Try replicating these results using interactive cell below
104
+
::::
105
+
106
+
```{pyodide}
107
+
# Try here
108
+
```
109
+
110
+
:::
111
+
112
+
:::: {.content-visible when-profile="fr"}
113
+
Dans la deuxième partie de l'exercice, si vous utilisez le mauvais type de _string_, vous devriez avoir une erreur:
114
+
::::
115
+
116
+
:::: {.content-visible when-profile="en"}
117
+
In the second part of the exercise, if you use the wrong type of _string_, you should get an error:
0 commit comments