Skip to content

Commit 8cfd74d

Browse files
authored
Adding fstring/rstring exercise (#671)
* ajoute un exo fstring rstring * ajoute exo3
1 parent dfae274 commit 8cfd74d

3 files changed

Lines changed: 247 additions & 8 deletions

File tree

_quarto.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project:
44
- index.qmd
55
- 404.qmd
66
- content/getting-started/index.qmd
7-
- content/modelisation/2_classification.qmd
7+
- content/getting-started/03_revisions.qmd
88

99
profile:
1010
default: fr

content/getting-started/03_revisions.qmd

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ For once, the exercises are done directly on this page rather than via _notebook
178178
{{< include "03_revisions/_exo1_solutions.qmd" >}}
179179

180180

181-
181+
:::: {.content-visible when-profile="fr"}
182182
## Exercice 2
183183

184184
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'
190190
:::
191191

192192
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+
:::
193205

206+
Here are the objects we will need for this exercise:
207+
::::
194208

195209
```{python}
196210
import requests
@@ -247,7 +261,7 @@ elves_example = example.split("\n\n")
247261
```
248262

249263

250-
264+
:::: {.content-visible when-profile="fr"}
251265
::: {.callout-tip}
252266
## Exercice 2
253267

@@ -258,23 +272,56 @@ Cet exercice vise à vous guider pour résoudre le problème [sur cette page](ht
258272
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.
259273
4. Généraliser pour résoudre le problème
260274
:::
275+
::::
276+
277+
:::: {.content-visible when-profile=“en”}
278+
::: {.callout-tip}
279+
## Exercise 2
261280

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"}
262292
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+
::::
263298

264299
```{pyodide}
265300
#| 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
267302
```
268303

304+
::::: {.content-visible when-profile="fr"}
269305
:::: { .hint exercise="ex_2_q2"}
270306
::: { .callout-note collapse="false"}
271307
## Indice 1
272308

273309
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
274318

319+
We will need to iterate over each value of `elves_example`: how can we transform the character string into a more manageable object?
275320
:::
276321
::::
322+
:::::
277323

324+
::::: {.content-visible when-profile="fr"}
278325
:::: { .hint exercise="ex_2_q2"}
279326
::: { .callout-note collapse="false"}
280327
## Indice 2
@@ -283,21 +330,45 @@ Attention au type d'objet que vous allez obtenir, n'oubliez pas de convertir dan
283330

284331
:::
285332
::::
333+
:::::
286334

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+
:::::
287345

346+
347+
::::: {.content-visible when-profile="fr"}
288348
:::: { .hint exercise="ex_2_q2"}
289349
::: { .callout-note collapse="false"}
290350
## Indice 3
291351

292352
Vous devriez finir avec la liste
293353

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
297364

298365
:::
299366
::::
367+
:::::
300368

369+
```{ojs}
370+
calories_example
371+
```
301372

302373
```{python}
303374
# Question 2
@@ -313,7 +384,14 @@ for idx, elf in enumerate(elves_example):
313384
ojs_define(calories_example = calories_example)
314385
```
315386

387+
:::: {.content-visible when-profile="fr"}
316388
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+
317395

318396
```{pyodide}
319397
#| exercise: ex_2_q3
@@ -339,13 +417,19 @@ data = process_calories(elves)
339417
calories_total = [sum(d) for d in data]
340418
```
341419

420+
:::: {.content-visible when-profile="fr"}
342421
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:
426+
::::
343427

344428
```{python}
345429
idx_elf = calories_total.index(max(calories_total))
346430
max_elf = max(calories_total)
347431
print(
348-
f"L'elfe numéro {idx_elf} porte le plus de calories ({max_elf} calories)"
432+
f"Elf number {idx_elf} carries the most calories ({max_elf} calories)."
349433
)
350434
```
351435

@@ -355,3 +439,4 @@ print(
355439
```
356440

357441

442+
{{< include "03_revisions/_exo3.qmd" >}}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
::: {.content-visible when-profile="fr"}
2+
## Exercice 3: les chaînes de caractères
3+
4+
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:
118+
::::
119+
120+
121+
::: {.content-visible when-format="html"}
122+
123+
```{pyodide}
124+
# Question 4: start from
125+
"C:\Users\Nadia\Documents\cours\chapitre_03\notes.txt"
126+
```
127+
128+
:::
129+
130+
```{python}
131+
#| error: true
132+
# Question 4
133+
chemin_bad = "C:\Users\Nadia\Documents\cours\chapitre_03\notes.txt"
134+
135+
print(chemin_bad)
136+
```
137+
138+
:::: {.content-visible when-profile="fr"}
139+
Les slashs posent souvent problème. Avec des _raw strings_ vous ne devriez pas rencontrer ce problème:
140+
::::
141+
142+
:::: {.content-visible when-profile="en"}
143+
Slashes often cause problems. With raw strings, you should not encounter this problem:
144+
::::
145+
146+
147+
```{python}
148+
# Question 4
149+
chemin_ok = r"C:\Users\Nadia\Documents\cours\chapitre_03\notes.txt"
150+
print(f"File can be found here: {chemin_ok}")
151+
152+
# This alternative is ok
153+
chemin_ok2 = "C:\\Users\\Nadia\\Documents\\cours\\chapitre_03\\notes.txt"
154+
```

0 commit comments

Comments
 (0)