-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathen.subject.tex
More file actions
1092 lines (909 loc) · 30.8 KB
/
en.subject.tex
File metadata and controls
1092 lines (909 loc) · 30.8 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% vim: set ts=4 sw=4 tw=80 noexpandtab:
\documentclass{42-en}
%******************************************************************************%
% %
% Prologue %
% %
%******************************************************************************%
\usepackage[
type={CC},
modifier={by-nc-sa},
version={4.0},
]{doclicense}
\usemintedstyle{monokai}
%****************************************************************%
% Re/definition of commands %
%****************************************************************%
\newcommand{\ailogo}[1]{\def \@ailogo {#1}}\ailogo{assets/42ai_logo.pdf}
%% Redefine \maketitle
\makeatletter
\def \maketitle {
\begin{titlepage}
\begin{center}
%\begin{figure}[t]
%\includegraphics[height=8cm]{\@ailogo}
\includegraphics[height=8cm]{assets/42ai_logo.pdf}
%\end{figure}
\vskip 5em
{\huge \@title}
\vskip 2em
{\LARGE \@subtitle}
\vskip 4em
\end{center}
%\begin{center}
%\@author
%\end{center}
%\vskip 5em
\vfill
\begin{center}
\emph{\summarytitle : \@summary}
\end{center}
\vspace{2cm}
%\vskip 5em
%\doclicenseThis
\end{titlepage}
}
\makeatother
\makeatletter
\def \makeheaderfilesforbidden
{
\noindent
\begin{tabularx}{\textwidth}{|X X X X|}
\hline
\multicolumn{1}{|>{\raggedright}m{1cm}|}
{\vskip 2mm \includegraphics[height=1cm]{assets/42ai_logo.pdf}} &
\multicolumn{2}{>{\centering}m{12cm}}{\small Exercise : \@exnumber } &
\multicolumn{1}{ >{\raggedleft}p{1.5cm}|}
%% {\scriptsize points : \@exscore} \\ \hline
{} \\ \hline
\multicolumn{4}{|>{\centering}m{15cm}|}
{\small \@extitle} \\ \hline
\multicolumn{4}{|>{\raggedright}m{15cm}|}
{\small Turn-in directory : \ttfamily
$ex\@exnumber/$ }
\\ \hline
\multicolumn{4}{|>{\raggedright}m{15cm}|}
{\small Files to turn in : \ttfamily \@exfiles }
\\ \hline
\multicolumn{4}{|>{\raggedright}m{15cm}|}
{\small Forbidden functions : \ttfamily \@exforbidden }
\\ \hline
%% \multicolumn{4}{|>{\raggedright}m{15cm}|}
%% {\small Remarks : \ttfamily \@exnotes }
%% \\ \hline
\end{tabularx}
%% \exnotes
\exrules
\exmake
\exauthorize{None}
\exforbidden{None}
\extitle{}
\exnumber{}
}
\makeatother
\begin{document}
% =============================================================================%
% ===================================== %
\title{Python \& ML - Module 00}
\subtitle{Basic stuff - The Eleven Commandments}
\author{
Maxime Choulika (cmaxime), Pierre Peigné (ppeigne), Matthieu David (mdavid)
}
\summary
{
The goal of the module is to get started with the Python language.
}
\maketitle
\input{en.py_proj.tex}
\newpage
\tableofcontents
\startexercices
% ===================================== %
% =============================================================================%
%******************************************************************************%
% %
% Exercises %
% %
%******************************************************************************%
% ============================================== %
% ===========================(start ex 00) %
\chapter{Exercise 00}
\extitle{\$PATH}
\turnindir{ex00}
\exnumber{00}
\exfiles{answers.txt, requirements.txt}
\exforbidden{None}
\makeheaderfilesforbidden
\emph{The first thing you need to do is install Python.}
\\\\
Most modern Unix-based systems have a \texttt{python} interpreter installed by default,
but its version might be lower/higher than the one used for these modules.
It is also possible that the default \texttt{python} command uses a version 2.x (for legacy reasons).
This is obviously very confusing for a new developper.
\\
\begin{42console}
$> python -V
$> python3 -V
\end{42console}
\hfill \break
To deal with those version issues we will use \texttt{conda}. This program allows you to manage your Python packages and several working environments.
\\\\
\emph{Note: the actual requirement is to use a Python 3.7.X version. You are free to use a different program/utilities to achieve this goal. At your own risk.}
\pagebreak
% ===========================(Conda Manual) %
\section*{Conda manual installation}
\emph{Go to the next section for an automated installation.}
\\\\
We recommend the following path for your \texttt{conda} folder.
\begin{42console}
$> MYPATH="/goinfre/$USER/miniconda3"
\end{42console}
% ---------------------------------- %
\subsection*{1. Download \& Install conda}
\begin{42console}
# For MAC
$> curl -LO "https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh"
$> sh Miniconda3-latest-MacOSX-x86_64.sh -b -p $MYPATH
# For Linux
$> curl -LO "https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh"
$> sh Miniconda3-latest-Linux-x86_64.sh -b -p $MYPATH
\end{42console}
% ---------------------------------- %
\subsection*{2. Initial configuration of conda}
\begin{42console}
# For zsh
$> $MYPATH/bin/conda init zsh
$> $MYPATH/bin/conda config --set auto_activate_base false
$> source ~/.zshrc
# For bash
$> $MYPATH/bin/conda init bash
$> $MYPATH/bin/conda config --set auto_activate_base false
$> source ~/.bash_profile
\end{42console}
% ---------------------------------- %
\subsection*{3. Create a dedicated environment for 42AI !}
\begin{42console}
$> conda create --name 42AI-$USER python=3.7 jupyter pandas pycodestyle numpy
\end{42console}
% ---------------------------------- %
\subsection*{4. Check your 42AI Python environment}
\begin{42console}
$> conda info --envs
$> conda activate 42AI-$USER
$> which python
$> python -V
$> python -c "print('Hello World!')"
\end{42console}
% ---------------------------------- %
\subsection*{Help !}
\begin{itemize}
\item \textbf{I have lost my miniconda3 folder !} Repeat step 1 and 3.
\item \textbf{I have lost my home directory !} Repeat step 2.
\end{itemize}
\pagebreak
% ===========================(Conda Auto) %
\section*{Conda automated installation}
Copy the following script into a file, launch it and follow the instructions. It downloads and installs miniconda in a \texttt{/goinfre} subfolder and creates a \texttt{python} environment in \texttt{conda}.
\begin{42console}
#!/bin/bash
function which_dl {
# If operating system name contains Darwnin: MacOS. Else Linux
if uname -s | grep -iqF Darwin; then
echo "Miniconda3-latest-MacOSX-x86_64.sh"
else
echo "Miniconda3-latest-Linux-x86_64.sh"
fi
}
function which_shell {
# if $SHELL contains zsh, zsh. Else Bash
if echo $SHELL | grep -iqF zsh; then
echo "zsh"
else
echo "bash"
fi
}
function when_conda_exist {
# check and install 42AI environement
printf "Checking 42AI-$USER environment: "
if conda info --envs | grep -iqF 42AI-$USER; then
printf "\e[33mDONE\e[0m\n"
else
printf "\e[31mKO\e[0m\n"
printf "\e[33mCreating 42AI environnment:\e[0m\n"
conda update -n base -c defaults conda -y
conda create --name 42AI-$USER python=3.7 jupyter numpy pandas pycodestyle -y
fi
}
function set_conda {
MINICONDA_PATH="/goinfre/$USER/miniconda3"
CONDA=$MINICONDA_PATH"/bin/conda"
PYTHON_PATH=$(which python)
REQUIREMENTS="jupyter numpy pandas pycodestyle"
SCRIPT=$(which_dl)
MY_SHELL=$(which_shell)
DL_LINK="https://repo.anaconda.com/miniconda/"$SCRIPT
DL_LOCATION="/tmp/"
printf "Checking conda: "
TEST=$(conda -h 2>/dev/null)
if [ $? == 0 ] ; then
printf "\e[32mOK\e[0m\n"
when_conda_exist
return
fi
printf "\e[31mKO\e[0m\n"
if [ ! -f $DL_LOCATION$SCRIPT ]; then
printf "\e[33mDonwloading installer:\e[0m\n"
cd $DL_LOCATION
curl -LO $DL_LINK
cd -
fi
printf "\e[33mInstalling conda:\e[0m\n"
sh $DL_LOCATION$SCRIPT -b -p $MINICONDA_PATH
printf "\e[33mConda initial setup:\e[0m\n"
$CONDA init $MY_SHELL
$CONDA config --set auto_activate_base false
printf "\e[33mCreating 42AI-$USER environnment:\e[0m\n"
$CONDA update -n base -c defaults conda -y
$CONDA create --name 42AI-$USER python=3.7 jupyter numpy pandas pycodestyle -y
printf "\e[33mLaunch the following command or restart your shell:\e[0m\n"
if [ $MY_SHELL == "zsh" ]; then
printf "\tsource ~/.zshrc\n"
else
printf "\tsource ~/.bash_profile\n"
fi
}
set_conda
\end{42console}
\hfill \break
Don't forget to check your 42AI Python environment !
\begin{42console}
conda info --envs
conda activate 42AI-$USER
which python
python -V
python -c "print('Hello World!')"
\end{42console}
\newpage
% =================================(Question) %
\section*{(Finally) getting started}
Now that your setup is ready to run, here are a few questions that need to be solved using \texttt{python}, \texttt{pip} or \texttt{conda}. Save your answers in a file \texttt{answers.txt} (one answer per line and per question), and check them with your peers.\\\\
Find the commands to:
\begin{itemize}
\item Output a list of installed packages and their versions.
\item Show the package metadata of \texttt{numpy}.
\item Remove the package \texttt{numpy}.
\item (Re)install the package \texttt{numpy}.
\item Freeze your \texttt{python} packages and their versions in a \texttt{requirements.txt} file you have to turn-in.
\end{itemize}
% ===========================(fin ex 00) %
\newpage
% ===========================(start ex 01) %
\chapter{Exercise 01}
\extitle{Rev Alpha}
\turnindir{ex01}
\exnumber{01}
\exfiles{exec.py}
\exforbidden{None}
\makeheaderfilesforbidden
% ================================= %
% \section*{}
Make a program that takes a string as argument, reverses it, swaps its letters case and prints the result.
\begin{itemize}
\item If more than one argument is provided, merge them into a single string with each argument separated by a single space character.
\item If no argument is provided, do nothing or print an usage.
\end{itemize}
% --------------------------------- %
\section*{Examples}
\begin{42console}
$> python3 exec.py 'Hello World!' | cat -e
!DLROw OLLEh$
$>
$> python3 exec.py 'Hello' 'my Friend' | cat -e
DNEIRf YM OLLEh$
$>
$> python3 exec.py
$>
\end{42console}
% ===========================(fin ex 01) %
\newpage
% ===========================(start ex 02) %
\chapter{Exercise 02}
\extitle{The Odd, the Even and the Zero}
\turnindir{ex02}
\exnumber{02}
\exfiles{whois.py}
\exforbidden{None}
\makeheaderfilesforbidden
% ================================= %
% \section*{}
Make a program that takes a number as argument, checks whether it is odd, even or zero, and prints the result.
\begin{itemize}
\item If more than one argument is provided or if the argument is not an integer, print an error message.
\item If no argument is provided, do nothing or print an usage.
\end{itemize}
% --------------------------------- %
\section*{Examples}
\begin{42console}
$> python3 whois.py 12
I'm Even.
$>
$> python3 whois.py 3
I'm Odd.
$>
$> python3 whois.py
$>
$> python3 whois.py 0
I'm Zero.
$>
$> python3 whois.py Hello
AssertionError: argument is not an integer
$>
$> python3 whois.py 12 3
AssertionError: more than one argument is provided
$>
\end{42console}
\hint{
No bonus point to be gained from a complex error management system. Keep it simple.
}
% ===========================(fin ex 02) %
\newpage
% ===========================(start ex 03) %
\chapter{Exercise 03}
\extitle{Functional file}
\turnindir{ex03}
\exnumber{03}
\exfiles{count.py}
\exforbidden{None}
\makeheaderfilesforbidden
% ================================= %
\section*{Part 1. text\_analyzer}
Create a function called \texttt{text\_analyzer} that takes a single string argument
and displays the total number of printable characters, and respectively : the number of upper-case characters, lower-case characters, punctuation characters and spaces.
\begin{itemize}
\item If \texttt{None} or nothing is provided, the user is prompted to provide a string.
\item If the argument is not a string, print an error message.
\item This function must have a \texttt{docstring} explaning its behavior.
\end{itemize}
Test your function with the \texttt{python} console
% --------------------------------- %
\subsection*{Examples}
\begin{42console}
$> python3
>>> from count import text_analyzer
>>> text_analyzer("Python 2.0, released 2000, introduced
features like List comprehensions and a garbage collection
system capable of collecting reference cycles.")
The text contains 143 printable character(s):
- 2 upper letter(s)
- 113 lower letter(s)
- 4 punctuation mark(s)
- 18 space(s)
>>> text_analyzer("Python is an interpreted, high-level,
general-purpose programming language. Created by Guido van
Rossum and first released in 1991, Python's design philosophy
emphasizes code readability with its notable use of significant
whitespace.")
The text contains 234 printable character(s):
- 5 upper letter(s)
- 187 lower letter(s)
- 8 punctuation mark(s)
- 30 space(s)
>>> text_analyzer()
What is the text to analyze?
>> Hello World!
The text contains 12 printable character(s):
- 2 upper letter(s)
- 8 lower letter(s)
- 1 punctuation mark(s)
- 1 space(s)
>>> text_analyzer(42)
AssertionError: argument is not a string
>>> print(text_analyzer.__doc__)
This function counts the number of upper characters, lower characters,
punctuation and spaces in a given text.
\end{42console}
% ================================= %
\section*{Part 2. \_\_name\_\_==\_\_main\_\_}
In the previous part, you wrote a function that can be used in the console or in another file when imported.
Without changing this behavior, update your file so it can also be launched as a standalone program.
\begin{itemize}
\item If more than one argument is provided to the program, print an error message.
\item Otherwise, use the \texttt{text\_analyzer} function.
\end{itemize}
% --------------------------------- %
\subsection*{Examples}
\begin{42console}
$> python3 count.py 'Hello World!'
The text contains 12 character(s):
- 2 upper letter(s)
- 8 lower letter(s)
- 1 punctuation mark(s)
- 1 space(s)
$> python3
>>> from count import text_analyzer
>>> text_analyzer("Hello World!")
The text contains 12 character(s):
- 2 upper letter(s)
- 8 lower letter(s)
- 1 punctuation mark(s)
- 1 space(s)
\end{42console}
% ===========================(fin ex 03) %
\newpage
% ===========================(start ex 04) %
\chapter{Exercise 04}
\extitle{Elementary}
\turnindir{ex04}
\exnumber{04}
\exfiles{operations.py}
\exforbidden{None}
\makeheaderfilesforbidden
% ================================= %
% \section*{}
Write a program that takes two integers A and B as arguments and prints the result of the following operations:
\begin{42console}
Sum: A+B
Difference: A-B
Product: A*B
Quotient: A/B
Remainder: A%B
\end{42console}
\begin{itemize}
\item If more or less than two arguments are provided or if one of the arguments is not an integer, print an error message.
\item If no argument is provided, do nothing or print an usage.
\item If an operation is impossible, print an error message instead of a numerical result.
\end{itemize}
% ================================= %
\section*{Examples}
\begin{42console}
$> python3 operations.py 10 3
Sum: 13
Difference: 7
Product: 30
Quotient: 3.3333...
Remainder: 1
$>
$> python3 operations.py 42 10
Sum: 52
Difference: 32
Product: 420
Quotient: 4.2
Remainder: 2
$>
$> python3 operations.py 1 0
Sum: 1
Difference: 1
Product: 0
Quotient: ERROR (division by zero)
Remainder: ERROR (modulo by zero)
$>
$> python3 operations.py
Usage: python operations.py <number1> <number2>
Example:
python operations.py 10 3
$>
$> python3 operations.py 12 10 5
AssertionError: too many arguments
$>
$> python3 operations.py "one" "two"
AssertionError: only integers
$>
\end{42console}
\hint{
No bonus point to be gained from handling decimal point or scientific notation. Keep it simple.
}
% ===========================(fin ex 04) %
\newpage
% ===========================(start ex 05) %
\chapter{Exercise 05}
\extitle{The right format}
\turnindir{ex05}
\exnumber{05}
\exfiles{kata00.py, kata01.py, kata02.py, kata03.py, kata04.py}
\exforbidden{None}
\makeheaderfilesforbidden
Let's get familiar with the useful concept of \textbf{string formatting} through a kata series.\\
Each exercice will provide you with a \texttt{kata} variable. This variable can be modified to a certain extent: your program must react accordingly.
% ================================= %
\section*{kata00}
The \texttt{kata} variable is always a tuple and can only be filled with integers.
\begin{42console}
# Put this at the top of your kata00.py file
kata = (19,42,21)
\end{42console}
Write a program that displays this variable content according to the format shown below:
\begin{42console}
$> python3 kata00.py
The 3 numbers are: 19, 42, 21
$>
\end{42console}
% ================================= %
\section*{kata01}
The \texttt{kata} variable is always a dictionary and can only be filled with strings.
\begin{42console}
# Put this at the top of your kata01.py file
kata = {
'Python': 'Guido van Rossum',
'Ruby': 'Yukihiro Matsumoto',
'PHP': 'Rasmus Lerdorf',
}
\end{42console}
Write a program that displays this variable content according to the format shown below:
\begin{42console}
$> python3 kata01.py
Python was created by Guido van Rossum
Ruby was created by Yukihiro Matsumoto
PHP was created by Rasmus Lerdorf
$>
\end{42console}
% ================================= %
\section*{kata02}
The \texttt{kata} variable is always a tuple that contains 5 non-negative integers. The first integer contains up to 4 digits, the rest up to 2 digits.
\begin{42console}
# Put this at the top of your kata02.py file
kata = (2019, 9, 25, 3, 30)
\end{42console}
Write a program that displays this variable content according to the format shown below:
\begin{42console}
$> python3 kata02.py | cat -e
09/25/2019 03:30$
$> python3 kata02.py | wc -c
17
$>
\end{42console}
% ================================= %
\section*{kata03}
The \texttt{kata} variable is always a string whose length is not higher than 42.
\begin{42console}
# Put this at the top of your kata03.py file
kata = "The right format"
\end{42console}
Write a program that displays this variable content according to the format shown below:
\begin{42console}
$> python3 kata03.py | cat -e
--------------------------The right format%
$> python3 kata03.py | wc -c
42
$>
\end{42console}
% ================================= %
\section*{kata04}
The \texttt{kata} variable is always a tuple that contains, in the following order:
\begin{itemize}
\item 2 non-negative integers containing up to 2 digits
\item 1 decimal
\item 1 integer
\item 1 decimal
\end{itemize}
\begin{42console}
# Put this at the top of your kata04.py file
kata = (0, 4, 132.42222, 10000, 12345.67)
\end{42console}
Write a program that displays this variable content according to the format shown below:
\begin{42console}
$> python3 kata04.py
module_00, ex_04 : 132.42, 1.00e+04, 1.23e+04
$> python3 kata04.py | cut -c 10,18
,:
\end{42console}
% ===========================(fin ex 05) %
\newpage
% ===========================(start ex 06) %
\chapter{Exercise 06}
\extitle{A recipe}
\turnindir{ex06}
\exnumber{06}
\exfiles{recipe.py}
\exforbidden{None}
\makeheaderfilesforbidden
% ================================= %
\section*{Part 1: Nested Dictionaries}
Create a dictionary called \texttt{cookbook}. You will use this \texttt{cookbook} to store recipes.
\\\\
A recipe is a \textbf{dictionary} that stores (at least) 3 key-value pairs:
\begin{itemize}
\item ''ingredients": a \textbf{list of strings} containing the list of ingredients
\item "meal": a \textbf{string} representing the type of meal
\item "prep\_time": a \textbf{non-negative integer} representing a preparation time in minutes
\end{itemize}
In the \texttt{cookbook}, the \textbf{key} to a recipe is the recipe's name.
\\
Initialize your \texttt{cookbook} with 3 recipes:
\begin{itemize}
\item The Sandwich's ingredients are \textit{ham}, \textit{bread}, \textit{cheese} and \textit{tomatoes}.
It is a \textit{lunch} and it takes $10$ minutes of preparation.
\item The Cake's ingredients are \textit{flour}, \textit{sugar} and \textit{eggs}.
It is a \textit{dessert} and it takes $60$ minutes of preparation.
\item The Salad's ingredients are \textit{avocado}, \textit{arugula}, \textit{tomatoes} and \textit{spinach}.
It is a \textit{lunch} and it takes $15$ minutes of preparation.
\end{itemize}
% ================================= %
\section*{Part 2: A Handful of Helpful Functions}
Create a series of useful functions to handle your \texttt{cookbook}:
\begin{enumerate}
\item A function that prints all recipe names.
\item A function that takes a recipe name and prints its details.
\item A function that takes a recipe name and delete it.
\item A function that adds a recipe from user input. You will need a name, a list of ingredients, a meal type and a preparation time.
\end{enumerate}
\subsection*{input example}
\begin{42console}
>>> Enter a name:
chips
>>> Enter ingredients:
potatoes
oil
salt
>>> Enter a meal type:
lunch
>>> Enter a preparation time:
15
\end{42console}
% ================================= %
\section*{Part 3: A command line executable !}
Create a program that uses your \texttt{cookbook} and your functions.\\
The program will prompt the user to make a choice between printing the cookbook's content, printing one recipe, adding a recipe, deleting a recipe or quitting the cookbook.\\
Your program will continue to prompt the user until the user decides to quit it.
The program cannot crash if a wrong value is entered: you must handle the error and ask for another prompt.
\begin{42console}
$> python3 recipe.py
Welcome to the Python Cookbook !
List of available options:
1: Add a recipe
2: Delete a recipe
3: Print a recipe
4: Print the cookbook
5: Quit
Please select an option:
>> 3
Please enter a recipe name to get its details:
>> cake
Recipe for cake:
Ingredients list: ['flour', 'sugar', 'eggs']
To be eaten for dessert.
Takes 60 minutes of cooking.
Please select an option:
>> Hello
Sorry, this option does not exist.
List of available options:
1: Add a recipe
2: Delete a recipe
3: Print a recipe
4: Print the cookbook
5: Quit
Please select an option:
>> 5
Cookbook closed. Goodbye !
$>
\end{42console}
% ===========================(fin ex 06) %
\newpage
% ===========================(start ex 07) %
\chapter{Exercise 07}
\extitle{Shorter, faster, pythonic}
\turnindir{ex07}
\exnumber{07}
\exfiles{filterwords.py}
\exforbidden{filter}
\makeheaderfilesforbidden
Make a program that takes a string S and an integer N as argument and prints the list of words in S that contains more than N non-punctuation characters.
\begin{itemize}
\item Words are separated from each other by space characters
\item Punctuation symbols must be removed from the printed list: they are neither part of a word nor a separator
\item The program must contain at least one \textbf{list comprehension} expression.
\end{itemize}
If the number of argument is different from 2, or if the type of any argument is invalid, the program prints an error message.
% ================================= %
\section*{Examples}
% --------------------------------- %
\begin{42console}
$> python3 filterwords.py 'Hello, my friend' 3
['Hello', 'friend']
$> python3 filterwords.py 'Hello, my friend' 10
[]
$> python3 filterwords.py 'A robot must protect its own existence as long as such protection does not conflict with the First or Second Law' 6
['protect', 'existence', 'protection', 'conflict']
$> python3 filterwords.py Hello World
ERROR
$> python3 filterwords.py 3 'Hello, my friend'
ERROR
$> python3 filterwords.py
ERROR
\end{42console}
% ===========================(fin ex 07) %
\newpage
% ===========================(start ex 08) %
\chapter{Exercise 08}
\extitle{S.O.S}
\turnindir{ex08}
\exnumber{08}
\exfiles{sos.py}
\exforbidden{None}
\makeheaderfilesforbidden
Make a program that takes a string as argument and encodes it into Morse code.
\begin{itemize}
\item The program supports space and alphanumeric characters
\item An alphanumeric character is represented by dots \texttt{.} and dashes \texttt{-}:
\item A space character is represented by a slash \texttt{/}
\item Complete morse characters are separated by a single space\\
\end{itemize}
If more than one argument is provided, merge them into a single string with each argument separated by a single space character.\\
If no argument is provided, do nothing or print an usage.
% ================================= %
\section*{Examples}
\begin{42console}
$> python3 sos.py "SOS"
... --- ...
$> python3 sos.py
$> python3 sos.py "HELLO / WORLD"
ERROR
$> python3 sos.py "96 BOULEVARD" "Bessiere"
----. -.... / -... --- ..- .-.. . ...- .- .-. -.. / -... . ... ... .. . .-. .
\end{42console}
\hint{
\url{https://morsecode.world/international/morse2.html}
}
% ===========================(fin ex 08) %
% ============================================== %
\newpage
% ============================================== %
% ===========================(start ex 09) %
\chapter{Exercise 09}
\extitle{Secret number}
\turnindir{ex09}
\exnumber{09}
\exfiles{guess.py}
\exforbidden{None}
\makeheaderfilesforbidden
You have to make a program that will be an interactive guessing game.
It will ask the user to guess a number between $1$ and $99$.
The program will tell the user if their input is too high or too low.
The game ends when the user finds out the secret number or types \texttt{exit}.
You will import the \texttt{random} module with the \texttt{randint} function to get a random number.
You have to count the number of trials and print that number when the user wins.
% ================================= %
\section*{Examples}
% --------------------------------- %
\begin{42console}
$> python guess.py
This is an interactive guessing game!
You have to enter a number between 1 and 99 to find out the secret number.
Type 'exit' to end the game.
Good luck!
What's your guess between 1 and 99?
>> 54
Too high!
What's your guess between 1 and 99?
>> 34
Too low!
What's your guess between 1 and 99?
>> 45
Too high!
What's your guess between 1 and 99?
>> A
That's not a number.
What's your guess between 1 and 99?
>> 43
Congratulations, you've got it!
You won in 5 attempts!
\end{42console}
If the user discovers the secret number on the first try, tell them.
If the secret number is 42, make a reference to Douglas Adams.
\begin{42console}
$> python guess.py
This is an interactive guessing game!
You have to enter a number between 1 and 99 to find out the secret number.
Type 'exit' to end the game.
Good luck!
What's your guess between 1 and 99?
>> 42
The answer to the ultimate question of life, the universe and everything is 42.
Congratulations! You got it on your first try!
\end{42console}
Other example:
\begin{42console}
$> python guess.py
This is an interactive guessing game!
You have to enter a number between 1 and 99 to find out the secret number.
Type 'exit' to end the game.
Good luck!
What's your guess between 1 and 99?
>> exit
Goodbye!
\end{42console}
% ===========================(fin ex 09) %
% ============================================== %
\newpage
% ============================================== %
% ===========================(start ex 10) %
\chapter{Exercise 10}
\extitle{Loading bar!}
\turnindir{ex10}
\exnumber{10}
\exfiles{loading.py}
\exforbidden{\texttt{tqdm} or any library for automatic loading bar}
\makeheaderfilesforbidden
You are about to discover the \texttt{yield} operator!
So let's create a function called \texttt{ft\_progress(lst)}.
The function will display the progress of a \texttt{for} loop.