Aller au contenu

Python - Bon départ

I. Parcours de listes et de chaînes de caractères⚓︎

Ce paragraphe a principalement été rédigé par Romain Janvier

Rappels sur les parcours de listes et de textes

En Python, il y a principalement 2 méthodes pour parcourir les listes Python (et donc les tableaux) et les textes.

Python
def parcours_par_valeur(donnees):
    for val in donnees:
        print(val)  # Quelque chose avec val
Python Console Session
>>> parcours_par_valeur([3, -2, 4])
3
-2
4
>>> parcours_par_valeur("AVION")
A
V
I
O
N
Python
def parcours_par_indice(donnees):
    for i in range(len(donnees)):
        print(i, donnees[i])  # Quelque chose avec donnees[i] et/ou i
Python Console Session
>>> parcours_par_indice([3, -2, 4])
0 3
1 -2
2 4
>>> parcours_par_indice("AVION")
0 A
1 V
2 I
3 O
4 N
Vous pouvez tester les deux fonctions ici

def parcours_par_valeur(donnees):
for val in donnees:
print(val) # Quelque chose avec val
def parcours_par_indice(donnees):
for i in range(len(donnees)):
print(i, donnees[i]) # Quelque chose avec donnees[i] et/ou i
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier

Lors d'un parcours par valeur, on n'a pas accès à l'indice mais uniquement à la valeur. Pour choisir entre les deux, il faut donc se poser la question suivante : Est-ce que j'ai besoin de connaître l'indice de la valeur que je regarde ?

  • Si la réponse est oui, il faut faire un parcours par indice.
  • Si la réponse est non, vous pouvez utiliser un parcours par valeur. Mais un parcours par indice fonctionnerait aussi.
Exercice 1 : compter(symbole, texte)

Compléter le code de la fonction compter qui prend en paramètre un texte d'un seul caractère symbole ainsi qu'un texte texte et qui renvoie le nombre de fois où symbole apparaît dans texte.

Python Console Session
>>> compter('b', 'bulle')
1
>>> compter('l', 'bulle')
2
>>> compter('v', 'bulle')
0

def compter(symbole, texte):
...
# Tests
assert compter('b', 'bulle') == 1
assert compter('l', 'bulle') == 2
assert compter('v', 'bulle') == 0
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uSws3/+frgebhpPic05a,onkyd1x)t050M0x0Q0G0C0b0q0d0D0b0G0q0q0h010Q0C0A010406050q0n0j0j0G0v0L040o0I0b0n0+0I0J050s0=0@0_0{0:0A04051b141e0s1b0:0M0C0i0Z0#0%0)0#0J0w0n0G0w0x0m0A0L0Q0z120d0z0C0w0z0b1G0z0Q0.050U0y0b0x1n0$0(011F1H1J1H0Q1P1R1N0Q0v1c1B0Z0~0q0A0G0J0)0k011T1p010u0W0x0J0G0j0x1N1/1;1_1V1|1R1 210.0a0d0B0v0I0A0I0q0C110J0d0S1-0v0v0x0D2m14240J1c0s1B2z1)1+1*1O0M261q0C0J1~2j1N1k1m0!1U2J2L0J0I2P1N0A2s1c2x2z2$0;1:2n2R1`2V0v0^0b1N0G1E2s0u0)030e0e0D2W0x1J2U0I0m0F0m0N0.0N140G2%2*0/2)252,1V2.2:2=2@0x2_012{2}2 312M340m1@040k3a3c1;3e2x2I013j0G2;1c2?0z2^2`2|2~0S3t2V3v0r0.0r3A2w3d0:3E3h0)3H3J053L3N3p3P3s2K3u350f0.0f3Y153!3f2+1o3i0I2/3I3l3M3n3O3r3R3;3T350F0.0F3`2$3#2*3F3)443-3q3Q304a33350l0.0l4g3d1f2!142P2C0M1+2H3%014p2O1l1c2Z0x2#4y3{3C054p4P250C0M0)2|2x3v373K0d4X4Z484q324$1^2a0x4+4p3S4s361N0s3b3}3F0K0.0S0u3Z4S3$3 0)0p0.0d542y4~4H0J0u0.2~0j0A0Q0x0v5c4V3~2S010-040c5p5e573G0.0q0L0j0y0~0x5x565s5u0H5p5b5J2-0.5m0O5m5I4j4H5u0P0g5p0:4R5d3E4*014!2*3v3x3+4)4Y5+4,4^5.4:204=5?4@4r5_2z3b0d635O5W5z0J5i5N5y5s0I0.0h6a5P1V0j0C0.0E5#5V4W5=5,1;3U3l5*3/494.353V0d4;4?3:6y0m3V610464653g5z50040u416g665s68040q6S6M6c59042K6Y5r5Q045S5U5%5q3F5u5!6.5$2(5)6q0e4#3?6u6`5~6F3@6B5{6D6x3=0m3@6I6K6K6b1`6O0C536.6L6)3i5B6(3F6d040h6f7i7d7l6W5D5F1R6o7k0)6;6n6.4~6v6{5-4c6~754-774d73217M5^7K7a7b7W7u3(697t6h0)7p7s2$7j4k7!7*7Y017p0t7n4H6j387E6^4j7H6|0m4u4(7H70774u7Q5|6w7N4b7 4{62647/6O2s0Q0n0v137#6T6*0D7`4Q4U4z4O4B4L140Q4E8x2F2A0G1Q8u0s4C5$0S0U0W0q04.

def compter(symbole, texte):
...
for s in texte:
if ...:
...
return ...
# Tests
assert compter('b', 'bulle') == 1
assert compter('l', 'bulle') == 2
assert compter('v', 'bulle') == 0
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uSws3/+frgebhpPic05a,onkyd1x)t050M0x0Q0G0C0b0q0d0D0b0G0q0q0h010Q0C0A010406050q0n0j0j0G0v0L040o0I0b0n0+0I0J050s0=0@0_0{0:0A04051b141e0s1b0:0M0C0i0Z0#0%0)0#0J0w0n0G0w0x0m0A0L0Q0z120d0z0C0w0z0b1G0z0Q0.050U0y0b0x1n0$0(011F1H1J1H0Q1P1R1N0Q0v1c1B0Z0~0q0A0G0J0)0k011T1p010u0W0x0J0G0j0x1N1/1;1_1V1|1R1 210.0a0d0B0v0I0A0I0q0C110J0d0S1-0v0v0x0D2m14240J1c0s1B2z1)1+1*1O0M261q0C0J1~2j1N1k1m0!1U2J2L0J0I2P1N0A2s1c2x2z2$0;1:2n2R1`2V0v0^0b1N0G1E2s0u0)030e0e0D2W0x1J2U0I0m0l0m0N0.0N140G2%2*0/2)252,1V2.2:2=2@0x2_012{2}2 312M340m1@040k3a3c1;3e2x2I013j0G2;1c2?0z2^2`2|2~0S3t2V3v0r0.0r3A2w3d0:3E3h0)3H3J053L3N3p3P3s2K3u350f0.0f3Y153!3f2+1o3i0I2/3I3l3M3n3O3r3R3;3T350F0.0F3`2$3#2*3F3)443-3q3Q304a33350l0.0l4g3d1f2!142P2C0M1+2H3%014p2O1l1c2Z0x2#4y3{3C054p4P250C0M0)2|2x3v373K0d4X4Z484q324$1^2a0x4+4p3S4s361N0s3b3}3F0K0.0S0u3Z4S3$3 0)0p0.0d542y4~4H0J0u0.2~0j0A0Q0x0v5c4V3~2S010-040c5p5e573G0.0q0L0j0y0~0x5x565s5u0H5p5b5J2-0.5m0O5m5I4j4H5u0P0g5p0:4R5d3E4*014!2*3v3x3+4)4Y5+4,4^5.4:204=5?4@4r5_2z3b0d635O5W5z0J5i5N5y5s0I0.0h6a5P1V0j0C0.0E5#5V4W5=5,1;3U3l5*3/494.353V0d4;4?3:6y0m3V610464653g5z50040u416g665s68040q6S6M6c59042K6Y5r5Q045S5U5%5q3F5u5!6.5$2(5)6q0e4#3?6u6`5~6F3@6B5{6D6x3=0m3@6I6K6K6b1`6O0C536.6L6)3i5B6(3F6d040h6f7i7d7l6W5D5F1R6o7k0)6;6n6.4~6v6{5-4c6~754-774d73217M5^7K7a7b7W7u3(697t6h0)7p7s2$7j4k7!7*7Y017p0t7n4H6j387E6^4j7H6|347L5}6E774u7Q5|6w7N4b7 7V637/6O2s0Q0n0v137#6T6*0D7`4Q4U4z4O4B4L140Q4E8v2F2A0G1Q8s0s4C5$0S0U0W0q04.
Exercice 2 : position(val, nombres)

Compléter le code de la fonction position qui prend en paramètre un entier val ainsi qu'une liste d'entiers nombres et renvoie l'indice de la première occurrence de val dans nombres s'il y en a une et None sinon.

Python Console Session
>>> position(7, [5, -1, 7, 4, 6, 4, 2])
2
>>> position(4, [5, -1, 7, 4, 6, 4, 2])
3
>>> position(0, [5, -1, 7, 4, 6, 4, 2])
None

def position(val, nombres):
...
# Tests
assert position(7, [5, -1, 7, 4, 6, 4, 2]) == 2
assert position(4, [5, -1, 7, 4, 6, 4, 2]) == 3
assert position(0, [5, -1, 7, 4, 6, 4, 2]) == None
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm2-uS8ws3/]frgebh[pPicN5a,onkyd1)t050N0x0Q0H0D0b0q0d0E0b0H0q0q0h010Q0D0B010406050q0m0j0j0H0v0M040n0J0b0m0+0J0K050s0=0@0_0{0:0B04051b141e0s1b0:0N0D0i0Z0#0%0)0#0K0w0m0H0w0x0l0B0M0Q0z120d0z0D0w0z0b1G0z0Q0.050U0y0b0x1n0$0(011F1H1J1H0Q1P1R1N0Q0v1c1B0Z0~0q0B0H0K0)0k011T1p010u0W0x0K0H0j0x1N1/1;1_1V1|1R1 210.0a0d0C0v0J0B0J0q0D110K0d0S1-0v0v0x0E2m14240K1c0s1B2z1)1+1*1O0N261q0D0K1~2j1N1k1m0!1U2J2L0K0J2P1N0B2s1c2x2z2$0;1:2n2R1`2V0v0^0b1N0H1E2s0u0)030e0e0E2W0x1J2U0J0l0o0l0O0.0O140H2%2*0/2)252,1V2.2:2=2@0x2_012{2}2 312M340l1@040k3a3c1;3e2x2I013j0H2;1c2?0z2^2`2|2~0S3t2V3v0r0.0r3A2w3d0:3E3h0)3H3J053L3N3p3P3s2K3u350f0.0f3Y153!3f2+1o3i0J2/3I3l3M3n3O3r3R3;3T350G0.0G3`2(1h2!142P2C0N1+2H3%013Q221c4l1d4j4h2(4s2#2*0d0D0N0)2|2x3v373K4D4F013/49324J1^2a0x4G48304a33354K3Y3$3 0)0L0.0S0u3Z3C3}3F0p0.0d4/2y4;4q0K0u0.2i2k2v3{4:4(2S010-040c4_4B3g4)3G0.0i3I5b4{5e580I5b4^552-0.2V0j0y2s0q5j5q1V580P0g5b0:534`3E4E4W4I353x3+4M4W4s3S4!3w4T204V4O4X5S3v5N0s3b0d5)5p4C4q4+040u415o5k560K0.0D5=5z0)0J4?042K5{5,5e0K0y0.0v1;1w5y6356585a5G5c3~5@6604296b5d6d0.6f2(5|5f045t5v0x5x6g5?1`5B5C5E6n2n5J5Y5L0l3V4L6I4P4Y4R353V0d4U5Q3:6R6L1N5%045*6%5+6o1`5.0D4.6g6)6i5r6v0J5u5w6G3F580A6`4|5_6~5l0.0t626*1V0J0.0h0h756;3i5g5i6A6t585D6g5F6s4C6O0e6K3@6N4N6P5!3?5V216W4Q3=0l7s3A6(7G6:3F5.2s0Q0m0v136/6B7e606F7h7o7u7q2*3v4d7t7A6Q7C4d6U5W7%7w0l7#7F5*7R4*670T7N7P2$7I5-0E0.0F120x7U4h4A1f4j0s4v2A4n142D8d0H1Q0x2z4l5F0S0U0W0q04.

def position(val, nombres):
for i in ...:
if ...:
return ...
return ...
# Tests
assert position(7, [5, -1, 7, 4, 6, 4, 2]) == 2
assert position(4, [5, -1, 7, 4, 6, 4, 2]) == 3
assert position(0, [5, -1, 7, 4, 6, 4, 2]) == None
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l(9 _4:=vm2-uSws3/]frgebh[pPicN5a,onkyd1)t050N0x0Q0H0D0b0q0e0E0b0H0q0q0i010Q0D0B010406050q0n0k0k0H0v0M040o0J0b0n0+0J0K050s0=0@0_0{0:0B04051b141e0s1b0:0N0D0j0Z0#0%0)0#0K0w0n0H0w0x0m0B0M0Q0z120e0z0D0w0z0b1G0z0Q0.050U0y0b0x1n0$0(011F1H1J1H0Q1P1R1N0Q0v1c1B0Z0~0q0B0H0K0)0l011T1p010u0W0x0K0H0k0x1N1/1;1_1V1|1R1 210.0a0e0C0v0J0B0J0q0D110K0e0S1-0v0v0x0E2m14240K1c0s1B2z1)1+1*1O0N261q0D0K1~2j1N1k1m0!1U2J2L0K0J2P1N0B2s1c2x2z2$0;1:2n2R1`2V0v0^0b1N0H1E2s0u0)030f0f0E2W0x1J2U0J0m0d0m0O0.0O140H2%2*0/2)252,1V2.2:2=2@0x2_012{2}2 312M340m1@040l3a3c1;3e2x2I013j0H2;1c2?0z2^2`2|2~0S3t2V3v0r0.0r3A2w3d0:3E3h0)3H3J053L3N3p3P3s2K3u350g0.0g3Y153!3f2+1o3i0J2/3I3l3M3n3O3r3R3;3T350G0.0G3`2(1h2!142P2C0N1+2H3%013Q221c4l1d4j4h2(4s2#2*0e0D0N0)2|2x3v373K4D4F013/49324J1^2a0x4G48304a33354K3Y3$3 0)0L0.0S0u3Z3C3}3F0p0.0e4/2y4;4q0K0u0.2i2k2v3{4:4(2S010-040c4_4B3g4)3G0.0j3I5b4{5e580I5b4^552-0.2V0k0y2s0q5j5q1V580P0h5b0:534`3E4E4W4I353x3+4M4W4s3S4!3w4T204V4O4X5S3v5N0s3b0e5)5p4C4q4+040u415o5k560K0.0D5=5z0)0J4?042K5{5,5e0K0y0.0v1;1w5y6356585a5G5c3~5@6604296b5d6d0.6f2(5|5f045t5v0x5x6g5?1`5B5C5E6n2n5J5Y5L0m3V4L6I4P4Y4R353V0e4U5Q3:6R6L1N5%045*6%5+6o1`5.0D4.6g6)6i5r6v0J5u5w6G3F580A6`4|5_6~5l0.0t626*1V0J0.0i0i756;3i5g5i6A6t585D6g5F6s4C6O0f6K3@6N4N6P5!3?5V216W4Q3=0m7s3A6(7G6:3F5.2s0Q0n0v136/6B7e606F7h7o7u7q2*3v4d7t7A6Q7C4d6U5W7%7w0m7#7F5*7R4*670T7N7P2$7I5-0E0.0F120x7U4h4A1f4j0s4v2A4n142D8d0H1Q0x2z4l5F0S0U0W0q04.
Sortie anticipée

Dans cet exercice nous avons un return dans la boucle. il y a donc sortie anticipée de la boucle s'il n'est pas nécessaire de la terminer.

Ce procédé est souvent utilisé et permet de gagner en efficacité.

Parcours en sens inverse

Il est parfois utile de parcourir une liste Python, ou un texte, en partant du dernier élément et en allant vers le premier. On rappelle que l'indice du dernier élément est n-1, où n est la longueur de la liste ou du texte.

On peut parcourir les indices dans l'ordre inverse en utilisant les paramètres supplémentaires de range. Pour rappel, range(de, vers, pas) part de de et va de pas en pas jusqu'à vers, en s'arrêtant juste avant de l'atteindre. En mettant -1 pour le pas, on parcourt les nombres dans l'ordre inverse.

Python
def parcours_inverse1(donnees):
    for i in range(len(donnees)-1, -1, -1): # on part du dernier indice 
        print(i, donnees[i])
Python Console Session
>>> parcours_inverse1([3, -2, 4])
2 4
1 -2
0 3
>>> parcours_inverse1("AVION")
4 N
3 O
2 I
1 V
0 A

Si on ne veut pas utiliser les paramètres supplémentaires de range, il faut calculer l'indice de l'élément regardé à chaque tour de boucle. Si on note i la variable de boucle, n la longueur de la liste ou du texte, alors à chaque tour de boucle, il faut regarder l'élément d'indice n-1-i.

Python
def parcours_inverse2(donnees):
    n = len(donnees)
    for i in range(n):
        indice = n - 1 - i
        print(indice, donnees[indice])
Python Console Session
>>> parcours_inverse2([3, -2, 4])
2 4
1 -2
0 3
>>> parcours_inverse2("AVION")
4 N
3 O
2 I
1 V
0 A
Vous pouvez tester les deux fonctions ici

def parcours_inverse1(donnees):
for i in range(len(donnees)-1, -1, -1): # on part du dernier indice
print(i, donnees[i])
def parcours_inverse2(donnees):
n = len(donnees)
for i in range(n):
indice = n - 1 - i
print(indice, donnees[indice])
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier

Vous pouvez utiliser ces parcours, ou pas, pour l'exercice suivant.

Exercice 3 : derniere_position(val, nombres)

Compléter le code de la fonction derniere_position qui prend en paramètre un entier val ainsi qu'une liste d'entiers nombres et renvoie l'indice de la dernière occurrence de val dans nombres s'il y en a une et None sinon.

Python Console Session
>>> derniere_position(7, [5, -1, 7, 4, 6, 4, 2])
2
>>> derniere_position(4, [5, -1, 7, 4, 6, 4, 2])
5
>>> derniere_position(0, [5, -1, 7, 4, 6, 4, 2])
None

def derniere_position(val, nombres):
...
# Tests
assert derniere_position(7, [5, -1, 7, 4, 6, 4, 2]) == 2
assert derniere_position(4, [5, -1, 7, 4, 6, 4, 2]) == 5
assert derniere_position(0, [5, -1, 7, 4, 6, 4, 2]) == None
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm2-uSws3/]frgebh[pPicN5a,onkyd1)t050M0w0P0G0C0b0p0d0D0b0G0p0p0h010P0C0A010406050p0m0j0j0G0u0L040n0I0b0m0*0I0J050r0;0?0^0`0/0A04051a131d0r1a0/0M0C0i0Y0!0$0(0!0J0v0m0G0v0w0l0A0L0P0y110d0y0C0v0y0b1F0y0P0-050T0x0b0w1m0#0%011E1G1I1G0P1O1Q1M0P0u1b1A0Y0}0p0A0G0J0(0k011S1o010t0V0w0J0G0j0w1M1.1:1^1U1{1Q1~200-0a0d0B0u0I0A0I0p0C100J0d0R1,0u0u0w0D2l13230J1b0r1A2y1(1*1)1N0M251p0C0J1}2i1M1j1l0Z1T2I2K0J0I2O1M0A2r1b2w2y2#0:1/2m2Q1_2U0u0@0b1M0G1D2r0t0(030e0e0D2V0w1I2T0I0l0N0F330-0N130G2$2)0.2(242+1U2-2/2;2?0w2^012`2|2~302L33351?040k393b1:3d2w2H013i0G2:1b2=0y2@2_2{2}0R3s2U3u0l0q0-0q3z2v3c0/3D3g0(3G3I053K3M3o3O3r2J3t340l0f0-0f3Y143!3e2*1n3h0I2.3H3k3L3m3N3q3Q3;3S3?0F0-0F3{2%1g2Z132O2B0M1*2G3%013P211b4m1c4k4i2%4t2!2)0d0C0M0(2{2w3T0N3k4F4H492 4b323?4L0d290w4O4t3R4S354L2y3a3~3E0K0-0R0t3Z3B4*4r0o0-0d4:2x4=403(0t4-0w0u0J0C510w0e2h2j2u3|4;3$4}010,040c4`4C3f5e0J0-0i3H5j4|2R5f0-0H5j4_5d5t5n042U0j0x2r0p5r5z1_5g0O0g5j0/5b4{3D4N014I2)3T3w3+4E4G5U4P4!5X1@4W4Y3:315)4(040d5=5y4D4r4,040t425x5s2,0-0C5~5J1U0I4@042J635^5m0x0-0u1:1v5I6b5t5g5i5Q5k3 5A6d04286i5l6k0-6m2%643(0-5D5F0w5H6n5 1U5L6u6p1_0I0-0l6L3E0j0C376R4r5g5w6n5@6v6N6P6W5e6T6V6H6A5u046Z2#6#6M656(6-6j1_6+04386`6$6J0-5M5O6R5T5V1:3T3V3J5!5,4a5.3?3V4V1 4X5$4Z4R7a1M0r3a5?7t6?4+614/6!6I6B5C0I5E5G6)6w040z7G60687K72040s6a710(6O040h0h7R6@7B5p0b7N0(5g5N6n5P6z4D770e4J3?3^7c7/7n7g353^7j207e4Q7`3@7q7s7u857A015`2r0P0m527Y3E5B627+765#780J3T4e7@8k5%7o4d5*7k7 5(8t5:7t87890S8c127z6.0K0D0-0E110w756H0r4B1e4k0r4w2z4o132C8Y0G1P0w2y4m5P0R0T0V0p04.

def derniere_position(val, nombres):
for i in range(...): # On peut mettre plusieurs paramètres
if ...:
...
return ...
# Tests
assert derniere_position(7, [5, -1, 7, 4, 6, 4, 2]) == 2
assert derniere_position(4, [5, -1, 7, 4, 6, 4, 2]) == 5
assert derniere_position(0, [5, -1, 7, 4, 6, 4, 2]) == None
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uSws3/]frgebh[pPicN5a,onkyd1)t050N0x0Q0H0D0b0q0d0E0b0H0q0q0h010Q0D0B010406050q0n0j0j0H0v0M040o0J0b0n0+0J0K050s0=0@0_0{0:0B04051b141e0s1b0:0N0D0i0Z0#0%0)0#0K0w0n0H0w0x0m0B0M0Q0z120d0z0D0w0z0b1G0z0Q0.050U0y0b0x1n0$0(011F1H1J1H0Q1P1R1N0Q0v1c1B0Z0~0q0B0H0K0)0k011T1p010u0W0x0K0H0j0x1N1/1;1_1V1|1R1 210.0a0d0C0v0J0B0J0q0D110K0d0S1-0v0v0x0E2m14240K1c0s1B2z1)1+1*1O0N261q0D0K1~2j1N1k1m0!1U2J2L0K0J2P1N0B2s1c2x2z2$0;1:2n2R1`2V0v0^0b1N0H1E2s0u0)030e0e0E2W0x1J2U0J0m0O0l340.0O140H2%2*0/2)252,1V2.2:2=2@0x2_012{2}2 312M34361@040k3a3c1;3e2x2I013j0H2;1c2?0z2^2`2|2~0S3t2V3v0m0r0.0r3A2w3d0:3E3h0)3H3J053L3N3p3P3s2K3u350m0f0.0f3Z153#3f2+1o3i0J2/3I3l3M3n3O3r3R3=3T3@0G0.0G3|2(1h2!142P2C0N1+2H3(013Q221c4n1d4l4j2(4u2#2*0d0D0N0)2|2x3U0O3l4G4I4a304c333@4M0d2a0x4P4u3S4T364M2z3b3 3F0L0.0S0u3!3C4+4s0p0.0d4;2y4?413)0u4.0x0v0K0D520x0e2i2k2v3}4=3%4~010-040c4{4D3g5f0K0.0i3I5k4}2S5g0.0I5k4`5e5u5o042V0j0y2s0q5s5A1`5h0P0g5k0:5c4|3E4O014J2*3U3x3,4F4H5V4Q4#5Y1^4X4Z3;325*4)040d5?5z4E4s4-040u435y5t2-0.0D5 5K1V0J4^042K645_5n0y0.0v1;1w5J6c5u5h5j5R5l405B6e04296j5m6l0.6n2(653)0.5E5G0x5I6o601V5M6v6q1`0J0.0m6M3F0j0D386S4s5h5x6o5^6w6O6Q6X5f6U6W6I6B5v046!2$6$6N666)6.6k1`6,04396{6%6K0.5N5P6S5U5W1;3U3W3K5#5-4b5/3@3W4W204Y5%4!4S7b1N0s3b5@7u6@4,624:6#6J6C5D0J5F5H6*6x040A7H61697L73040t6b720)6P040h0h7S6^7C5q0b7O0)5h5O6o5Q6A4E780e4K3@3_7d7:7o7h363_7k217f4R7{3^7r7t7v867B015{2s0Q0n537Z3F5C637,775$790K3U4f7^8l5(7p4e5+7l805)8u5;7u888a0T8d137A6/0L0E0.0F120x766I0s4C1f4l0s4x2A4p142D8Z0H1Q0x2z4n5Q0S0U0W0q04.

def derniere_position(val, nombres):
...
for i in range(len(nombres)):
if nombres[i] == val:
...
return ...
# Tests
assert derniere_position(7, [5, -1, 7, 4, 6, 4, 2]) == 2
assert derniere_position(4, [5, -1, 7, 4, 6, 4, 2]) == 5
assert derniere_position(0, [5, -1, 7, 4, 6, 4, 2]) == None
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uSws3/]fr7gebh[pPicN5a,onkyd1)t050O0y0R0I0E0b0q0d0F0b0I0q0q0h010R0E0C010406050q0n0j0j0I0v0N040o0K0b0n0,0K0L050s0?0^0`0|0;0C04051c151f0s1c0;0O0E0i0!0$0(0*0$0L0x0n0I0x0y0m0C0N0R0A130d0A0E0x0A0b1H0A0R0/050V0z0b0y1o0%0)011G1I1K1I0R1Q1S1O0R0v1d1C0!0 0q0C0I0L0*0k011U1q010u0X0y0L0I0j0y1O1:1=1`1W1}1S20220/0a0d0D0v0K0C0K0q0E120L0d0T1.0v0v0y0F2n15250L1d0s1C2A1*1,1+1P0O271r0E0L1 2k1O1l1n0#1V2K2M0L0K2Q1O0C2t1d2y2A2%0=1;2o2S1{2W0v0_0b1O0I1F2t0u0*030e0e0F2X0y1K2V0K0m0P0w350/0P150I2(2+0:2*262-1W2/2;2?2^0y2`012|2~30322N35371^040k3b3d1=3f2y2J013k0I2=1d2@0A2_2{2}2 0T3u2W3w0m0r0/0r3B2x3e0;3F3i0*3I3K053M3O3q3Q3t2L3v360m0f0/0f3!163$3g2,1p3j0K2:3J3m3N3o3P3s3S3?3U3^0H0/0H3}2%3%2+3G3+473/3r3R314d343^0l0/0l4j3e1g2#152Q2D0O1,2I3)014s2P1m1d2!0y2$4B3~3D054s4S260E0O0*2}2y3V0P3m4!4$4b4t334)1_2b0y4-4s3T4v374*2A3c403G0M0/0T0u3#4V3(420*0p0/0d562z504K0L0u530y0v0L0E5l0y0e2j2l2w4U5f582T010.040c5e4Y415y0L0/0i3J5D5g595z0/0J5D5d5x2.0/2W0j0z2t0q5L5T1W5A0Q0g5D0;5v5E0d4,014%2+3V3y3-5/4#5;4.4`5@4=214@5|4_4u5 4~040d695S4m5h0/2t2j0L0q0y5R5M5y0K0/0h6k5$0*0M0F0/0G136j5-5,2)3F5:5=1=3V3X3L5`4^3=4:3^3X0d4?6K4c6M376H3B6a6b3h5N52040u446q6c5N5H040E6)6Z6m5b6-145-6Y5F2.0z6e1=1x5#6*5y5A5C5-6l6{0/2a706:1{737a6`3j5V0K5X5Z7e3G5(5)5+7l6D0e4(3^3`6I7r646T3_60226R4/3@377v6W6X6a761W6#0E556^7L3*7h7j0y5!756r5O040B7l6d6-7$5N5A0t6/7f0*6n040h6p7Q7Y6,5J0b7)720/5*6z7q5{6E0L3V4g7w825}654f7B623;6S7F0m867I7J7J7R3H6e0y6g6i7-3G7:7?2%6_4n0/6.807X4m7r7t374x877D5~4w8c8J8a8G1O0s3c6X8m6#2t0R0n5m8s7%6f138r8B2)0s4X4C4R4E4O150R4H8;2G2B0I1R8.0s4F5,0T0V0X0q04.
Exercice 4 : indice_egal_valeurs(nombres)

Compléter le code de la fonction indice_egal_valeurs qui prend en paramètre une liste d'entiers nombres et renvoie True s'il existe un indice i tel que nombres[i]==i et False sinon.

Python Console Session
>>> indice_egal_valeur([7, 1, 8])  # nombres[1] = 1
True
>>> indice_egal_valeur([9, -7, 2, 9, 6])  # nombres[2] = 2
True
>>> indice_egal_valeur([1, 2, 3, 4])
False

def indice_egal_valeur(nombres):
...
# Tests
assert indice_egal_valeur([7, 1, 8])
assert indice_egal_valeur([9, -7, 2, 9, 6])
assert not indice_egal_valeur([1, 2, 3, 4])
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l(9 _4:=vTm2-uSws3/]frgebh[pPic5aonkFyd1)t050N0y0Q0H0E0b0r0e0F0b0H0r0r0i010Q0E0C010406050r0o0l0l0H0w0M040p0I0b0o0+0I0J050t0=0@0_0{0:0C04051b141e0t1b0:0N0E0j0Z0#0%0)0#0J0x0o0H0x0y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f7s7$7u7C364f7y6T3:7B6W0n7*6Z5)6^015-2s0Q7O7b7K1V0K7T040L3I0r7Y7j5o0t4C1f4l0t4x2A4p142D8n0H1Q0y2z4n5F0S0U0W0r04.

def indice_egal_valeur(nombres):
for i in ...:
if ...:
return ...
return ...
# Tests
assert indice_egal_valeur([7, 1, 8])
assert indice_egal_valeur([9, -7, 2, 9, 6])
assert not indice_egal_valeur([1, 2, 3, 4])
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vTm2-uSws3/]frgebh[pPic05aonkFyd1)t050N0x0Q0H0D0b0q0d0E0b0H0q0q0h010Q0D0B010406050q0n0k0k0H0v0M040o0I0b0n0+0I0J050s0=0@0_0{0:0B04051b141e0s1b0:0N0D0i0Z0#0%0)0#0J0w0n0H0w0x0m0B0M0Q0z120d0z0D0w0z0b1G0z0Q0.050U0y0b0x1n0$0(011F1H1J1H0Q1P1R1N0Q0v1c1B0Z0~0q0B0H0J0)0l011T1p010u0W0x0J0H0k0x1N1/1;1_1V1|1R1 210.0a0d0C0v0I0B0I0q0D110J0d0S1-0v0v0x0E2m14240J1c0s1B2z1)1+1*1O0N261q0D0J1~2j1N1k1m0!1U2J2L0J0I2P1N0B2s1c2x2z2$0;1:2n2R1`2V0v0^0b1N0H1E2s0u0)030e0e0E2W0x1J2U0I0m0l0F0m0O0.0O140H2%2*0/2)252,1V2.2:2=2@0x2_012{2}2 312M34361@040l3b3d1;3f2x2I013k0H2;1c2?0z2^2`2|2~0S3u2V3w0m0r0.0r3B2w3e0:3F3i0)3I3K053M3O3q3Q3t2K3v350m0f0.0f3!153$3g2+1o3j0I2/3J3m3N3o3P3s3S3?3U3^0G0.0G3}2(1h2!142P2C0N1+2H3)013R221c4o1d4m4k2(4v2#2*0d0D0N0)2|2x3V383L4G4I013;4c324M1^2a0x4J4b304d333^4N3!3(420)0K0.0S0u3#3D403G0p0.0d4=2y4@4t0J0u0.2K1k0E0x0e0x0w3J0e0i3J0x0n0v4|4E3h4,010-040c5h4~5k0J0.2V0k0y2s0q5p4+2S5l0.0P0g5h0:3~4?3F4H4Z4L3^3y3-4P4Z4v3T4%3x4W204Y4R4!5U3V5P0s3c0d5+4{5A1`4.040u445h5-4F4 525@5q5B0I4_042K5|5.3j0y0.0v1;1w5z5_5k5m5o5I4}643*6604296b5j5B6e6n415B5s045u5w0x5y6g5i6s1`5m0P5E5G6r5R5!5N363X4O5L5!5T4$3V3X0d4X5S3=4U3^6N3B5,6%5^6o5/524;6A6)6C3j5t0I5v5x6I4t5m0A6_5r5{6A5}6D0.0t636c5~0.0h0h756*6;616}6p0.5F6A5H2(5K4Q4K2*3V3`6O7n5#6S3^3`6V5Y6X4T3@367r6$6(7G711V5:2s0Q5f136.7I4-0E0.0j0v0n0x6H707m5M7p4f3m6P4S4#6Z364g7y217A7+7C0m4g2z5*5,7Q017K0T7N7b6:7R0.0L3J0q7X7j5p0s4D1f4m0s4y2A4q142D8j0H1Q0x2z4o5H0S0U0W0q04.
Exercice 5 : moyenne(valeurs)

Compléter le code de la fonction moyenne qui prend en paramètre une liste non vide de nombres valeurs et renvoie la moyenne de ces nombres. On n'utilisera pas la fonction len.

Comparaison de nombres réels

On rappelle que la représentation des nombres réels en Python est basée sur le principe de la virgule flottante et qu'à cause de cela, deux expressions mathématiquement égales ne donnent pas forcément le même résultat en Python.

Pour vérifier le résultat de la fonction moyenne avec le résultat attendu, nous utiiserons la fonction indiscernables qui prend deux nombres et renvoie un booléen indiquant si la distance entre ces deux nombres (mathématiquement la valeur absolue de leur différence) est inférieure à 1015.

Python Console Session
>>> 0.1 * 3 == 0.3  # Erreur de virgule flottante classique
False
>>> indiscernables(0.1*3, 0.3)
True
Python Console Session
>>> moyenne([5])
5.0
>>> moyenne([5, 15, 8])
9.333333333333334
>>> moyenne([5, 15, 10])
10.0

def indiscernables(nombre1, nombre2):
return abs(nombre1 - nombre2) < 1e-15
def moyenne(valeurs):
...
# Tests
assert indiscernables(moyenne([5]), 5.0)
assert indiscernables(moyenne([5, 15, 8]), 9.333333333333334)
assert indiscernables(moyenne([5, 15, 10]), 10.0)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l(9 _4:;=vm26-uS8ws3/+fr7gebhp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666x6z2:0C0;0K0C6a6P716J0;6g786^3M6B6D0B6q2)6@594!0M0;0o707e6k046m6o0B6F2+7e6K7r7m4_7o04020b0T0i7C4Z4_0l5 040R1A0R4n6P6O7z4r6Z0f5Q3}3$5T5N6+5%523#6(245$4Q6$7-5*3j7d7Y7)6U1@3!3 7(7:4,7=3 0e5!816#4j397 3)7e6`5:6h7D1}5_3D8g7M5~0;0l0M0P212O8l5}7a6f8u4s0;0k3O0B6~777X8m8w6L6N8y7Z7#394l807*7;890o4l856)875X4k1Q6:3D6?793/0;0t6C247L8v0,7F0j8/3L0l0G0;0I8K7_5L7{7!6V4B3q7Z7+7=4C8W7/8R828T4C7@6?7l8H8*7u0C0f2Y0T698@5a8=9q4!8_8{8}8G2q8M92394T8Q6!8Z9C7.6*9F6-9H9f8(8d0;0x499t6A7u0M9o9T1}0M8j2N9Y3n8A8C8E8y5a6e6M7V8L908N0o4/9E6,7,4/999J9`7=9^3G9g9g8)7f048,0`0B9%8;0;8?6P9h8:a6a88.afa57F0waba69naa9:8~9z9=9B0o549_968T549}8Y9Lay8#6;a36=a57t0N9lar8F3iag3L9sal7s6laP9W9paW8h1Yanap9v7Q9x5F3K9A7}3}5k9490aB5i3a5laE9b88a`a?9NaL9P6{0W6~16a$9iai8-as7kam0;0vapaNaZ9oaR4X6b0v5J5p5D5r5A170T5ubv2I2D751U2C5s6O0V0X0Z0t04.

def indiscernables(nombre1, nombre2):
return abs(nombre1 - nombre2) < 1e-15
def moyenne(valeurs):
somme = ...
nb_notes = ...
for note in valeurs:
somme = ...
nb_notes = ...
return ...
# Tests
assert indiscernables(moyenne([5]), 5.0)
assert indiscernables(moyenne([5, 15, 8]), 9.333333333333334)
assert indiscernables(moyenne([5, 15, 10]), 10.0)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l(9 _4:;=vm26-uS8ws3/+fr7gebhpPic05a,onkyd1)t050Q0B0T0K0G0b0t0e0H0b0K0t0t0j010T0G0E010406050t0p0l0l0K0y0P040q0M0b0p0.0M0N050v0^0`0|0~0?0E04051e171h0v1e0?0Q0G0k0$0(0*0,0(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$5T5N6+5%523#6(245$4Q6$7-5*3j7d7Y7)6U1@3!3 7(7:4,7=3 0e5!816#4j397 3)7e6`5:6h7D1}5_3D8g7M5~0;0l0M0P212O8l5}7a6f8u4s0;0k3O0B6~777X8m8w6L6N8y7Z7#394l807*7;890o4l856)875X4k1Q6:3D6?793/0;0t6C247L8v0,7F0j8/3L0l0G0;0I8K7_5L7{7!6V4B3q7Z7+7=4C8W7/8R828T4C7@6?7l8H8*7u0C0f2Y0T698@5a8=9q4!8_8{8}8G2q8M92394T8Q6!8Z9C7.6*9F6-9H9f8(8d0;0x499t6A7u0M9o9T1}0M8j2N9Y3n8A8C8E8y5a6e6M7V8L908N0o4/9E6,7,4/999J9`7=9^3G9g9g8)7f048,0`0B9%8;0;8?6P9h8:a6a88.afa57F0waba69naa9:8~9z9=9B0o549_968T549}8Y9Lay8#6;a36=a57t0N9lar8F3iag3L9sal7s6laP9W9paW8h1Yanap9v7Q9x5F3K9A7}3}5k9490aB5i3a5laE9b88a`a?9NaL9P6{0W6~16a$9iai8-as7kam0;0vapaNaZ9oaR4X6b0v5J5p5D5r5A170T5ubv2I2D751U2C5s6O0V0X0Z0t04.
Exercice 6 : moyenne_ponderee(valeurs)

Compléter le code de la fonction moyenne_ponderee qui prend en paramètre une liste non vide valeurs de couples de nombres (note, coeff) et renvoie la moyenne pondérée correspondant à ces notes. On suppose que tous les coefficients sont positifs et qu'il y a au moins un coefficient non nul.

Calcul d'une moyenne pondérée

Pour calculer une moyenne pondérée, il faut aditionner le produit de chacune des notes avec le coefficient correspondant et diviser par la somme des coefficients.

Par exemple, avec 5 notes n1, n2, ..., n5 et les coefficients correspondants c1, c2, ..., c5, la moyenne pondérée est :

n1×c1+n2×c2+n3×c3+n4×c4+n5×c5c1+c2+c3+c4+c5

Parcours d'une liste de couples

On rappelle que lors d'un parcours par valeur d'une liste composée de couples, on peut décomposer chacun des couples de la manière suivante :

Python
def parcours_liste_couples(couples):
    for a, b in couples: # 
        print(a, b)
Python Console Session
>>> parcours_liste_couples([("voiture", 3), ("vélo", 5), ("moto", 1)])
voiture 3
vélo 5
moto 1
Python Console Session
>>> moyenne_ponderee([(5, 1), (15, 1)])
10.0
>>> moyenne_ponderee([(5, 1), (15, 2)]) # 5*1 + 15*2 = 35 -> 35/3
11.666666666666666
>>> moyenne_ponderee([(5, 1), (15, 3)]) # 5*1 + 15*3 = 50 -> 50/4
12.5
>>> moyenne_ponderee([(5, 1), (15, 3), (20, 0)]) # le coeff 0 ne compte pas
12.5

def indiscernables(nombre1, nombre2):
return abs(nombre1 - nombre2) < 1e-15
def moyenne_ponderee(valeurs):
...
# Tests
assert indiscernables(moyenne_ponderee([(5, 1), (15, 1)]), 10.0)
assert indiscernables(moyenne_ponderee([(5, 1), (15, 2)]), 11.666666666666666)
assert indiscernables(moyenne_ponderee([(5, 1), (15, 3)]), 12.5)
assert indiscernables(moyenne_ponderee([(5, 1), (15, 3), (20, 0)]), 12.5)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l(9 _4:;=vm26-uS8ws*3/+fr7gebhpPic05a,onkyd1)t050R0C0U0L0H0b0t0e0I0b0L0t0t0j010U0H0F010406050t0p0l0l0L0z0Q040q0N0b0p0/0N0O050w0_0{0}0 0@0F04051f181i0w1f0@0R0H0k0%0)0+0-0)0O0B0p0L0B0C0o0F0Q0U0E160e0E0H0B0E0b1K0E0U0=050Y0D0b0C1r0*0,011J1L1N1L0U1T1V1R0U0z1g1F0%120t0F0L0O0-0m011X1t010y0!0C0O0L0l0C1R1?1^1}1Z201V23250=0a0e0G0z0N0F0N0t0H150O0e0W1;0z0z0C0I2q18280O1g0w1F2D1-1/1.1S0R2a1u0H0O222n1R1o1q0(1Y2N2P0O0N2T1R0F2w1g2B2D2*0^1@2r2V1~2Z0z0|0b1R0L1I2w0y0-030f0f0I2!0C1N2Y0N0o0m0r0o0S0=0e0S180L2+2.0?2-292:1Z2=2@2_2{0C2}012 3133352Q383a1{040e0m3g3i1^3k2B2M013p0L2^1g2`0E2|2~30320W3z2Z3B0o0v3d0v3H2A3j0@3L3n0-3O3Q053S3U3v3W3y2O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9s9s8;7g9w8^9z9:9D3J9F3M9I6QaD5b7u9x8_aAa2900=0xaNaw9:a97o0=0uaU9_9{0y5=ad9=6Saf9R0o55aj9i8#55an8*9#a,8-6=at6?avaJay9Aa#aB2CaHaV0492aGa}8?a a!9CaR7GaQb87t0=bc9N5G3L9Q7~3~5l9g9ca/5j3b5ma=9n89bvbr9%a|9)6|0X6 17bh8ia3ax9y8`0N8|b28l7f7G0waRa~bObcbS7X5G0w5K5q5E5s5B180U5vb.2J2E761V2D5t6P0W0Y0!0t04.

def indiscernables(nombre1, nombre2):
return abs(nombre1 - nombre2) < 1e-15
def moyenne_ponderee(resultats):
somme_notes = ...
somme_coeffs = ...
for note, coeff in resultats:
...
...
return ...
# Tests
assert indiscernables(moyenne_ponderee([(5, 1), (15, 1)]), 10.0)
assert indiscernables(moyenne_ponderee([(5, 1), (15, 2)]), 11.666666666666666)
assert indiscernables(moyenne_ponderee([(5, 1), (15, 3)]), 12.5)
assert indiscernables(moyenne_ponderee([(5, 1), (15, 3), (20, 0)]), 12.5)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l(9 _4:;=vm26-uS8ws*3/+fr7gebhpPic05a,onkyd1)t050R0C0U0L0H0b0t0e0I0b0L0t0t0j010U0H0F010406050t0p0l0l0L0z0Q040q0N0b0p0/0N0O050w0_0{0}0 0@0F04051f181i0w1f0@0R0H0k0%0)0+0-0)0O0B0p0L0B0C0o0F0Q0U0E160e0E0H0B0E0b1K0E0U0=050Y0D0b0C1r0*0,011J1L1N1L0U1T1V1R0U0z1g1F0%120t0F0L0O0-0m011X1t010y0!0C0O0L0l0C1R1?1^1}1Z201V23250=0a0e0G0z0N0F0N0t0H150O0e0W1;0z0z0C0I2q18280O1g0w1F2D1-1/1.1S0R2a1u0H0O222n1R1o1q0(1Y2N2P0O0N2T1R0F2w1g2B2D2*0^1@2r2V1~2Z0z0|0b1R0L1I2w0y0-030f0f0I2!0C1N2Y0N0o0m0d0o0S0=0e0S180L2+2.0?2-292:1Z2=2@2_2{0C2}012 3133352Q383a1{040e0m3g3i1^3k2B2M013p0L2^1g2`0E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}3o8H6a0p8K8M8F5b6f6N7W8T9c8V0o4:9U6-7-4:9l9Zak7?ai3H9s9s8;7g9w8^9z9:9D3J9F3M9I6QaD5b7u9x8_aAa2900=0xaNaw9:a97o0=0uaU9_9{0y5=ad9=6Saf9R0o55aj9i8#55an8*9#a,8-6=at6?avaJay9Aa#aB2CaHaV0492aGa}8?a a!9CaR7GaQb87t0=bc9N5G3L9Q7~3~5l9g9ca/5j3b5ma=9n89bvbr9%a|9)6|0X6 17bh8ia3ax9y8`0N8|b28l7f7G0waRa~bObcbS7X5G0w5K5q5E5s5B180U5vb.2J2E761V2D5t6P0W0Y0!0t04.
Exercice 7 : effectifs(donnees)

Compléter le code de la fonction effectifs qui prend en paramètre une liste donnees contenant des entiers ou des textes et qui renvoie un dictionnaire qui associe à chaque valeur apparaissant dans donnees le nombre de fois où elle y apparaît (c'est à dire le nombre d'occurences de valeur dans donnees).

Par exemple dans la liste [4, 1, 2, 4, 2, 2, 6] il y a une fois la valeur 1, trois fois la valeur 2, deux fois la valeur 4 et une fois la valeur 6. Le résultat attendu pour cette liste sera donc le dictionnaire {1: 1, 2: 3, 4: 2, 6: 1}.

On rappelle aussi que dans un dictionnaire, l'ordre dans lequel on donne les valeurs n'a pas d'importance. Ainsi, les dictionnaires {1: 1, 2: 3, 4: 2, 6: 1} et {4: 2, 1: 1, 6: 1, 2: 3} sont égaux.

Python Console Session
>>> effectifs([4, 1, 2, 4, 2, 2, 6])
{4: 2, 1: 1, 2: 3, 6: 1}
>>> assert effectifs(["chien", "chat", "chien", "chien", "poisson", "chat"])
{'chien': 3, 'chat': 2, 'poisson': 1}

def effectifs(donnees):
...
# Tests
assert effectifs([4, 1, 2, 4, 2, 2, 6]) == {4: 2, 1: 1, 2: 3, 6: 1}
assert effectifs(["chien", "chat", "chien", "chien", "poisson", "chat"]) == {'chien': 3, 'chat': 2, 'poisson': 1}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uS8ws3/]+fr7gebh[pPic5aonkyd1)t050O0A0R0J0G0b0r0d0H0b0J0r0r0h010R0G0E010406050r0n0j0j0J0x0N040o0K0b0n0,0K0L050t0?0^0`0|0;0E04051c151f0t1c0;0O0G0i0!0$0(0*0$0L0z0n0J0z0A0m0E0N0R0C130d0C0G0z0C0b1H0C0R0/050V0B0b0A1o0%0)011G1I1K1I0R1Q1S1O0R0x1d1C0!0 0r0E0J0L0*0k011U1q010w0X0A0L0J0j0A1O1:1=1`1W1}1S20220/0a0d0F0x0K0E0K0r0G120L0d0T1.0x0x0A0H2n15250L1d0t1C2A1*1,1+1P0O271r0G0L1 2k1O1l1n0#1V2K2M0L0K2Q1O0E2t1d2y2A2%0=1;2o2S1{2W0x0_0b1O0J1F2t0w0*030e0e0H2X0A1K2V0K0m0s0P0m0P0/0P150J2(2+0:2*262-1W2/2;2?2^0A2`012|2~30322N35371^040k3c3e1=3g2y2J013l0J2=1d2@0C2_2{2}2 0T3v2W3x350/0s3C2x3f0;3G3j0*3J3L053N3P3r3R3u2L3w360m0f0/0f3!163$3h2,1p3k0K2:3K3n3O3p3Q3t3T3?3V3^0I0/0I3}2%3%2+3H3+473/3s3S314d343^0l0/0l4j3 3(423*443m3M3o3q4r3=333W0y0/0y4A3E4l3i4D3I4F464H484J3;4c4M3^0p0/0p4R2z1g2#152Q2D0O1,2I3)014s2P1m1d2!0A2$3f3#3E054s52260G0O0*2}2y3W394H5a5c4b4t4(37390d2b0A5j4s3U4v5n1O0t3d403H0M0/0T0w544.4C2T010q0/0d5G58415J0L0w0/3p0w2u0,0w0r5O5A4`0.040c5#5I2.5D132M0A5!3~555,1W5(0Q0g5O0;5?5H4m5i015d2+3W3z3-0d624$5l3@3y1_5q5s4L6d0m675y040d6o5N5^3*5D0G2 0e5V5X5O6q4m4`0K0/0h6z5$4V0L0B6t2v5+6B4V5(0c0Q5}6N595b630e5e3^0s3n6a5k5u3W6#5p215r6X5t4u6*5x3d6p6A4U5J5C040w446G6r3I0/0i706O5J0K5L042L756`5-040O5/0A5;6U5Q1{5(5|5 3g7p5A6%6Z373`5h6W6b6)3^3`6,226h4%6j7w3C6^6^6H6{0/0G5F7p6_7l3k737c7T0*787O147R7M7e1l6v6x6M7r717n6T7-617y6Y654f6$7?6:5m0m4g7D6.7z6;7_2A6@7K877%7U7f6u0K6w0w5W7,2)7.0/0D7k4n7V7;7d5_0/0u7W3H6D046F7$710L6L8d7+0R8m5%8k8G6I8o8i767m8s8u6C0/0v8Q4V0j0G3a7:8M6V5j7u0m4x7x7F6c4e374x808+7A8.6?6n877S5B5U0Y0A8J5J7/7p5~8!2o7t7^374O8*6/6i8-0m4O8:9b7G9d997J8_7L8A8C8e8g8F8p7X015(8l9t8n04749y8H048t8z8N1W8w8y2%8`4`8W8Y928m961=3W4*9a827}4*9g9X6j9V9l6o890*6|2t0R0n0x7#9L9*728b7*8f6y9Q7r0t574/514;4~150R4@a42G2B0J1Ra10t4=5~0T0V0X0r04.

def effectifs(donnees):
dico_effect = dict()
for v in donnees:
if ...:
...
else:
...
return ...
# Tests
assert effectifs([4, 1, 2, 4, 2, 2, 6]) == {4: 2, 1: 1, 2: 3, 6: 1}
assert effectifs(["chien", "chat", "chien", "chien", "poisson", "chat"]) == {'chien': 3, 'chat': 2, 'poisson': 1}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uS8ws3/]+fr7gebh[pPic5aonkyd1)t050O0A0R0J0G0b0r0d0H0b0J0r0r0h010R0G0E010406050r0n0j0j0J0x0N040o0K0b0n0,0K0L050t0?0^0`0|0;0E04051c151f0t1c0;0O0G0i0!0$0(0*0$0L0z0n0J0z0A0m0E0N0R0C130d0C0G0z0C0b1H0C0R0/050V0B0b0A1o0%0)011G1I1K1I0R1Q1S1O0R0x1d1C0!0 0r0E0J0L0*0k011U1q010w0X0A0L0J0j0A1O1:1=1`1W1}1S20220/0a0d0F0x0K0E0K0r0G120L0d0T1.0x0x0A0H2n15250L1d0t1C2A1*1,1+1P0O271r0G0L1 2k1O1l1n0#1V2K2M0L0K2Q1O0E2t1d2y2A2%0=1;2o2S1{2W0x0_0b1O0J1F2t0w0*030e0e0H2X0A1K2V0K0m0s0k0m0P0/0P150J2(2+0:2*262-1W2/2;2?2^0A2`012|2~30322N35371^040k3c3e1=3g2y2J013l0J2=1d2@0C2_2{2}2 0T3v2W3x350/0s3C2x3f0;3G3j0*3J3L053N3P3r3R3u2L3w360m0f0/0f3!163$3h2,1p3k0K2:3K3n3O3p3Q3t3T3?3V3^0I0/0I3}2%3%2+3H3+473/3s3S314d343^0l0/0l4j3 3(423*443m3M3o3q4r3=333W0y0/0y4A3E4l3i4D3I4F464H484J3;4c4M3^0p0/0p4R2z1g2#152Q2D0O1,2I3)014s2P1m1d2!0A2$3f3#3E054s52260G0O0*2}2y3W394H5a5c4b4t4(37390d2b0A5j4s3U4v5n1O0t3d403H0M0/0T0w544.4C2T010q0/0d5G58415J0L0w0/3p0w2u0,0w0r5O5A4`0.040c5#5I2.5D132M0A5!3~555,1W5(0Q0g5O0;5?5H4m5i015d2+3W3z3-0d624$5l3@3y1_5q5s4L6d0m675y040d6o5N5^3*5D0G2 0e5V5X5O6q4m4`0K0/0h6z5$4V0L0B6t2v5+6B4V5(0c0Q5}6N595b630e5e3^0s3n6a5k5u3W6#5p215r6X5t4u6*5x3d6p6A4U5J5C040w446G6r3I0/0i706O5J0K5L042L756`5-040O5/0A5;6U5Q1{5(5|5 3g7p5A6%6Z373`5h6W6b6)3^3`6,226h4%6j7w3C6^6^6H6{0/0G5F7p6_7l3k737c7T0*787O147R7M7e1l6v6x6M7r717n6T7-617y6Y654f6$7?6:5m0m4g7D6.7z6;7_2A6@7K877%7U7f6u0K6w0w5W7,2)7.0/0D7k4n7V7;7d5_0/0u7W3H6D046F7$710L6L8d7+0R8m5%8k8G6I8o8i767m8s8u6C0/0v8Q4V0j0G3a7:8M6V5j7u0m4x7x7F6c4e374x808+7A8.6?6n877S5B5U0Y0A8J5J7/7p5~8!2o7t7^374O8*6/6i8-0m4O8:9b7G9d997J8_7L8A8C8e8g8F8p7X015(8l9t8n04749y8H048t8z8N1W8w8y2%8`4`8W8Y928m961=3W4*9a827}4*9g9X6j9V9l6o890*6|2t0R0n0x7#9L9*728b7*8f6y9Q7r0t574/514;4~150R4@a42G2B0J1Ra10t4=5~0T0V0X0r04.

def effectifs(donnees):
dico_effect = dict()
for v in donnees:
if v in dico_effect:
dico_effect[v] = ...
else:
dico_effect[v] = ...
return dico_effect
# Tests
assert effectifs([4, 1, 2, 4, 2, 2, 6]) == {4: 2, 1: 1, 2: 3, 6: 1}
assert effectifs(["chien", "chat", "chien", "chien", "poisson", "chat"]) == {'chien': 3, 'chat': 2, 'poisson': 1}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uS8ws3/]+fr7Og}ebh[pPicé5a{onkyd1)t050S0C0V0M0I0b0r0d0J0b0M0r0r0h010V0I0G010406050r0n0j0j0M0x0R040o0O0b0n0:0O0P050t0`0|0~100^0G04051g191j0t1g0^0S0I0i0(0*0,0.0*0P0A0n0M0A0C0m0G0R0V0E170d0E0I0A0E0b1L0E0V0?050Z0D0b0C1s0+0-011K1M1O1M0V1U1W1S0V0x1h1G0(130r0G0M0P0.0k011Y1u010w0#0C0P0M0j0C1S1@1_1~1!211W24260?0a0d0H0x0O0G0O0r0I160P0d0X1=0x0x0C0J2r19290P1h0t1G2E1.1:1/1T0S2b1v0I0P232o1S1p1r0)1Z2O2Q0P0O2U1S0G2x1h2C2E2+0_1^2s2W1 2!0x0}0b1S0M1J2x0w0.030e0e0J2#0C1O2Z0O0m0s0s0m0T0?0T190M2,2/0@2.2a2;1!2?2^2`2|0C2~01303234362R393b1|040k3g3i1_3k2C2N013p0M2_1h2{0E2}2 31330X3z2!3B390?0s3G2B3j0^3K3n0.3N3P053R3T3v3V3y2P3A3a0m0f0?0f3(1a3*3l2:1t3o0O2@3O3r3S3t3U3x3X3`3Z3|0L0?0L412+3+2/3L3/4b3?3w3W354h383|0l0?0l4n433,463.483q3Q3s3u4v3_373!0y0?0y4E3I4p3m4H3M4J4a4L4c4N3^4g4Q3|0p0?0p4V2D1k2)192U2H0S1:2M3-014w2T1q1h2(0C2*3j3)3I054w562a0I0S0.312C3!3d4L5e5g4f4x4,3b3d0d2f0C5n4w3Y4z5r1S0t3h443L0Q0?0X0w584=4G2X010q0?0d5K5c455N0P0w0?3t0w2y0:0w0r5S5E4~0=040c5)5M2=5H172Q0C5(42595:1!5,0U0g5S0^5`5L4q5m015h2/3!3D3;0d664*5p3{3C1}5u5w4P6h0m6b5C040d6s5R5|3.5H0I330e5Z5#5S6u4q4~0O0?0h6D5*4Z0P0D6x2z5/6F4Z5,0c0U6D6E4Y5N0J5k030d0z2s0G0C150d0M0n0,0I0d0K0J0x0I2x2t6y0O6A0w5!2z0d0h0d0N0B616R5d5f670e5i3|0s3r6e5o5y3!7g5t255v7c5x4y7l5B3h6t6Y5U1 5G040w486K6v3M0?0i7E6S5N0O5P042P7J6Z5;040S5?0C5^797y5}0?60633k7%5E7i7e3b3~5l7b6f7k3|3~7n266l4+6n7.3G7w7w6L5N7A0I5J7%7x4r7H7Q7Z0.7M0?7P86817S1p6z6B6Q7)7F5,7$4o7Y6d7:7d694j7h8u7r5q0m4k7^7p7;7s8x2E7v7 8L8h3o6P6~8l0V8s5+0?0F8T6M898n7K1 5,0u8a3L6H046J8g7F0P8P6 718S8!7R7!048W8^8b7G047I8}3L8%8)6G0?0v954Z0j0I3e78927+8w3b4B7/7`6g4i9i6j7o9l7=9o8J6r8L874~7A350r0C8X5N8p9e2-3K9g1_4R8y9r8H3b4S8E9N8B4S9u9w7 8N6w7T6}8=6C928U8{9D7S919H8#8`8(8.9/8c6I995N9b9d7%629.7a5n7,0m4.9k7q6m9na39p7_a67{a8a47~6t9Y017A2x0V0n0x189=8_9Z8j8Q709%8r7)0t5b4?554^52190V4{aF2K2F0M1VaC0t4_620X0Z0#0r04.

II. Les listes⚓︎

Exercice 1⚓︎

Vous devez générer une liste contenant les entiers de 1 à 49 dans l'ordre croissant. Ensuite, votre code doit tirer, sans remise, 6 numéros qu'on stocke dans une liste, puis, toujours sans remise, un dernier numéro (le numéro complémentaire).

Exemple

Votre code affichera la liste des 6 numéros puis le complémentaire comme ceci :

Python Console Session
[31, 3, 12, 45, 13, 27]
18

Attention

Vous pourrez importer le module random. Vous ne devez pas utiliser la fonction choice du module random.

A vous de jouer

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier

Astuce

🌵 Envisagez différentes façons de coder ce problème, utilisant pop, del ou remove ...

Solution avec la méthode pop

C'est la solution préférable.

Python
from random import randint
liste = [i for i in range(1, 50)]
reponse = []
for i in range(6):
    numero = liste.pop(randint(0, len(liste) - 1))
    reponse.append(numero)
print(reponse)
complementaire = liste.pop(randint(0, len(liste) - 1))
print(complementaire)
Solution avec del
Python
from random import randint
liste = [i for i in range(1, 50)]
reponse = []
for i in range(6):
    i = randint(0, len(liste) - 1)
    reponse.append(liste[i])
    del liste[i]
print(reponse)
i = randint(0, len(liste) - 1)
print(liste[i])
Solution avec la méthode remove
Python
from random import randint
liste = [i for i in range(1,50)]
reponse = []
for i in range(6):
    i = randint(0, len(liste) - 1)
    reponse.append(liste[i])
    liste.remove(liste[i])
print(reponse)
i = randint(0, len(liste) - 1)

print(liste[i])

Exercice 2⚓︎

1. On considère le programme suivant :

Python
liste1 = [0]*100
liste2 = [0 for k in range(100)]
liste3 = []
for k in range(100):
    liste3.append(0)

Quel est le contenu de chacune des listes ?

Solution

😀 Utiliser l'éditeur Python pour vérifier les réponses ...

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier

2. Ecrire un programme python permettant de créer les listes suivantes :

a. Une liste contenant 12 fois le chiffre 7.
b. La liste des nombres entiers de 1 à 100.
c. Une liste contenant 50 nombres tirés au sort entre 1 et 6.

A vous de jouer

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier

Solution
Python
from random import randint
liste_a = [7 for _ in range(12)]
liste_b = [i for i in range(1, 101)]
liste_c = [randint(1, 6) for _ in range(50)]
print(liste_a)
print(liste_b)
print(liste_c)

Exercice 3⚓︎

Écrire une fonction sans_doublon qui prend en paramètre une liste de nombres pouvant contenir des nombres répétés plusieurs fois et renvoie la liste ne contenant qu'une fois chaque nombre.

Exemple

Python
>>> sans_doublon([1, 2, 4, 6, 6])
[1, 2, 4, 6]
>>> sans_doublon([2, 5, 7, 7, 7, 9])
[2, 5, 7, 9]
>>> sans_doublon([5, 1, 1, 2, 5, 6, 3, 4, 4, 4, 2])
[5, 1, 2, 6, 3, 4]

Attention

Il est interdit d'utiliser count

Compléter le code ci-dessous

def sans_doublon(tableau):
...
# Tests
assert sans_doublon([1, 2, 4, 6, 6]) == [1, 2, 4, 6]
assert sans_doublon([2, 5, 7, 7, 7, 9]) == [2, 5, 7, 9]
assert sans_doublon([5, 1, 1, 2, 5, 6, 3, 4, 4, 4, 2]) == [5, 1, 2, 6, 3, 4]
assert sans_doublon([]) == []
assert sans_doublon([1, 2, 3, 4]) == [1, 2, 3, 4]
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uSw.s3/]frgebh[pPic5aonkyd1)t050M0y0P0H0E0b0r0d0F0b0H0r0r0h010P0E0C010406050r0n0j0j0H0w0L040o0I0b0n0*0I0J050t0;0?0^0`0/0C04051a131d0t1a0/0M0E0i0Y0!0$0(0!0J0x0n0H0x0y0m0C0L0P0A110d0A0E0x0A0b1F0A0P0-050T0z0b0y1m0#0%011E1G1I1G0P1O1Q1M0P0w1b1A0Y0}0r0C0H0J0(0k011S1o010v0V0y0J0H0j0y1M1.1:1^1U1{1Q1~200-0a0d0D0w0I0C0I0r0E100J0d0R1,0w0w0y0F2l13230J1b0t1A2y1(1*1)1N0M251p0E0J1}2i1M1j1l0Z1T2I2K0J0I2O1M0C2r1b2w2y2#0:1/2m2Q1_2U0w0@0b1M0H1D2r0v0(030e0e0F2V0y1I2T0I0m0f0N0m0N0-0N130H2$2)0.2(242+1U2-2/2;2?0y2^012`2|2~302L33351?040k3a3c1:3e2w2H013j0H2:1b2=0A2@2_2{2}0R3t2U3v0m0s0-0s3A2v3d0/3E3h0(3H3J053L3N3p3P3s2J3u34330-0f3Z143#3f2*1n3i0I2.3I3l3M3n3O3r3R3=3T3@0G0-0G3{2#3$2)3F3*453.3q3Q2 4b323@0l0-0l4h3d1e2Z132O2B0M1*2G3(014q2N1k1b2Y0y2!4z3|3C054q4Q240E0M0(2{2w3U373K0d4Y4!494r314%1@290y4,4q3S4t354(3Z3%400(0K0-0R0v3!4T4~2R010p0-0d552x3~4l0v0-0r1:0r0e0M0I0n1P115d4W3 580,040c5s5f4I0J0-1#0y0H0n5z571_5w0O0g5s0/4S5e3E4+014#2)3U3x3,4*4Z5U4-4_5X4;1 4?5$4^4s5)2y3b0d5?5c5J3i0-2U0n0i5F5H5Q045^4k4I0I0-0h5s633g4 015w0B0u5O5I4k5T5V1:3U3W4)6k5%5/3@3W0d4=4@3;4/6t1M0t5=5@5A6c51040v42696F585C042U0j0z2r6L5_0(0I5a042J6U646c6O5E5G6i6b5v0-5N615P2%5S5#6l0J3U0f3l6q5.6z356{6v5+6x4a6 3^5;625@7a6M1_6H0E54616a5u2,5{0I6R6T7h7c1U6X7l0P6#6,1_7s6Z127p6V3G7l5}5 6+7j1U5w6/4i7H5!4,4$4d6|6@6r764e7220744.3?354e787a7)6E7C6O5|5~6*617q6W0-0q7N5B0-0H0C0C1}0M7_6c5w5y7;7,7l7n0y816-040O6h856j7T7Q354v6p7T6~7#0m4v7X5,3:758o8k3A7b7C6H2r0P0n0w7A2#7i4l7E7/607M7;0t4V4A4P4C4M130P4F8U2E2z0H1P8R0t4D5P0R0T0V0r04.

Exercice 4 : Savoir compter⚓︎

Écrire une fonction compter_triples qui prend en paramètre une liste entiers de nombres entiers et renvoie le nombre de multiples de 3 de cette liste

Attention

Il est interdit d'utiliser count

Compléter le code ci-dessous

def compter_triples(entiers):
...
# Tests
assert compter_triples([1, 3, 18, 5]) == 2
assert compter_triples([15, 3, 18]) == 3
assert compter_triples([5, 13, 11, 2]) == 0
assert compter_triples([]) == 0
assert compter_triples([5, 13,0, 2]) == 1
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l( _4:=vm26-uSws3%/+frgebhpPic05aonkyd1)t050M0y0P0H0D0b0q0d0E0b0H0q0q0h010P0D0B010406050q0n0j0j0H0w0L040o0I0b0n0*0I0J050t0;0?0^0`0/0B04051a131d0t1a0/0M0D0i0Y0!0$0(0!0J0x0n0H0x0y0m0B0L0P0A110d0A0D0x0A0b1F0A0P0-050T0z0b0y1m0#0%011E1G1I1G0P1O1Q1M0P0w1b1A0Y0}0q0B0H0J0(0k011S1o010v0V0y0J0H0j0y1M1.1:1^1U1{1Q1~200-0a0d0C0w0I0B0I0q0D100J0d0R1,0w0w0y0E2l13230J1b0t1A2y1(1*1)1N0M251p0D0J1}2i1M1j1l0Z1T2I2K0J0I2O1M0B2r1b2w2y2#0:1/2m2Q1_2U0w0@0b1M0H1D2r0v0(030e0e0E2V0y1I2T0I0m0f0k0m0N0-0N130H2$2)0.2(242+1U2-2/2;2?0y2^012`2|2~302L33351?040k3a3c1:3e2w2H013j0H2:1b2=0A2@2_2{2}0R3t2U3v0m0r0-0r3A2v3d0/3E3h0(3H3J053L3N3p3P3s2J3u34330-0f3Z143#3f2*1n3i0I2.3I3l3M3n3O3r3R3=3T3@0G0-0G3{2#3$2)3F3*453.3q3Q2 4b323@0l0-0l4h3d1e2Z132O2B0M1*2G3(014q2N1k1b2Y0y2!4z3|3C054q4Q240D0M0(2{2w3U373K0d4Y4!494r314%1@290y4,4q3S4t354(3Z3%400(0K0-0R0v3!4T4~2R010p0-0d552x3~4l0v0-2}0j0B0P0y0w0e1(0+1Q0q5d4W3 580,040c5u5f4I0J0-1}0*5n5t4S5e571_5y0O0g5u0/5K5v4*4Z014#2)3U3x3,5V4@3;4/3@1?0d4=5(4a5*3w1M0t3b0d5_5c5M3i5i5l5u5{4k4I0I0-0h605C4 010j0D0-0F5R5B3E4+5X0e4$3@3W4)6i3:5:3?353W5-1 4?6j4^4s3U6n3A5`613g6951040v42675|3)0-2U0j0z2r6M62690I5a042J6U6G585E045G0D5I6g6V5x0-5Q5T5S2%6h5W5Y1:3U0f3l6p4-4_6|4;6w5/4.6s3^2y5^6E5`68586I0D545T6F5w2,6P0I6R6T7i7d1_64040s6#7k1U6b3X7w3F7t0h667q6N6a6c046e5T7r1U5y6;4i6-4X6_6k5Z4d6~7U6z5;0m4e6v2075717X79047b7/7j4l5~0P7B63657^696(0E5 7G6.7s0-0u7{587z04396=7S2m6 6l354v6o7Z5)774v7(6x6q764c8f5?7a5_7N500-2r0P0n0w12806$7l047~7@8a7M0t4V4A4P4C4M130P4F8R2E2z0H1P8O0t4D5S0R0T0V0q04.

Exercice 5 :⚓︎

Indice du minimum

Exercice 6 :⚓︎

Maximum

Exercice 7 :⚓︎

Distribution de costumes

Exercice 8 :⚓︎

Aplatir un tableau

Exercice 9 : Le nombre mystère⚓︎

Trouvez le nombre mystère qui répond aux conditions suivantes :

  • Il est composé de 3 chiffres.
  • Il est strictement inférieur à 300.
  • Il est pair.
  • Deux de ses chiffres sont identiques.
  • La somme de ses chiffres est égale à 7.

On vous propose d'employer une méthode dite de « force brute », c'est-à-dire de tester tous les nombres possibles répondants aux trois premières conditions.

Question

Compléter le script suivant

  • possibles est la liste des nombres répondant aux trois premiers critères.
  • liste est la liste des nombres répondant au problème.

def deux_chiffes_ident(nbre: int) -> bool:
"""
renvoie True si nbre a deux chiffres identiques, False sinon
Exemples :
>>> deux_chiffes_ident(121)
True
>>> deux_chiffes_ident(123)
False
"""
mot = str(nbre)
...
def somme_chiffres(nbre: int) -> int:
"""
renvoie la somme des chiffes de nbre
Exemple :
>>> somme_chiffres(123)
6
"""
mot = str(nbre)
...
possibles = [i for i in range(...) if ...]
liste = ...
print(liste[0])
# Tests
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
Évaluations restantes : 5/5

.128013l(9 _4:;=vTm26-uS8ws3%/]+fr7gebhE[pPic05qa,onkFyd1x)t050X0E0#0Q0L0b0u0e0M0b0Q0u0u0j010#0L0J010406050u0q0m0m0Q0B0W040r0S0b0q0_0S0T050x101214160~0J04051m1f1p0x1m0~0X0L0k0.0:0=0@0:0T0D0q0Q0D0E0p0J0W0#0G1d0e0G0L0D0G0b1R0G0#0|050)0F0b0E1y0;0?011Q1S1U1S0#1!1$1Y0#0B1n1M0.190u0J0Q0T0@0n011(1A010A0+0E0T0Q0m0E1Y1}1 241*271$2a2c0|0a0e0K0B0S0J0S0u0L1c0T0e0%1{0B0B0E0M2x1f2f0T1n0x1M2K1@1_1^1Z0X2h1B0L0T292u1Y1v1x0/1)2U2W0T0S2!1Y0J2D1n2I2K2;0 1~2y2$252*0B130b1Y0Q1P2D0A0@030f0f0M2+0E1U2)0S0p0g0v0p0Y0|0e0Y1f0Q2=2^0}2@2g2`1*2|2~30320E340136383a3c2X3f3h22040e0n3n3p1 3r2I2T013w0Q2 1n310G333537390%3G2*3I0p0v3k0v3O2H3q0~3S3u0@3V3X053Z3#3C3%3F2V3H3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4j{8+0x8.1q2/1f8V1f0#8Xnp2Q2L0Qlu8U8%a30%0)0+0u04.

III. Les chaines de caractères⚓︎

Renverser une chaîne

IV. Les dictionnaires⚓︎

Question

Compléter le script suivant pour qu'il affiche la liste pokemons_rapides des noms des pokémons dont la vitesse est supérieure à 60.
On doit obtenir l'affichage : ['Salameche', 'Reptencil'] ou ['Reptencil', 'Salameche']

pokemon1 = {'Nom' : 'Carapuce', 'HP' : 44, 'Attaque' : 48, 'Defense' : 65, 'Vitesse' : 43, 'Type' : ['Eau']}
pokemon2 = {'Nom' : 'Evoli', 'HP' : 55, 'Attaque' : 55, 'Defense' : 50, 'Vitesse' : 55, 'Type' : ['Normal']}
pokemon3 = {'Nom' : 'Salameche', 'HP' : 39, 'Attaque' : 52, 'Defense' : 43, 'Vitesse' : 65, 'Type' : ['Feu']}
pokemon4 = {'Nom' : 'Reptencil', 'HP' : 58, 'Attaque' : 64, 'Defense' : 58, 'Vitesse' : 80, 'Type' : ['Eau']}
liste_pokemon = [pokemon1, pokemon2, pokemon3, pokemon4]
pokemons_rapides = []
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
###(Dés-)Active le code après la ligne # Tests (insensible à la casse)
(Ctrl+I)
Entrer ou sortir du mode "deux colonnes"
(Alt+: ; Ctrl pour inverser les colonnes)
Entrer ou sortir du mode "plein écran"
(Esc)
rounded
>>> 
 
x
x
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier

Solution 1

Correction possible utilisant la méthode items:

Python
pokemon1 = {'Nom' : 'Carapuce', 'HP' : 44, 'Attaque' : 48, 'Defense' : 65, 'Vitesse' : 43, 'Type' : ['Eau']}
pokemon2 = {'Nom' : 'Evoli', 'HP' : 55, 'Attaque' : 55, 'Defense' : 50, 'Vitesse' : 55, 'Type' : ['Normal']}
pokemon3 = {'Nom' : 'Salameche', 'HP' : 39, 'Attaque' : 52, 'Defense' : 43, 'Vitesse' : 65, 'Type' : ['Feu']}
pokemon4 = {'Nom' : 'Reptencil', 'HP' : 58, 'Attaque' : 64, 'Defense' : 58, 'Vitesse' : 80, 'Type' : ['Eau']}
liste_pokemon = [pokemon1, pokemon2, pokemon3, pokemon4]
pokemons_rapides = []
for pokemon in liste_pokemon:
    for cle, valeur in pokemon.items():
        if cle == 'Vitesse':
            if valeur > 60 :
                pokemons_rapides.append(pokemon['Nom'])
print(pokemons_rapides)
Solution 2

Correction possible sans utiliser la méthode items:

Python
pokemon1 = {'Nom' : 'Carapuce', 'HP' : 44, 'Attaque' : 48, 'Defense' : 65, 'Vitesse' : 43, 'Type' : ['Eau']}
pokemon2 = {'Nom' : 'Evoli', 'HP' : 55, 'Attaque' : 55, 'Defense' : 50, 'Vitesse' : 55, 'Type' : ['Normal']}
pokemon3 = {'Nom' : 'Salameche', 'HP' : 39, 'Attaque' : 52, 'Defense' : 43, 'Vitesse' : 65, 'Type' : ['Feu']}
pokemon4 = {'Nom' : 'Reptencil', 'HP' : 58, 'Attaque' : 64, 'Defense' : 58, 'Vitesse' : 80, 'Type' : ['Eau']}
liste_pokemon = [pokemon1, pokemon2, pokemon3, pokemon4]
pokemons_rapides = []
for pokemon in liste_pokemon:
    if pokemon['Vitesse'] > 60:
        pokemons_rapides.append(pokemon['Nom'])
print(pokemons_rapides)
Solution 3

😀 : Solution beaucoup plus élégante avec une liste en compréhension :

Python
pokemon1 = {'Nom' : 'Carapuce', 'HP' : 44, 'Attaque' : 48, 'Defense' : 65, 'Vitesse' : 43, 'Type' : ['Eau']}
pokemon2 = {'Nom' : 'Evoli', 'HP' : 55, 'Attaque' : 55, 'Defense' : 50, 'Vitesse' : 55, 'Type' : ['Normal']}
pokemon3 = {'Nom' : 'Salameche', 'HP' : 39, 'Attaque' : 52, 'Defense' : 43, 'Vitesse' : 65, 'Type' : ['Feu']}
pokemon4  ={'Nom' : 'Reptencil', 'HP' : 58, 'Attaque' : 64, 'Defense' : 58, 'Vitesse' : 80, 'Type' : ['Eau']}
liste_pokemon = [pokemon1, pokemon2, pokemon3, pokemon4]
pokemons_rapides = [pokemon["Nom"] for pokemon in liste_pokemon if pokemon["Vitesse"] > 60]
print(pokemons_rapides)