Spaces:
Running
Running
Delete code.txt
Browse files
code.txt
DELETED
@@ -1,2226 +0,0 @@
|
|
1 |
-
moyenne (N2)
|
2 |
-
;;;
|
3 |
-
Écrire une fonction moyenne qui prend en paramètre un tableau non vide d'entiers tab et qui renvoie la moyenne de ces entiers.
|
4 |
-
Il est interdit d'utiliser la fonction Python sum
|
5 |
-
Exemple :
|
6 |
-
>>> moyenne([10, 15, 20])
|
7 |
-
15.0
|
8 |
-
>>> moyenne([8, 11, 17, 10])
|
9 |
-
11.5
|
10 |
-
;;;
|
11 |
-
c = 0
|
12 |
-
if moyenne([10, 15, 20]) == 15:
|
13 |
-
print("Test n°1 : OK")
|
14 |
-
c += 1
|
15 |
-
else :
|
16 |
-
print("Test n°1 : échec")
|
17 |
-
if moyenne([8, 11, 17, 10]) == 11.5:
|
18 |
-
print("Test n°2 : OK")
|
19 |
-
c += 1
|
20 |
-
else :
|
21 |
-
print("Test n°2 : échec")
|
22 |
-
if c == 2 :
|
23 |
-
print('OK')
|
24 |
-
else :
|
25 |
-
print('KO')
|
26 |
-
;;;
|
27 |
-
maximum_tableau (N2)
|
28 |
-
;;;
|
29 |
-
Écrire la fonction maximum_tableau, prenant en paramètre un tableau non vide de nombres tab (de type list) et renvoyant le plus grand élément de ce tableau.
|
30 |
-
Il est interdit d'utiliser la fonction Python max
|
31 |
-
Exemples :
|
32 |
-
>>> maximum_tableau([98, 12, 104, 23, 131, 9])
|
33 |
-
131
|
34 |
-
>>> maximum_tableau([-27, 24, -3, 15])
|
35 |
-
24
|
36 |
-
;;;
|
37 |
-
c = 0
|
38 |
-
if maximum_tableau([98, 12, 104, 23, 131, 9]) == 131:
|
39 |
-
print("Test n°1 : OK")
|
40 |
-
c += 1
|
41 |
-
else :
|
42 |
-
print("Test n°1 : échec")
|
43 |
-
if maximum_tableau([-27, 24, -3, 15]) == 24:
|
44 |
-
print("Test n°2 : OK")
|
45 |
-
c += 1
|
46 |
-
else :
|
47 |
-
print("Test n°2 : échec")
|
48 |
-
if maximum_tableau([-27, -24, -3, -15]) == -3:
|
49 |
-
print("Test n°3 : OK")
|
50 |
-
c += 1
|
51 |
-
else :
|
52 |
-
print("Test n°3 : échec")
|
53 |
-
if c == 3 :
|
54 |
-
print('OK')
|
55 |
-
else :
|
56 |
-
print('KO')
|
57 |
-
;;;
|
58 |
-
recherche (N2)
|
59 |
-
;;;
|
60 |
-
Programmer la fonction recherche, prenant en paramètres un tableau non vide tab (type list) d’entiers et un entier n, et qui renvoie l’indice de la première occurrence de l’élément cherché.
|
61 |
-
Si l’élément n’est pas présent, la fonction renvoie None.
|
62 |
-
Exemples
|
63 |
-
>>> recherche([5, 3],1) # renvoie None
|
64 |
-
>>> recherche([2,4],2)
|
65 |
-
0
|
66 |
-
>>> recherche([2,3,5,2,4],2)
|
67 |
-
0
|
68 |
-
;;;
|
69 |
-
c = 0
|
70 |
-
if recherche([5, 3],1) == None:
|
71 |
-
print("Test n°1 : OK")
|
72 |
-
c += 1
|
73 |
-
else :
|
74 |
-
print("Test n°1 : échec")
|
75 |
-
if recherche([2,4],2) == 0:
|
76 |
-
print("Test n°2 : OK")
|
77 |
-
c += 1
|
78 |
-
else :
|
79 |
-
print("Test n°2 : échec")
|
80 |
-
if recherche([2,3,5,2,4],2) == 0:
|
81 |
-
print("Test n°3 : OK")
|
82 |
-
c += 1
|
83 |
-
else :
|
84 |
-
print("Test n°3 : échec")
|
85 |
-
if c == 3 :
|
86 |
-
print('OK')
|
87 |
-
else :
|
88 |
-
print('KO')
|
89 |
-
;;;
|
90 |
-
max_et_indice (N2)
|
91 |
-
;;;
|
92 |
-
Écrire une fonction max_et_indice qui prend en paramètre un tableau non vide tab (type Python list) de nombres entiers et qui renvoie la valeur du plus grand élément de ce tableau ainsi que l’indice de sa première apparition dans ce tableau.
|
93 |
-
L’utilisation de la fonction native max n’est pas autorisée.
|
94 |
-
Exemples :
|
95 |
-
>>> max_et_indice([1, 5, 6, 9, 1, 2, 3, 7, 9, 8])
|
96 |
-
(9, 3)
|
97 |
-
>>> max_et_indice([-2])
|
98 |
-
(-2, 0)
|
99 |
-
>>> max_et_indice([-1, -1, 3, 3, 3])
|
100 |
-
(3, 2)
|
101 |
-
>>> max_et_indice([1, 1, 1, 1])
|
102 |
-
(1, 0)
|
103 |
-
;;;
|
104 |
-
c = 0
|
105 |
-
if max_et_indice([1, 5, 6, 9, 1, 2, 3, 7, 9, 8]) == (9,3):
|
106 |
-
print("Test 1 : OK")
|
107 |
-
c += 1
|
108 |
-
else :
|
109 |
-
print("Test 1 : échec")
|
110 |
-
if max_et_indice([-2]) == (-2, 0):
|
111 |
-
print("Test 2 : OK")
|
112 |
-
c += 1
|
113 |
-
else :
|
114 |
-
print("Test 2 : échec")
|
115 |
-
if max_et_indice([-1, -1, 3, 3, 3]) == (3, 2):
|
116 |
-
print("Test 3 : OK")
|
117 |
-
c += 1
|
118 |
-
else :
|
119 |
-
print("Test 3 : échec")
|
120 |
-
if max_et_indice([1, 1, 1, 1]) == (1, 0):
|
121 |
-
print("Test 4 : OK")
|
122 |
-
c += 1
|
123 |
-
else :
|
124 |
-
print("Test 4 : échec")
|
125 |
-
if c == 4 :
|
126 |
-
print("OK")
|
127 |
-
else :
|
128 |
-
print("KO")
|
129 |
-
;;;
|
130 |
-
verifie (N3)
|
131 |
-
;;;
|
132 |
-
Écrire une fonction verifie qui prend en paramètre un tableau de valeurs numériques et qui renvoie True si ce tableau est trié dans l’ordre croissant, False sinon.
|
133 |
-
Un tableau vide est considéré comme trié.
|
134 |
-
Exemples :
|
135 |
-
>>> verifie([0, 5, 8, 8, 9])
|
136 |
-
True
|
137 |
-
>>> verifie([8, 12, 4])
|
138 |
-
False
|
139 |
-
>>> verifie([-1, 4])
|
140 |
-
True
|
141 |
-
>>> verifie([])
|
142 |
-
True
|
143 |
-
>>> verifie([5])
|
144 |
-
True
|
145 |
-
;;;
|
146 |
-
c = 0
|
147 |
-
if verifie([0, 5, 8, 8, 9]):
|
148 |
-
print("Test 1 : OK")
|
149 |
-
c += 1
|
150 |
-
else :
|
151 |
-
print("Test 1 : échec")
|
152 |
-
if not verifie([8, 12, 4]):
|
153 |
-
print("Test 2 : OK")
|
154 |
-
c += 1
|
155 |
-
else :
|
156 |
-
print("Test 2 : échec")
|
157 |
-
if verifie([-1, 4]):
|
158 |
-
print("Test 3 : OK")
|
159 |
-
c += 1
|
160 |
-
else :
|
161 |
-
print("Test 3 : échec")
|
162 |
-
if verifie([]):
|
163 |
-
print("Test 4 : OK")
|
164 |
-
c += 1
|
165 |
-
else :
|
166 |
-
print("Test 4 : échec")
|
167 |
-
if verifie([5]):
|
168 |
-
print("Test 5 : OK")
|
169 |
-
c += 1
|
170 |
-
else :
|
171 |
-
print("Test 5 : échec")
|
172 |
-
if c == 5 :
|
173 |
-
print("OK")
|
174 |
-
else :
|
175 |
-
print("KO")
|
176 |
-
;;;
|
177 |
-
premier et dernier (N1)
|
178 |
-
;;;
|
179 |
-
Écrire une fonction premier_dernier qui prend en paramètre un tableau non vide tab. Cette fonction renvoie un tuple contenant le premièr et le dernier éléments du tabeau tab.
|
180 |
-
Exemples :
|
181 |
-
>>> premier_dernier([4, 15, 34, 22])
|
182 |
-
(4, 22)
|
183 |
-
>>> premier_dernier(["a", "f", "g", "z"])
|
184 |
-
("a", "z")
|
185 |
-
>>> premier_dernier([42])
|
186 |
-
(42, 42)
|
187 |
-
;;;
|
188 |
-
c = 0
|
189 |
-
if premier_dernier([4, 15, 34, 22]) == (4, 22):
|
190 |
-
print("Test 1 : OK")
|
191 |
-
c += 1
|
192 |
-
else :
|
193 |
-
print("Test 1 : échec")
|
194 |
-
if premier_dernier(["a", "f", "g", "z"]) == ("a", "z"):
|
195 |
-
print("Test 2 : OK")
|
196 |
-
c += 1
|
197 |
-
else :
|
198 |
-
print("Test 2 : échec")
|
199 |
-
if premier_dernier([42]) == (42, 42):
|
200 |
-
print("Test 3 : OK")
|
201 |
-
c += 1
|
202 |
-
else :
|
203 |
-
print("Test 3 : échec")
|
204 |
-
if c == 3 :
|
205 |
-
print("OK")
|
206 |
-
else :
|
207 |
-
print("KO")
|
208 |
-
;;;
|
209 |
-
delta encoding (N3)
|
210 |
-
;;;
|
211 |
-
Le codage par différence (delta encoding en anglais) permet de compresser un tableau d’entiers dont les valeurs sont proches les unes des autres. Le principe est de stocker la première donnée en indiquant pour chaque autre donnée sa différence avec la précédente plutôt que la donnée elle-même.
|
212 |
-
On se retrouve alors avec un tableau dont les valeurs sont plus petites, nécessitant moins de place en mémoire.
|
213 |
-
Programmer la fonction delta qui prend en paramètre un tableau non vide de nombres entiers et qui renvoie un tableau contenant les valeurs entières compressées à l’aide cette technique.
|
214 |
-
Exemples :
|
215 |
-
>>> delta([1000, 800, 802, 1000, 1003])
|
216 |
-
[1000, -200, 2, 198, 3]
|
217 |
-
>>> delta([42])
|
218 |
-
[42]
|
219 |
-
;;;
|
220 |
-
c = 0
|
221 |
-
if delta([1000, 800, 802, 1000, 1003]) == [1000, -200, 2, 198, 3]:
|
222 |
-
print("Test 1 : OK")
|
223 |
-
c += 1
|
224 |
-
else :
|
225 |
-
print("Test 1 : échec")
|
226 |
-
if delta([42]) == [42]:
|
227 |
-
print("Test 2 : OK")
|
228 |
-
c += 1
|
229 |
-
else :
|
230 |
-
print("Test 2 : échec")
|
231 |
-
if c == 2 :
|
232 |
-
print("OK")
|
233 |
-
else :
|
234 |
-
print("KO")
|
235 |
-
;;;
|
236 |
-
moyenne pondérée (N3)
|
237 |
-
;;;
|
238 |
-
Dans cet exercice, on cherche à calculer la moyenne pondérée d’un élève dans une matière donnée. Chaque note est associée à un coefficient qui la pondère. Par exemple, si ses notes sont : 14 avec coefficient 3, 12 avec coefficient 1 et 16 avec coefficient 2, sa moyenne pondérée sera donnée par :
|
239 |
-
(14 × 3 + 12 × 1 + 16 × 2) / (3 + 1 + 2) = 14, 333...
|
240 |
-
Écrire une fonction moyenne : qui prend en paramètre une liste notes non vide de tuples à deux éléments entiers de la forme (note, coefficient) (int ou float) positifs ou nuls et qui renvoie la moyenne pondérée des notes de la liste sous forme de flottant si la somme des coefficients est non nulle, None sinon.
|
241 |
-
Exemple :
|
242 |
-
>>> moyenne([(12, 2), (8, 1), (10, 1)])
|
243 |
-
10.5
|
244 |
-
>>> moyenne([(3, 0), (5, 0)])
|
245 |
-
None
|
246 |
-
;;;
|
247 |
-
c = 09,50
|
248 |
-
if moyenne([(12, 2), (8, 1), (10, 1)]) == 10.5:
|
249 |
-
print("Test 1 : OK")
|
250 |
-
c += 1
|
251 |
-
else :
|
252 |
-
print("Test 1 : échec")
|
253 |
-
if moyenne([(3, 0), (5, 0)]) == None:
|
254 |
-
print("Test 2 : OK")
|
255 |
-
c += 1
|
256 |
-
else :
|
257 |
-
print("Test 2 : échec")
|
258 |
-
if c == 2 :
|
259 |
-
print("OK")
|
260 |
-
else :
|
261 |
-
print("KO")
|
262 |
-
;;;
|
263 |
-
tri sélection (N3)
|
264 |
-
;;;
|
265 |
-
Écrire une fonction tri_selection qui prend en paramètre un tableau tab de nombres entiers (type list) et qui renvoie un tableau trié par ordre croissant. Cette fonction doit utiliser l'algorithme du tri sélection
|
266 |
-
Exemple :
|
267 |
-
>>> tri_selection([1, 52, 6, -9, 12])
|
268 |
-
[-9, 1, 6, 12, 52]
|
269 |
-
>>> tri_selection([6])
|
270 |
-
[6]
|
271 |
-
;;;
|
272 |
-
c = 0
|
273 |
-
if tri_selection([1, 52, 6, -9, 12]) == [-9, 1, 6, 12, 52]:
|
274 |
-
print("Test 1 : OK")
|
275 |
-
c += 1
|
276 |
-
else :
|
277 |
-
print("Test 1 : échec")
|
278 |
-
if tri_selection([6]) == [6]:
|
279 |
-
print("Test 2 : OK")
|
280 |
-
c += 1
|
281 |
-
else :
|
282 |
-
print("Test 2 : échec")
|
283 |
-
if c == 2 :
|
284 |
-
print("OK")
|
285 |
-
else :
|
286 |
-
print("KO")
|
287 |
-
;;;
|
288 |
-
min et max (N2)
|
289 |
-
;;;
|
290 |
-
Écrire une fonction min_et_max qui prend en paramètre un tableau de nombres tab non vide, et qui renvoie la plus petite et la plus grande valeur du tableau sous la forme d’un dictionnaire à deux clés min et max.
|
291 |
-
L’utilisation des fonctions natives min, max et sorted, ainsi que la méthode sort n’est pas autorisée.
|
292 |
-
Exemples :
|
293 |
-
>>> min_et_max([0, 1, 4, 2, -2, 9, 3, 1, 7, 1])
|
294 |
-
{'min': -2, 'max': 9}
|
295 |
-
>>> min_et_max([0, 1, 2, 3])
|
296 |
-
{'min': 0, 'max': 3}
|
297 |
-
>>> min_et_max([3])
|
298 |
-
{'min': 3, 'max': 3}
|
299 |
-
>>> min_et_max([1, 3, 2, 1, 3])
|
300 |
-
{'min': 1, 'max': 3}
|
301 |
-
>>> min_et_max([-1, -1, -1, -1, -1])
|
302 |
-
{'min': -1, 'max': -1}
|
303 |
-
;;;
|
304 |
-
c = 0
|
305 |
-
if min_et_max([0, 1, 4, 2, -2, 9, 3, 1, 7, 1]) == {'min': -2, 'max': 9}:
|
306 |
-
print("Test 1 : OK")
|
307 |
-
c += 1
|
308 |
-
else :
|
309 |
-
print("Test 1 : échec")
|
310 |
-
if min_et_max([0, 1, 2, 3]) == {'min': 0, 'max': 3}:
|
311 |
-
print("Test 2 : OK")
|
312 |
-
c += 1
|
313 |
-
else :
|
314 |
-
print("Test 2 : échec")
|
315 |
-
if min_et_max([3]) == {'min': 3, 'max': 3}:
|
316 |
-
print("Test 3 : OK")
|
317 |
-
c += 1
|
318 |
-
else :
|
319 |
-
print("Test 3 : échec")
|
320 |
-
if min_et_max([1, 3, 2, 1, 3]) == {'min': 1, 'max': 3}:
|
321 |
-
print("Test 4 : OK")
|
322 |
-
c += 1
|
323 |
-
else :
|
324 |
-
print("Test 4 : échec")
|
325 |
-
if min_et_max([-1, -1, -1, -1, -1]) == {'min': -1, 'max': -1}:
|
326 |
-
print("Test 5 : OK")
|
327 |
-
c += 1
|
328 |
-
else :
|
329 |
-
print("Test 5 : échec")
|
330 |
-
if c == 5 :
|
331 |
-
print("OK")
|
332 |
-
else :
|
333 |
-
print("KO")
|
334 |
-
;;;
|
335 |
-
nombre de répétitions (N2)
|
336 |
-
;;;
|
337 |
-
Écrire une fonction Python appelée nb_repetitions qui prend en paramètres un élément elt et un tableau tab (type list) d’éléments du même type et qui renvoie le nombre de fois où l’élément apparaît dans le tableau.
|
338 |
-
Exemples :
|
339 |
-
>>> nb_repetitions(5, [2, 5, 3, 5, 6, 9, 5])
|
340 |
-
3
|
341 |
-
>>> nb_repetitions('A', ['B', 'A', 'B', 'A', 'R'])
|
342 |
-
2
|
343 |
-
>>> nb_repetitions(12, [1, 3, 7, 21, 36, 44])
|
344 |
-
0
|
345 |
-
;;;
|
346 |
-
c = 0
|
347 |
-
if nb_repetitions(5, [2, 5, 3, 5, 6, 9, 5]) == 3:
|
348 |
-
print("Test 1 : OK")
|
349 |
-
c += 1
|
350 |
-
else :
|
351 |
-
print("Test 1 : échec")
|
352 |
-
if nb_repetitions('A', ['B', 'A', 'B', 'A', 'R']) == 2 :
|
353 |
-
print("Test 2 : OK")
|
354 |
-
c += 1
|
355 |
-
else :
|
356 |
-
print("Test 2 : échec")
|
357 |
-
if nb_repetitions(12, [1, 3, 7, 21, 36, 44]) == 0:
|
358 |
-
print("Test 3 : OK")
|
359 |
-
c += 1
|
360 |
-
else :
|
361 |
-
print("Test 3 : échec")
|
362 |
-
if c == 3 :
|
363 |
-
print("OK")
|
364 |
-
else :
|
365 |
-
print("KO")
|
366 |
-
;;;
|
367 |
-
recherche motif (N3)
|
368 |
-
;;;
|
369 |
-
Écrire une fonction recherche_motif qui prend en paramètres une chaîne de caractères motif non vide et une chaîne de caractères texte et qui renvoie la liste des positions de motif dans texte. Si motif n’apparaît pas, la fonction renvoie une liste vide.
|
370 |
-
Exemples:
|
371 |
-
>>> recherche_motif("ab", "")
|
372 |
-
[]
|
373 |
-
>>> recherche_motif("ab", "cdcdcdcd")
|
374 |
-
[]
|
375 |
-
>>> recherche_motif("ab", "abracadabra")
|
376 |
-
[0, 7]
|
377 |
-
>>> recherche_motif("ab", "abracadabraab")
|
378 |
-
[0, 7, 11]
|
379 |
-
;;;
|
380 |
-
c = 0
|
381 |
-
if recherche_motif("ab", "") == []:
|
382 |
-
print("Test 1 : OK")
|
383 |
-
c += 1
|
384 |
-
else :
|
385 |
-
print("Test 1 : échec")
|
386 |
-
if recherche_motif("ab", "cdcdcdcd") == []:
|
387 |
-
print("Test 2 : OK")
|
388 |
-
c += 1
|
389 |
-
else :
|
390 |
-
print("Test 2 : échec")
|
391 |
-
if recherche_motif("ab", "abracadabra") == [0, 7]:
|
392 |
-
print("Test 3 : OK")
|
393 |
-
c += 1
|
394 |
-
else :
|
395 |
-
print("Test 3 : échec")
|
396 |
-
if recherche_motif("ab", "abracadabraab") == [0, 7, 11]:
|
397 |
-
print("Test 4 : OK")
|
398 |
-
c += 1
|
399 |
-
else :
|
400 |
-
print("Test 4 : échec")
|
401 |
-
if c == 4 :
|
402 |
-
print("OK")
|
403 |
-
else :
|
404 |
-
print("KO")
|
405 |
-
;;;
|
406 |
-
recherche indice classement (N2)
|
407 |
-
;;;
|
408 |
-
Écrire une fonction recherche_indices_classement qui prend en paramètres un entier elt et un tableau d’entiers tab représenté par une liste Python, et qui renvoie trois listes Python d’entiers:
|
409 |
-
• la première liste contient les indices des valeurs du tableau tab strictement inférieures à elt ;
|
410 |
-
• la deuxième liste contient les indices des valeurs du tableau tab égales à elt ;
|
411 |
-
• la troisième liste contient les indices des valeurs du tableau tab strictement supérieures à elt.
|
412 |
-
Exemples :
|
413 |
-
>>> recherche_indices_classement(3, [1, 3, 4, 2, 4, 6, 3, 0])
|
414 |
-
([0, 3, 7], [1, 6], [2, 4, 5])
|
415 |
-
>>> recherche_indices_classement(3, [1, 4, 2, 4, 6, 0])
|
416 |
-
([0, 2, 5], [], [1, 3, 4])
|
417 |
-
>>>recherche_indices_classement(3, [1, 1, 1, 1])
|
418 |
-
([0, 1, 2, 3], [], [])
|
419 |
-
>>> recherche_indices_classement(3, [])
|
420 |
-
([], [], [])
|
421 |
-
;;;
|
422 |
-
c = 0
|
423 |
-
if recherche_indices_classement(3, [1, 3, 4, 2, 4, 6, 3, 0]) == ([0, 3, 7], [1, 6], [2, 4, 5]):
|
424 |
-
print("Test 1 : OK")
|
425 |
-
c += 1
|
426 |
-
else :
|
427 |
-
print("Test 1 : échec")
|
428 |
-
if recherche_indices_classement(3, [1, 4, 2, 4, 6, 0]) == ([0, 2, 5], [], [1, 3, 4]):
|
429 |
-
print("Test 2 : OK")
|
430 |
-
c += 1
|
431 |
-
else :
|
432 |
-
print("Test 2 : échec")
|
433 |
-
if recherche_indices_classement(3, [1, 1, 1, 1]) == ([0, 1, 2, 3], [], []):
|
434 |
-
print("Test 3 : OK")
|
435 |
-
c += 1
|
436 |
-
else :
|
437 |
-
print("Test 3 : échec")
|
438 |
-
if recherche_indices_classement(3, []) == ([], [], []):
|
439 |
-
print("Test 4 : OK")
|
440 |
-
c += 1
|
441 |
-
else :
|
442 |
-
print("Test 4 : échec")
|
443 |
-
if c == 4 :
|
444 |
-
print("OK")
|
445 |
-
else :
|
446 |
-
print("KO")
|
447 |
-
;;;
|
448 |
-
parcours largeur arbre (N3 T)
|
449 |
-
;;;
|
450 |
-
Un arbre binaire est soit vide, représenté en Python par la valeur None, soit un nœud représenté par un triplet (g, x, d) où x est l’étiquette du nœud et g et d sont les sousarbres gauche et droit.
|
451 |
-
On souhaite écrire une fonction parcours_largeur qui prend en paramètre un arbre binaire et qui renvoie la liste des étiquettes des nœuds de l’arbre parcourus en largeur.
|
452 |
-
Exemples :
|
453 |
-
>>> parcours_largeur(( ( (None, 1, None), 2, (None, 3, None) ),4,( (None, 5, None), 6, (None, 7, None) ) ))
|
454 |
-
[4, 2, 6, 1, 3, 5, 7]
|
455 |
-
;;;
|
456 |
-
c = 0
|
457 |
-
if parcours_largeur(( ( (None, 1, None), 2, (None, 3, None) ),4,( (None, 5, None), 6, (None, 7, None) ) )) == [4, 2, 6, 1, 3, 5, 7]:
|
458 |
-
print("Test 1 : OK")
|
459 |
-
c += 1
|
460 |
-
else :
|
461 |
-
print("Test 1 : échec")
|
462 |
-
if c == 1 :
|
463 |
-
print("OK")
|
464 |
-
else :
|
465 |
-
print("KO")
|
466 |
-
;;;
|
467 |
-
recherche minimum (N2)
|
468 |
-
;;;
|
469 |
-
Écrire une fonction recherche_min qui prend en paramètre un tableau de nombres tab, et qui renvoie l’indice de la première occurrence du minimum de ce tableau. Les tableaux seront représentés sous forme de liste Python.
|
470 |
-
Il est interdit d'utiliser les fonctions min et index de Python.
|
471 |
-
Exemples :
|
472 |
-
>>> recherche_min([5])
|
473 |
-
0
|
474 |
-
>>> recherche_min([2, 4, 1])
|
475 |
-
2
|
476 |
-
>>> recherche_min([5, 3, 2, 2, 4])
|
477 |
-
2
|
478 |
-
>>> recherche_min([-1, -2, -3, -3])
|
479 |
-
2
|
480 |
-
;;;
|
481 |
-
c = 0
|
482 |
-
if recherche_min([5]) == 0:
|
483 |
-
print("Test 1 : OK")
|
484 |
-
c += 1
|
485 |
-
else :
|
486 |
-
print("Test 1 : échec")
|
487 |
-
if recherche_min([2, 4, 1]) == 2:
|
488 |
-
print("Test 2 : OK")
|
489 |
-
c += 1
|
490 |
-
else :
|
491 |
-
print("Test 2 : échec")
|
492 |
-
if recherche_min([5, 3, 2, 2, 4]) == 2:
|
493 |
-
print("Test 3 : OK")
|
494 |
-
c += 1
|
495 |
-
else :
|
496 |
-
print("Test 3 : échec")
|
497 |
-
if recherche_min([-1, -2, -3, -3]) == 2:
|
498 |
-
print("Test 4 : OK")
|
499 |
-
c += 1
|
500 |
-
else :
|
501 |
-
print("Test 4 : échec")
|
502 |
-
if c == 4 :
|
503 |
-
print("OK")
|
504 |
-
else :
|
505 |
-
print("KO")
|
506 |
-
;;;
|
507 |
-
ajoute dictionnaire (N3)
|
508 |
-
;;;
|
509 |
-
Écrire une fonction ajoute_dictionnaires qui prend en paramètres deux dictionnaires d1 et d2 dont les clés et les valeurs associées sont des nombres et renvoie le dictionnaire d défini de la façon suivante :
|
510 |
-
• les clés de d sont celles de d1 et celles de d2 réunies ;
|
511 |
-
• si une clé est présente dans les deux dictionnaires d1 et d2, sa valeur associée dans le dictionnaire d est la somme de ses valeurs dans les dictionnaires d1 et d2 ;
|
512 |
-
• si une clé n’est présente que dans un des deux dictionnaires, sa valeur associée dans le dictionnaire d est la même que sa valeur dans le dictionnaire où elle est présente.
|
513 |
-
Exemples :
|
514 |
-
>>> ajoute_dictionnaires({1: 5, 2: 7}, {2: 9, 3: 11})
|
515 |
-
{1: 5, 2: 16, 3: 11}
|
516 |
-
>>> ajoute_dictionnaires({}, {2: 9, 3: 11})
|
517 |
-
{2: 9, 3: 11}
|
518 |
-
>>> ajoute_dictionnaires({1: 5, 2: 7}, {})
|
519 |
-
{1: 5, 2: 7}
|
520 |
-
;;;
|
521 |
-
c = 0
|
522 |
-
if ajoute_dictionnaires({1: 5, 2: 7}, {2: 9, 3: 11}) == {1: 5, 2: 16, 3: 11}:
|
523 |
-
print("Test 1 : OK")
|
524 |
-
c += 1
|
525 |
-
else :
|
526 |
-
print("Test 1 : échec")
|
527 |
-
if ajoute_dictionnaires({}, {2: 9, 3: 11}) == {2: 9, 3: 11}:
|
528 |
-
print("Test 2 : OK")
|
529 |
-
c += 1
|
530 |
-
else :
|
531 |
-
print("Test 2 : échec")
|
532 |
-
if ajoute_dictionnaires({1: 5, 2: 7}, {}) == {1: 5, 2: 7}:
|
533 |
-
print("Test 3 : OK")
|
534 |
-
c += 1
|
535 |
-
else :
|
536 |
-
print("Test 3 : échec")
|
537 |
-
if c == 3 :
|
538 |
-
print("OK")
|
539 |
-
else :
|
540 |
-
print("KO")
|
541 |
-
;;;
|
542 |
-
plus grande valeur (N1)
|
543 |
-
;;;
|
544 |
-
Écrire une fonction plus_grande qui prend en paramètres deux entiers a et b. Cette fonction renvoie a si a est plus grand que b et renvoie b si b est plus grand que a.
|
545 |
-
Exemples :
|
546 |
-
>>> plus_grande(15,3)
|
547 |
-
15
|
548 |
-
>>> plus_grande(-15,-3)
|
549 |
-
-3
|
550 |
-
;;;
|
551 |
-
c = 0
|
552 |
-
if plus_grande(15,3) == 15:
|
553 |
-
print("Test 1 : OK")
|
554 |
-
c += 1
|
555 |
-
else :
|
556 |
-
print("Test 1 : échec")
|
557 |
-
if plus_grande(-15,-3) == -3:
|
558 |
-
print("Test 2 : OK")
|
559 |
-
c += 1
|
560 |
-
else :
|
561 |
-
print("Test 2 : échec")
|
562 |
-
if c == 2 :
|
563 |
-
print("OK")
|
564 |
-
else :
|
565 |
-
print("KO")
|
566 |
-
;;;
|
567 |
-
couples consecutifs (N3)
|
568 |
-
;;;
|
569 |
-
Écrire une fonction couples_consecutifs qui prend en paramètre un tableau de nombres entiers tab non vide (type list), et qui renvoie la liste Python (éventuellement vide) des couples d’entiers consécutifs successifs qu’il peut y avoir dans tab.
|
570 |
-
Exemples :
|
571 |
-
>>> couples_consecutifs([1, 4, 3, 5])
|
572 |
-
[]
|
573 |
-
>>> couples_consecutifs([1, 4, 5, 3])
|
574 |
-
[(4, 5)]
|
575 |
-
>>> couples_consecutifs([1, 1, 2, 4])
|
576 |
-
[(1, 2)]
|
577 |
-
>>> couples_consecutifs([7, 1, 2, 5, 3, 4])
|
578 |
-
[(1, 2), (3, 4)]
|
579 |
-
>>> couples_consecutifs([5, 1, 2, 3, 8, -5, -4, 7])
|
580 |
-
[(1, 2), (2, 3), (-5, -4)]
|
581 |
-
;;;
|
582 |
-
c = 0
|
583 |
-
if couples_consecutifs([1, 4, 3, 5]) == []:
|
584 |
-
print("Test 1 : OK")
|
585 |
-
c += 1
|
586 |
-
else :
|
587 |
-
print("Test 1 : échec")
|
588 |
-
if couples_consecutifs([1, 4, 5, 3]) == [(4, 5)]:
|
589 |
-
print("Test 2 : OK")
|
590 |
-
c += 1
|
591 |
-
else :
|
592 |
-
print("Test 2 : échec")
|
593 |
-
if couples_consecutifs([1, 1, 2, 4]) == [(1, 2)]:
|
594 |
-
print("Test 3 : OK")
|
595 |
-
c += 1
|
596 |
-
else :
|
597 |
-
print("Test 3 : échec")
|
598 |
-
if couples_consecutifs([7, 1, 2, 5, 3, 4]) == [(1, 2), (3, 4)]:
|
599 |
-
print("Test 4 : OK")
|
600 |
-
c += 1
|
601 |
-
else :
|
602 |
-
print("Test 4 : échec")
|
603 |
-
if couples_consecutifs([5, 1, 2, 3, 8, -5, -4, 7]) == [(1, 2), (2, 3), (-5, -4)]:
|
604 |
-
print("Test 5 : OK")
|
605 |
-
c += 1
|
606 |
-
else :
|
607 |
-
print("Test 5 : échec")
|
608 |
-
if c == 5 :
|
609 |
-
print("OK")
|
610 |
-
else :
|
611 |
-
print("KO")
|
612 |
-
;;;
|
613 |
-
moyenne (N1)
|
614 |
-
;;;
|
615 |
-
Écrire une fonction moyenne qui prend en paramètre 3 entiers a, b et c. Cette fonction renvoie la moyenne des entiers a, b et c.
|
616 |
-
>>> moyenne(5, 10, 15)
|
617 |
-
10
|
618 |
-
;;;
|
619 |
-
c = 0
|
620 |
-
if moyenne(5, 10, 15) == 10:
|
621 |
-
print("Test 1 : OK")
|
622 |
-
c += 1
|
623 |
-
else :
|
624 |
-
print("Test 1 : échec")
|
625 |
-
if c == 1 :
|
626 |
-
print("OK")
|
627 |
-
else :
|
628 |
-
print("KO")
|
629 |
-
;;;
|
630 |
-
recheche arbre binaire de recherche (N2 T)
|
631 |
-
;;;
|
632 |
-
Écrire une fonction recherche_abr qui prend en paramètre une instance de la classe arbre T (T étant un arbre binaire de recherche) et un entier n. Cette fonction renvoie True si n est présent dans T et False dans le cas contraire.
|
633 |
-
La classe Arbre possède 3 méthodes :
|
634 |
-
- get_gauche() renvoie l'arbre gauche
|
635 |
-
- get_droit() renvoie l'arbre droit
|
636 |
-
- get_valeur() renvoie la valeur du noeud
|
637 |
-
;;;
|
638 |
-
class ArbreBinaire:
|
639 |
-
def __init__(self, valeur):
|
640 |
-
self.valeur = valeur
|
641 |
-
self.enfant_gauche = None
|
642 |
-
self.enfant_droit = None
|
643 |
-
def insert_gauche(self, valeur):
|
644 |
-
if self.enfant_gauche == None:
|
645 |
-
self.enfant_gauche = ArbreBinaire(valeur)
|
646 |
-
else:
|
647 |
-
new_node = ArbreBinaire(valeur)
|
648 |
-
new_node.enfant_gauche = self.enfant_gauche
|
649 |
-
self.enfant_gauche = new_node
|
650 |
-
def insert_droit(self, valeur):
|
651 |
-
if self.enfant_droit == None:
|
652 |
-
self.enfant_droit = ArbreBinaire(valeur)
|
653 |
-
else:
|
654 |
-
new_node = ArbreBinaire(valeur)
|
655 |
-
new_node.enfant_droit = self.enfant_droit
|
656 |
-
self.enfant_droit = new_node
|
657 |
-
def get_valeur(self):
|
658 |
-
return self.valeur
|
659 |
-
def get_gauche(self):
|
660 |
-
return self.enfant_gauche
|
661 |
-
def get_droit(self):
|
662 |
-
return self.enfant_droit
|
663 |
-
racine_r = ArbreBinaire(15)
|
664 |
-
racine_r.insert_gauche(6)
|
665 |
-
racine_r.insert_droit(18)
|
666 |
-
|
667 |
-
r_6 = racine_r.get_gauche()
|
668 |
-
r_6.insert_gauche(3)
|
669 |
-
r_6.insert_droit(7)
|
670 |
-
|
671 |
-
r_18 = racine_r.get_droit()
|
672 |
-
r_18.insert_gauche(17)
|
673 |
-
r_18.insert_droit(20)
|
674 |
-
|
675 |
-
r_3 = r_6.get_gauche()
|
676 |
-
r_3.insert_gauche(2)
|
677 |
-
r_3.insert_droit(4)
|
678 |
-
|
679 |
-
r_7 = r_6.get_droit()
|
680 |
-
r_7.insert_droit(13)
|
681 |
-
|
682 |
-
r_13 = r_7.get_droit()
|
683 |
-
r_13.insert_gauche(9)
|
684 |
-
c = 0
|
685 |
-
if recherche_abr(racine_r,4):
|
686 |
-
print("Test 1 : OK")
|
687 |
-
c += 1
|
688 |
-
else :
|
689 |
-
print("Test 1 : échec")
|
690 |
-
if not recherche_abr(racine_r,42):
|
691 |
-
print("Test 2 : OK")
|
692 |
-
c += 1
|
693 |
-
else :
|
694 |
-
print("Test 2 : échec")
|
695 |
-
if c == 2 :
|
696 |
-
print("OK")
|
697 |
-
else :
|
698 |
-
print("KO")
|
699 |
-
;;;
|
700 |
-
suite fibonacci (N3)
|
701 |
-
;;;
|
702 |
-
On s’intéresse à la suite d’entiers définie par :
|
703 |
-
• la première valeur est égale à 0 ;
|
704 |
-
• la deuxième valeur est égale à 1 ;
|
705 |
-
• ensuite, chaque valeur est obtenue en faisant la somme des deux valeurs qui le précèdent.
|
706 |
-
La troisième valeur est donc 0+1 = 1, la quatrième est 1+2 = 3, la cinquième est 2+3 = 5, la sixième est 3 + 5 = 8, et ainsi de suite.
|
707 |
-
Cette suite d’entiers est connue sous le nom de suite de Fibonacci.
|
708 |
-
Écrire en Python une fonction fibonacci qui prend en paramètre un entier n supposé strictement positif et qui renvoie le terme d’indice n de cette suite.
|
709 |
-
Exemples :
|
710 |
-
>>> fibonacci(0)
|
711 |
-
0
|
712 |
-
>>> fibonacci(1)
|
713 |
-
1
|
714 |
-
>>> fibonacci(2)
|
715 |
-
1
|
716 |
-
>>> fibonacci(10)
|
717 |
-
55
|
718 |
-
>>> fibonacci(13)
|
719 |
-
233
|
720 |
-
;;;
|
721 |
-
c = 0
|
722 |
-
if fibonacci(0) == 0:
|
723 |
-
print("Test 1 : OK")
|
724 |
-
c += 1
|
725 |
-
else :
|
726 |
-
print("Test 1 : échec")
|
727 |
-
if fibonacci(1) == 1:
|
728 |
-
print("Test 2 : OK")
|
729 |
-
c += 1
|
730 |
-
else :
|
731 |
-
print("Test 2 : échec")
|
732 |
-
if fibonacci(10) == 55:
|
733 |
-
print("Test 3 : OK")
|
734 |
-
c += 1
|
735 |
-
else :
|
736 |
-
print("Test 3 : échec")
|
737 |
-
if fibonacci(13) == 233:
|
738 |
-
print("Test 4 : OK")
|
739 |
-
c += 1
|
740 |
-
else :
|
741 |
-
print("Test 4 : échec")
|
742 |
-
if c == 4 :
|
743 |
-
print("OK")
|
744 |
-
else :
|
745 |
-
print("KO")
|
746 |
-
;;;
|
747 |
-
fusion tableaux (N3)
|
748 |
-
;;;
|
749 |
-
Programmer la fonction fusion prenant en paramètres deux tableaux non vides tab1 et tab2 (type list) d’entiers, chacun dans l’ordre croissant, et renvoyant un tableau trié dans l’ordre croissant et contenant l’ensemble des valeurs de tab1 et tab2.
|
750 |
-
Exemples :
|
751 |
-
>>> fusion([3, 5], [2, 5])
|
752 |
-
[2, 3, 5, 5]
|
753 |
-
>>> fusion([-2, 4], [-3, 5, 10])
|
754 |
-
[-3, -2, 4, 5, 10]
|
755 |
-
>>> fusion([4], [2, 6])
|
756 |
-
[2, 4, 6]
|
757 |
-
>>> fusion([], [])
|
758 |
-
[]
|
759 |
-
>>> fusion([1, 2, 3], [])
|
760 |
-
[1, 2, 3]
|
761 |
-
;;;
|
762 |
-
c = 0
|
763 |
-
if fusion([3, 5], [2, 5]) == [2, 3, 5, 5]:
|
764 |
-
print("Test 1 : OK")
|
765 |
-
c += 1
|
766 |
-
else :
|
767 |
-
print("Test 1 : échec")
|
768 |
-
if fusion([-2, 4], [-3, 5, 10]) == [-3, -2, 4, 5, 10]:
|
769 |
-
print("Test 2 : OK")
|
770 |
-
c += 1
|
771 |
-
else :
|
772 |
-
print("Test 2 : échec")
|
773 |
-
if fusion([4], [2, 6]) == [2, 4, 6]:
|
774 |
-
print("Test 3 : OK")
|
775 |
-
c += 1
|
776 |
-
else :
|
777 |
-
print("Test 3 : échec")
|
778 |
-
if fusion([], []) == []:
|
779 |
-
print("Test 4 : OK")
|
780 |
-
c += 1
|
781 |
-
else :
|
782 |
-
print("Test 4 : échec")
|
783 |
-
if fusion([1, 2, 3], []) == [1, 2, 3]:
|
784 |
-
print("Test 5 : OK")
|
785 |
-
c += 1
|
786 |
-
else :
|
787 |
-
print("Test 5 : échec")
|
788 |
-
if c == 5 :
|
789 |
-
print("OK")
|
790 |
-
else :
|
791 |
-
print("KO")
|
792 |
-
;;;
|
793 |
-
multiplication (N4)
|
794 |
-
;;;
|
795 |
-
Programmer la fonction multiplication qui prend en paramètres deux nombres entiers relatifs n1 et n2, et qui renvoie le produit de ces deux nombres.
|
796 |
-
Les seules opérations arithmétiques autorisées sont l’addition et la soustraction.
|
797 |
-
Exemples :
|
798 |
-
>>> multiplication(3, 5)
|
799 |
-
15
|
800 |
-
>>> multiplication(-4, -8)
|
801 |
-
32
|
802 |
-
>>> multiplication(-2, 6)
|
803 |
-
-12
|
804 |
-
>>> multiplication(-2, 0)
|
805 |
-
0
|
806 |
-
;;;
|
807 |
-
c = 0
|
808 |
-
if multiplication(3, 5) == 15:
|
809 |
-
print("Test 1 : OK")
|
810 |
-
c += 1
|
811 |
-
else :
|
812 |
-
print("Test 1 : échec")
|
813 |
-
if multiplication(-4, -8) == 32:
|
814 |
-
print("Test 2 : OK")
|
815 |
-
c += 1
|
816 |
-
else :
|
817 |
-
print("Test 2 : échec")
|
818 |
-
if multiplication(-2, 6) == -12:
|
819 |
-
print("Test 3 : OK")
|
820 |
-
c += 1
|
821 |
-
else :
|
822 |
-
print("Test 3 : échec")
|
823 |
-
if multiplication(-2, 0) == 0:
|
824 |
-
print("Test 4 : OK")
|
825 |
-
c += 1
|
826 |
-
else :
|
827 |
-
print("Test 4 : échec")
|
828 |
-
if c == 4 :
|
829 |
-
print("OK")
|
830 |
-
else :
|
831 |
-
print("KO")
|
832 |
-
;;;
|
833 |
-
renverse chaine (N3)
|
834 |
-
;;;
|
835 |
-
Programmer une fonction renverse qui prend en paramètre une chaîne de caractères mot et qui renvoie cette chaîne de caractères en ordre inverse.
|
836 |
-
Exemple :
|
837 |
-
>>> renverse("")
|
838 |
-
""
|
839 |
-
>>> renverse("abc")
|
840 |
-
"cba"
|
841 |
-
>>> renverse("informatique")
|
842 |
-
"euqitamrofni"
|
843 |
-
;;;
|
844 |
-
c = 0
|
845 |
-
if renverse("") == "":
|
846 |
-
print("Test 1 : OK")
|
847 |
-
c += 1
|
848 |
-
else :
|
849 |
-
print("Test 1 : échec")
|
850 |
-
if renverse("abc") == "cba":
|
851 |
-
print("Test 2 : OK")
|
852 |
-
c += 1
|
853 |
-
else :
|
854 |
-
print("Test 2 : échec")
|
855 |
-
if renverse("informatique") == "euqitamrofni":
|
856 |
-
print("Test 3 : OK")
|
857 |
-
c += 1
|
858 |
-
else :
|
859 |
-
print("Test 3 : échec")
|
860 |
-
if c == 3 :
|
861 |
-
print("OK")
|
862 |
-
else :
|
863 |
-
print("KO")
|
864 |
-
;;;
|
865 |
-
nombre d’occurrences d’un caractère (N3)
|
866 |
-
;;;
|
867 |
-
Le nombre d’occurrences d’un caractère dans une chaîne de caractères est le nombre d’apparitions de ce caractère dans la chaîne.
|
868 |
-
Exemples :
|
869 |
-
• le nombre d’occurrences du caractère 'o' dans 'bonjour' est 2 ;
|
870 |
-
• le nombre d’occurrences du caractère 'b' dans 'Bébé' est 1 ;
|
871 |
-
• le nombre d’occurrences du caractère 'B' dans 'Bébé' est 1 ;
|
872 |
-
• le nombre d’occurrences du caractère ' ' dans 'Hello world !' est 2.
|
873 |
-
On cherche les occurrences des caractères dans une phrase. On souhaite stocker ces occurrences dans un dictionnaire dont les clefs seraient les caractères de la phrase et les valeurs le nombre d’occurrences de ces caractères.
|
874 |
-
Par exemple : avec la phrase 'Hello world !' le dictionnaire est le suivant :
|
875 |
-
|
876 |
-
{'H': 1,'e': 1,'l': 3,'o': 2,' ': 2,'w': 1,'r': 1,'d': 1,'!': 1}
|
877 |
-
|
878 |
-
L’ordre des clefs n’a pas d’importance.
|
879 |
-
Écrire une fonction nbr_occurrences prenant comme paramètre une chaîne de caractères chaine et renvoyant le dictionnaire des nombres d’occurrences des caractères de cette chaîne.
|
880 |
-
;;;
|
881 |
-
c = 0
|
882 |
-
if nbr_occurrences('Hello world !') == {'H': 1,'e': 1,'l': 3,'o': 2,' ': 2,'w': 1,'r': 1,'d': 1,'!': 1}:
|
883 |
-
print("Test 1 : OK")
|
884 |
-
c += 1
|
885 |
-
else :
|
886 |
-
print("Test 1 : échec")
|
887 |
-
if c == 1 :
|
888 |
-
print("OK")
|
889 |
-
else :
|
890 |
-
print("KO")
|
891 |
-
;;;
|
892 |
-
temperature minimale (N3)
|
893 |
-
;;;
|
894 |
-
On a relevé les valeurs moyennes annuelles des températures à Paris pour la période allant de 2013 à 2019. Les résultats ont été récupérés sous la forme de deux tableaux (de type list) : l’un pour les températures, l’autre pour les années :
|
895 |
-
|
896 |
-
t_moy = [14.9, 13.3, 13.1, 12.5, 13.0, 13.6, 13.7]
|
897 |
-
annees = [2013, 2014, 2015, 2016, 2017, 2018, 2019]
|
898 |
-
|
899 |
-
Écrire la fonction annee_temperature_minimale qui prend en paramètres ces deux tableaux et qui renvoie la plus petite valeur relevée au cours de la période et l’année correspondante.
|
900 |
-
On suppose que la température minimale est atteinte une seule fois.
|
901 |
-
Exemple :
|
902 |
-
>>> annee_temperature_minimale(t_moy, annees)
|
903 |
-
(12.5, 2016)
|
904 |
-
;;;
|
905 |
-
c = 0
|
906 |
-
if annee_temperature_minimale([14.9, 13.3, 13.1, 12.5, 13.0, 13.6, 13.7], [2013, 2014, 2015, 2016, 2017, 2018, 2019]) == (12.5, 2016):
|
907 |
-
print("Test 1 : OK")
|
908 |
-
c += 1
|
909 |
-
else :
|
910 |
-
print("Test 1 : échec")
|
911 |
-
if c == 1 :
|
912 |
-
print("OK")
|
913 |
-
else :
|
914 |
-
print("KO")
|
915 |
-
;;;
|
916 |
-
occurrences (N2)
|
917 |
-
;;;
|
918 |
-
Écrire une fonction occurrences(caractere, chaine) qui prend en paramètres caractere, une chaîne de caractère de longueur 1, et chaine, une chaîne de caractères.
|
919 |
-
Cette fonction renvoie le nombre d’occurrences de caractere dans chaine, c’est-à-dire le nombre de fois où caractere apparaît dans chaine.
|
920 |
-
Exemples :
|
921 |
-
>>> occurrences('e', "sciences")
|
922 |
-
2
|
923 |
-
>>> occurrences('i',"mississippi")
|
924 |
-
4
|
925 |
-
>>> occurrences('a',"mississippi")
|
926 |
-
0
|
927 |
-
;;;
|
928 |
-
c = 0
|
929 |
-
if occurrences('e', "sciences") == 2:
|
930 |
-
print("Test 1 : OK")
|
931 |
-
c += 1
|
932 |
-
else :
|
933 |
-
print("Test 1 : échec")
|
934 |
-
if occurrences('i',"mississippi") == 4:
|
935 |
-
print("Test 2 : OK")
|
936 |
-
c += 1
|
937 |
-
else :
|
938 |
-
print("Test 2 : échec")
|
939 |
-
if occurrences('a',"mississippi") == 0:
|
940 |
-
print("Test 3 : OK")
|
941 |
-
c += 1
|
942 |
-
else :
|
943 |
-
print("Test 3 : échec")
|
944 |
-
if c == 3 :
|
945 |
-
print("OK")
|
946 |
-
else :
|
947 |
-
print("KO")
|
948 |
-
;;;
|
949 |
-
aire cercle (N1)
|
950 |
-
;;;
|
951 |
-
Écrire une fonction aire_cercle qui prend en paramètre le rayon d'un cercle r et qui renvoie l'aire du cercle.
|
952 |
-
Exemple :
|
953 |
-
>>> aire_cercle(2)
|
954 |
-
28.274
|
955 |
-
;;;
|
956 |
-
c = 0
|
957 |
-
import math
|
958 |
-
if round(aire_cercle(3)) == 28:
|
959 |
-
print("Test 1 : OK")
|
960 |
-
c += 1
|
961 |
-
else :
|
962 |
-
print("Test 1 : échec")
|
963 |
-
if c == 1 :
|
964 |
-
print("OK")
|
965 |
-
else :
|
966 |
-
print("KO")
|
967 |
-
;;;
|
968 |
-
indices maxi (N2)
|
969 |
-
;;;
|
970 |
-
Écrire une fonction indices_maxi qui prend en paramètre un tableau non vide de nombres entiers tab, représenté par une liste Python et qui renvoie un tuple (maxi, indices) où :
|
971 |
-
• maxi est le plus grand élément du tableau tab ;
|
972 |
-
• indices est une liste Python contenant les indices du tableau tab où apparaît ce plus grand élément.
|
973 |
-
Exemple :
|
974 |
-
>>> indices_maxi([1, 5, 6, 9, 1, 2, 3, 7, 9, 8])
|
975 |
-
(9, [3, 8])
|
976 |
-
>>> indices_maxi([7])
|
977 |
-
(7, [0])
|
978 |
-
;;;
|
979 |
-
c = 0
|
980 |
-
if indices_maxi([1, 5, 6, 9, 1, 2, 3, 7, 9, 8]) == (9, [3, 8]):
|
981 |
-
print("Test 1 : OK")
|
982 |
-
c += 1
|
983 |
-
else :
|
984 |
-
print("Test 1 : échec")
|
985 |
-
if indices_maxi([7]) == (7, [0]):
|
986 |
-
print("Test 2 : OK")
|
987 |
-
c += 1
|
988 |
-
else :
|
989 |
-
print("Test 2 : échec")
|
990 |
-
if c == 2 :
|
991 |
-
print("OK")
|
992 |
-
else :
|
993 |
-
print("KO")
|
994 |
-
;;;
|
995 |
-
addition (N1)
|
996 |
-
;;;
|
997 |
-
Écrire une fonction add qui prend en paramètre 2 entiers a et b. Cette fonction renvoie l'addition de a et b.
|
998 |
-
Exemple :
|
999 |
-
>>> add(3, 5)
|
1000 |
-
8
|
1001 |
-
;;;
|
1002 |
-
c = 0
|
1003 |
-
if add(3,5) == 8:
|
1004 |
-
print("Test 1 : OK")
|
1005 |
-
c += 1
|
1006 |
-
else :
|
1007 |
-
print("Test 1 : échec")
|
1008 |
-
if c == 1 :
|
1009 |
-
print("OK")
|
1010 |
-
else :
|
1011 |
-
print("KO")
|
1012 |
-
;;;
|
1013 |
-
sélection enclos (N3)
|
1014 |
-
;;;
|
1015 |
-
On considère des tables, c’est-à-dire des tableaux de dictionnaires ayant tous les mêmes clés, qui contiennent des enregistrements relatifs à des animaux hébergés dans un refuge. Les attributs des enregistrements sont 'nom', 'espece', 'age', 'enclos'.
|
1016 |
-
Voici un exemple d’une telle table :
|
1017 |
-
animaux = [ {'nom':'Medor', 'espece':'chien', 'age':5, 'enclos':2}, {'nom':'Titine', 'espece':'chat', 'age':2, 'enclos':5}, {'nom':'Tom', 'espece':'chat', 'age':7, 'enclos':4}, {'nom':'Belle', 'espece':'chien', 'age':6, 'enclos':3}, {'nom':'Mirza', 'espece':'chat', 'age':6, 'enclos':5}]
|
1018 |
-
Programmer une fonction selection_enclos qui :
|
1019 |
-
prend en paramètres : une table animaux contenant des enregistrements relatifs à des animaux (comme dans l’exemple ci-dessus) et un numéro d’enclos num_enclos ;
|
1020 |
-
La fonction renvoie une table contenant les enregistrements de animaux dont l’attribut 'enclos' est num_enclos.
|
1021 |
-
Exemples avec la table animaux ci-dessus :
|
1022 |
-
>>> selection_enclos(animaux, 5)
|
1023 |
-
[{'nom':'Titine', 'espece':'chat', 'age':2, 'enclos':5},
|
1024 |
-
{'nom':'Mirza', 'espece':'chat', 'age':6, 'enclos':5}]
|
1025 |
-
>>> selection_enclos(animaux, 2)
|
1026 |
-
[{'nom':'Medor', 'espece':'chien', 'age':5, 'enclos':2}]
|
1027 |
-
>>> selection_enclos(animaux, 7)
|
1028 |
-
[]
|
1029 |
-
;;;
|
1030 |
-
c = 0
|
1031 |
-
animaux = [ {'nom':'Medor', 'espece':'chien', 'age':5, 'enclos':2}, {'nom':'Titine', 'espece':'chat', 'age':2, 'enclos':5}, {'nom':'Tom', 'espece':'chat', 'age':7, 'enclos':4}, {'nom':'Belle', 'espece':'chien', 'age':6, 'enclos':3}, {'nom':'Mirza', 'espece':'chat', 'age':6, 'enclos':5}]
|
1032 |
-
if selection_enclos(animaux, 5)== [{'nom':'Titine', 'espece':'chat', 'age':2, 'enclos':5}, {'nom':'Mirza', 'espece':'chat', 'age':6, 'enclos':5}]:
|
1033 |
-
print("Test 1 : OK")
|
1034 |
-
c += 1
|
1035 |
-
else :
|
1036 |
-
print("Test 1 : échec")
|
1037 |
-
if selection_enclos(animaux, 2) == [{'nom':'Medor', 'espece':'chien', 'age':5, 'enclos':2}]:
|
1038 |
-
print("Test 2 : OK")
|
1039 |
-
c += 1
|
1040 |
-
else :
|
1041 |
-
print("Test 2 : échec")
|
1042 |
-
if selection_enclos(animaux, 7) == []:
|
1043 |
-
print("Test 3 : OK")
|
1044 |
-
c += 1
|
1045 |
-
else :
|
1046 |
-
print("Test 3 : échec")
|
1047 |
-
if c == 3 :
|
1048 |
-
print("OK")
|
1049 |
-
else :
|
1050 |
-
print("KO")
|
1051 |
-
;;;
|
1052 |
-
pair (N1)
|
1053 |
-
;;;
|
1054 |
-
Écrire une fonction pair qui prend en paramètre un entier n. Cette fonction renvoie True si n est pair et False si n est impair.
|
1055 |
-
>>> pair(8)
|
1056 |
-
True
|
1057 |
-
>>> pair(5)
|
1058 |
-
False
|
1059 |
-
;;;
|
1060 |
-
c = 0
|
1061 |
-
if pair(8):
|
1062 |
-
print("Test 1 : OK")
|
1063 |
-
c += 1
|
1064 |
-
else :
|
1065 |
-
print("Test 1 : échec")
|
1066 |
-
if not pair(5):
|
1067 |
-
print("Test 2 : OK")
|
1068 |
-
c += 1
|
1069 |
-
else :
|
1070 |
-
print("Test 2 : échec")
|
1071 |
-
if c == 2 :
|
1072 |
-
print("OK")
|
1073 |
-
else :
|
1074 |
-
print("KO")
|
1075 |
-
;;;
|
1076 |
-
doublon (N2)
|
1077 |
-
;;;
|
1078 |
-
Écrire une fonction a_doublon qui prend en paramètre un tableau trié de nombres dans l’ordre croissant et renvoie True si ce tableau contient au moins deux nombres identiques, False sinon.
|
1079 |
-
Exemple :
|
1080 |
-
>>> a_doublon([])
|
1081 |
-
False
|
1082 |
-
>>> a_doublon([1])
|
1083 |
-
False
|
1084 |
-
>>> a_doublon([1, 2, 4, 6, 6])
|
1085 |
-
True
|
1086 |
-
>>> a_doublon([2, 5, 7, 7, 7, 9])
|
1087 |
-
True
|
1088 |
-
>>> a_doublon([0, 2, 3])
|
1089 |
-
False
|
1090 |
-
;;;
|
1091 |
-
c = 0
|
1092 |
-
if not a_doublon([]):
|
1093 |
-
print("Test 1 : OK")
|
1094 |
-
c += 1
|
1095 |
-
else :
|
1096 |
-
print("Test 1 : échec")
|
1097 |
-
if not a_doublon([1]):
|
1098 |
-
print("Test 2 : OK")
|
1099 |
-
c += 1
|
1100 |
-
else :
|
1101 |
-
print("Test 2 : échec")
|
1102 |
-
if a_doublon([1, 2, 4, 6, 6]):
|
1103 |
-
print("Test 3 : OK")
|
1104 |
-
c += 1
|
1105 |
-
else :
|
1106 |
-
print("Test 3 : échec")
|
1107 |
-
if a_doublon([2, 5, 7, 7, 7, 9]):
|
1108 |
-
print("Test 4 : OK")
|
1109 |
-
c += 1
|
1110 |
-
else :
|
1111 |
-
print("Test 4 : échec")
|
1112 |
-
if not a_doublon([0, 2, 3]):
|
1113 |
-
print("Test 5 : OK")
|
1114 |
-
c += 1
|
1115 |
-
else :
|
1116 |
-
print("Test 5 : échec")
|
1117 |
-
if c == 5 :
|
1118 |
-
print("OK")
|
1119 |
-
else :
|
1120 |
-
print("KO")
|
1121 |
-
;;;
|
1122 |
-
somme (N2)
|
1123 |
-
;;;
|
1124 |
-
Écrire une fonction somme qui prend en paramètre un tableau d'entiers tab (de type list). Cette fonction renvoie la somme des éléments présents dans tab.
|
1125 |
-
Il est interdit d'utiliser la fonction Python sum.
|
1126 |
-
Exemples :
|
1127 |
-
>>> somme([5, 4, 7])
|
1128 |
-
16
|
1129 |
-
>>> somme([])
|
1130 |
-
0
|
1131 |
-
;;;
|
1132 |
-
c = 0
|
1133 |
-
if somme([5, 4, 7]) == 16:
|
1134 |
-
print("Test 1 : OK")
|
1135 |
-
c += 1
|
1136 |
-
else :
|
1137 |
-
print("Test 1 : échec")
|
1138 |
-
if somme([]) == 0:
|
1139 |
-
print("Test 2 : OK")
|
1140 |
-
c += 1
|
1141 |
-
else :
|
1142 |
-
print("Test 2 : échec")
|
1143 |
-
if c == 2 :
|
1144 |
-
print("OK")
|
1145 |
-
else :
|
1146 |
-
print("KO")
|
1147 |
-
;;;
|
1148 |
-
énumère (N3)
|
1149 |
-
;;;
|
1150 |
-
Écrire une fonction enumere qui prend en paramètre un tableau tab (typelist) et renvoie un dictionnaire d dont les clés sont les éléments de tab avec pour valeur associée la liste des indices de l’élément dans le tableau tab.
|
1151 |
-
Exemple :
|
1152 |
-
>>> enumere([])
|
1153 |
-
{}
|
1154 |
-
>>> enumere([1, 2, 3])
|
1155 |
-
{1: [0], 2: [1], 3: [2]}
|
1156 |
-
>>> enumere([1, 1, 2, 3, 2, 1])
|
1157 |
-
{1: [0, 1, 5], 2: [2, 4], 3: [3]}
|
1158 |
-
;;;
|
1159 |
-
c = 0
|
1160 |
-
if enumere([])== {}:
|
1161 |
-
print("Test 1 : OK")
|
1162 |
-
c += 1
|
1163 |
-
else :
|
1164 |
-
print("Test 1 : échec")
|
1165 |
-
if enumere([1, 2, 3]) == {1: [0], 2: [1], 3: [2]}:
|
1166 |
-
print("Test 2 : OK")
|
1167 |
-
c += 1
|
1168 |
-
else :
|
1169 |
-
print("Test 2 : échec")
|
1170 |
-
if enumere([1, 1, 2, 3, 2, 1]) == {1: [0, 1, 5], 2: [2, 4], 3: [3]}:
|
1171 |
-
print("Test 3 : OK")
|
1172 |
-
c += 1
|
1173 |
-
else :
|
1174 |
-
print("Test 3 : échec")
|
1175 |
-
if c == 3 :
|
1176 |
-
print("OK")
|
1177 |
-
else :
|
1178 |
-
print("KO")
|
1179 |
-
;;;
|
1180 |
-
max_dico (N3)
|
1181 |
-
;;;
|
1182 |
-
Sur le réseau social TipTop, on s’intéresse au nombre de « like » des abonnés. Les données sont stockées dans des dictionnaires où les clés sont les pseudos et les valeurs correspondantes sont les nombres de « like » comme ci-dessous :
|
1183 |
-
|
1184 |
-
{ 'Bob': 102, 'Ada': 201, 'Alice': 103, 'Tim': 50 }
|
1185 |
-
|
1186 |
-
Écrire une fonction max_dico qui :
|
1187 |
-
• prend en paramètre un dictionnaire dico non vide dont les clés sont des chaînes de caractères et les valeurs associées sont des entiers ;
|
1188 |
-
• et qui renvoie un tuple dont :
|
1189 |
-
– la première valeur est une clé du dictionnaire associée à la valeur maximale ;
|
1190 |
-
– la seconde valeur est cette valeur maximale.
|
1191 |
-
Exemples :
|
1192 |
-
>>> max_dico({ 'Bob': 102, 'Ada': 201, 'Alice': 103, 'Tim': 50 })
|
1193 |
-
('Ada', 201)
|
1194 |
-
>>> max_dico({ 'Alan': 222, 'Ada': 201, 'Eve': 222, 'Tim': 50 })
|
1195 |
-
('Alan', 222) # ou ('Eve', 222) également possible
|
1196 |
-
;;;
|
1197 |
-
c = 0
|
1198 |
-
if max_dico({ 'Bob': 102, 'Ada': 201, 'Alice': 103, 'Tim': 50 }) == ('Ada', 201) :
|
1199 |
-
print("Test 1 : OK")
|
1200 |
-
c += 1
|
1201 |
-
else :
|
1202 |
-
print("Test 1 : échec")
|
1203 |
-
if max_dico({ 'Alan': 222, 'Ada': 201, 'Eve': 222, 'Tim': 50 }) == ('Alan', 222) or ('Eve', 222) :
|
1204 |
-
print("Test 2 : OK")
|
1205 |
-
c += 1
|
1206 |
-
else :
|
1207 |
-
print("Test 2 : échec")
|
1208 |
-
if c == 2 :
|
1209 |
-
print("OK")
|
1210 |
-
else :
|
1211 |
-
print("KO")
|
1212 |
-
;;;
|
1213 |
-
correspond (N3)
|
1214 |
-
;;;
|
1215 |
-
On considère des chaînes de caractères contenant uniquement des majuscules et des caractères * appelées mots à trous.
|
1216 |
-
Par exemple INFO*MA*IQUE, ***I***E** et *S* sont des mots à trous.
|
1217 |
-
Programmer une fonction correspond :
|
1218 |
-
• qui prend en paramètres deux chaînes de caractères mot et mot_a_trous où mot_a_trous est un mot à trous comme indiqué ci-dessus ;
|
1219 |
-
• et qui renvoie :
|
1220 |
-
– True si on peut obtenir mot en remplaçant convenablement les caractères '*' de mot_a_trous ;
|
1221 |
-
– False sinon.
|
1222 |
-
Exemple :
|
1223 |
-
>>> correspond('INFORMATIQUE', 'INFO*MA*IQUE')
|
1224 |
-
True
|
1225 |
-
>>> correspond('AUTOMATIQUE', 'INFO*MA*IQUE')
|
1226 |
-
False
|
1227 |
-
>>> correspond('STOP', 'S*')
|
1228 |
-
False
|
1229 |
-
>>> correspond('AUTO', '*UT*')
|
1230 |
-
True
|
1231 |
-
;;;
|
1232 |
-
c = 0
|
1233 |
-
if correspond('INFORMATIQUE', 'INFO*MA*IQUE') :
|
1234 |
-
print("Test 1 : OK")
|
1235 |
-
c += 1
|
1236 |
-
else :
|
1237 |
-
print("Test 1 : échec")
|
1238 |
-
if not correspond('AUTOMATIQUE', 'INFO*MA*IQUE') :
|
1239 |
-
print("Test 2 : OK")
|
1240 |
-
c += 1
|
1241 |
-
else :
|
1242 |
-
print("Test 2 : échec")
|
1243 |
-
if not correspond('STOP', 'S*') :
|
1244 |
-
print("Test 3 : OK")
|
1245 |
-
c += 1
|
1246 |
-
else :
|
1247 |
-
print("Test 3 : échec")
|
1248 |
-
if correspond('AUTO', '*UT*') :
|
1249 |
-
print("Test 4 : OK")
|
1250 |
-
c += 1
|
1251 |
-
else :
|
1252 |
-
print("Test 4 : échec")
|
1253 |
-
if c == 4 :
|
1254 |
-
print("OK")
|
1255 |
-
else :
|
1256 |
-
print("KO")
|
1257 |
-
;;;
|
1258 |
-
tri insertion (N3)
|
1259 |
-
;;;
|
1260 |
-
Écrire une fonction tri_insertion qui prend en paramètre un tableau tab de nombres entiers (type list) et qui renvoie un tableau trié par ordre croissant. Cette fonction doit utiliser l'algorithme du tri par insertion.
|
1261 |
-
Exemple :
|
1262 |
-
>>> tri_insertion([1, 52, 6, -9, 12])
|
1263 |
-
[-9, 1, 6, 12, 52]
|
1264 |
-
>>> tri_insertion([6])
|
1265 |
-
[6]
|
1266 |
-
;;;
|
1267 |
-
c = 0
|
1268 |
-
if tri_insertion([1, 52, 6, -9, 12]) == [-9, 1, 6, 12, 52]:
|
1269 |
-
print("Test 1 : OK")
|
1270 |
-
c += 1
|
1271 |
-
else :
|
1272 |
-
print("Test 1 : échec")
|
1273 |
-
if tri_insertion([6]) == [6]:
|
1274 |
-
print("Test 2 : OK")
|
1275 |
-
c += 1
|
1276 |
-
else :
|
1277 |
-
print("Test 2 : échec")
|
1278 |
-
if c == 2 :
|
1279 |
-
print("OK")
|
1280 |
-
else :
|
1281 |
-
print("KO")
|
1282 |
-
;;;
|
1283 |
-
insére (N3)
|
1284 |
-
;;;
|
1285 |
-
On considère la fonction insere ci-dessous qui prend en arguments un tableau tab d’entiers triés par ordre croissant et un entier a.
|
1286 |
-
Cette fonction crée et renvoie un nouveau tableau à partir de celui fourni en paramètre en y insérant la valeur a de sorte que le tableau renvoyé soit encore trié par ordre croissant. Les tableaux seront représentés sous la forme de listes Python.
|
1287 |
-
Écrire cette fonction insere
|
1288 |
-
Exemples :
|
1289 |
-
>>> insere([1, 2, 4, 5], 3)
|
1290 |
-
[1, 2, 3, 4, 5]
|
1291 |
-
>>> insere([1, 2, 7, 12, 14, 25], 30)
|
1292 |
-
[1, 2, 7, 12, 14, 25, 30]
|
1293 |
-
>>> insere([2, 3, 4], 1)
|
1294 |
-
[1, 2, 3, 4]
|
1295 |
-
>>> insere([], 1)
|
1296 |
-
[1]
|
1297 |
-
;;;
|
1298 |
-
c = 0
|
1299 |
-
if insere([1, 2, 4, 5], 3) == [1, 2, 3, 4, 5]:
|
1300 |
-
print("Test 1 : OK")
|
1301 |
-
c += 1
|
1302 |
-
else :
|
1303 |
-
print("Test 1 : échec")
|
1304 |
-
if insere([1, 2, 7, 12, 14, 25], 30) == [1, 2, 7, 12, 14, 25, 30]:
|
1305 |
-
print("Test 2 : OK")
|
1306 |
-
c += 1
|
1307 |
-
else :
|
1308 |
-
print("Test 2 : échec")
|
1309 |
-
if insere([2, 3, 4], 1) == [1, 2, 3, 4]:
|
1310 |
-
print("Test 3 : OK")
|
1311 |
-
c += 1
|
1312 |
-
else :
|
1313 |
-
print("Test 3 : échec")
|
1314 |
-
if insere([], 1) == [1] :
|
1315 |
-
print("Test 4 : OK")
|
1316 |
-
c += 1
|
1317 |
-
else :
|
1318 |
-
print("Test 4 : échec")
|
1319 |
-
if c == 4 :
|
1320 |
-
print("OK")
|
1321 |
-
else :
|
1322 |
-
print("KO")
|
1323 |
-
;;;
|
1324 |
-
décimal vers binaire (N3)
|
1325 |
-
;;;
|
1326 |
-
On considère la fonction binaire. Cette fonction prend en paramètre un entier positif a en écriture décimale et renvoie son écriture binaire sous la forme d’une chaine de caractères.
|
1327 |
-
Écrire cette fonction binaire
|
1328 |
-
Exemples :
|
1329 |
-
>>> binaire(83)
|
1330 |
-
'1010011'
|
1331 |
-
>>> binaire(6)
|
1332 |
-
'110'
|
1333 |
-
>>> binaire(127)
|
1334 |
-
'1111111'
|
1335 |
-
>>> binaire(0)
|
1336 |
-
'0'
|
1337 |
-
;;;
|
1338 |
-
c = 0
|
1339 |
-
if binaire(83) == '1010011':
|
1340 |
-
print("Test 1 : OK")
|
1341 |
-
c += 1
|
1342 |
-
else :
|
1343 |
-
print("Test 1 : échec")
|
1344 |
-
if binaire(6) == '110':
|
1345 |
-
print("Test 2 : OK")
|
1346 |
-
c += 1
|
1347 |
-
else :
|
1348 |
-
print("Test 2 : échec")
|
1349 |
-
if binaire(127) == '1111111':
|
1350 |
-
print("Test 3 : OK")
|
1351 |
-
c += 1
|
1352 |
-
else :
|
1353 |
-
print("Test 3 : échec")
|
1354 |
-
if binaire(0) == '0' :
|
1355 |
-
print("Test 4 : OK")
|
1356 |
-
c += 1
|
1357 |
-
else :
|
1358 |
-
print("Test 4 : échec")
|
1359 |
-
if c == 4 :
|
1360 |
-
print("OK")
|
1361 |
-
else :
|
1362 |
-
print("KO")
|
1363 |
-
;;;
|
1364 |
-
recherche dichotomique récursive (N3 T)
|
1365 |
-
;;;
|
1366 |
-
Soit tab un tableau non vide d’entiers triés dans l’ordre croissant et n un entier.
|
1367 |
-
La fonction chercher doit renvoyer l'indice de position de n dans le dernier s'il s'y trouve et None s'il est absent du tableau
|
1368 |
-
La fonction recherche prend n en paramètre
|
1369 |
-
L’algorithme demandé est une recherche dichotomique récursive.
|
1370 |
-
Écrire le code de la fonction chercher.
|
1371 |
-
|
1372 |
-
Exemples :
|
1373 |
-
>>> chercher([1, 5, 6, 9, 12], 7)
|
1374 |
-
>>> chercher([1, 5, 6, 9, 12], 9)
|
1375 |
-
3
|
1376 |
-
>>> chercher([1, 5, 6, 9, 12], 6)
|
1377 |
-
2
|
1378 |
-
;;;
|
1379 |
-
c = 0
|
1380 |
-
if chercher([1, 5, 6, 9, 12], 7) == None:
|
1381 |
-
print("Test 1 : OK")
|
1382 |
-
c += 1
|
1383 |
-
else :
|
1384 |
-
print("Test 1 : échec")
|
1385 |
-
if chercher([1, 5, 6, 9, 12], 9) == 3:
|
1386 |
-
print("Test 2 : OK")
|
1387 |
-
c += 1
|
1388 |
-
else :
|
1389 |
-
print("Test 2 : échec")
|
1390 |
-
if chercher([1, 5, 6, 9, 12], 6) == 2:
|
1391 |
-
print("Test 3 : OK")
|
1392 |
-
c += 1
|
1393 |
-
else :
|
1394 |
-
print("Test 3 : échec")
|
1395 |
-
if c == 3 :
|
1396 |
-
print("OK")
|
1397 |
-
else :
|
1398 |
-
print("KO")
|
1399 |
-
;;;
|
1400 |
-
moyenne dico (N3)
|
1401 |
-
;;;
|
1402 |
-
Une professeure de NSI décide de gérer les résultats de sa classe sous la forme d’un dictionnaire :
|
1403 |
-
• les clefs sont les noms des élèves ;
|
1404 |
-
• les valeurs sont des dictionnaires dont les clefs sont les types d’épreuves sous forme de chaîne de caractères et les valeurs sont les notes obtenues associées à leurs coefficients dans une liste.
|
1405 |
-
Avec :
|
1406 |
-
resultats = {
|
1407 |
-
Dupont': {
|
1408 |
-
'DS1': [15.5, 4],
|
1409 |
-
'DM1': [14.5, 1],
|
1410 |
-
'DS2': [13, 4],
|
1411 |
-
'PROJET1': [16, 3],
|
1412 |
-
'DS3': [14, 4]
|
1413 |
-
},
|
1414 |
-
'Durand': {
|
1415 |
-
'DS1': [6 , 4],
|
1416 |
-
'DS2': [8, 4],
|
1417 |
-
'PROJET1': [9, 3],
|
1418 |
-
'IE1': [7, 2],
|
1419 |
-
'DS3': [12, 4]
|
1420 |
-
}
|
1421 |
-
}
|
1422 |
-
L’élève dont le nom est Durand a ainsi obtenu au DS2 la note de 8 avec un coefficient 4. La professeure crée une fonction moyenne qui prend en paramètre le nom d’un de ses élèves et renvoie sa moyenne arrondie au dixième. Si l’élève n’a pas de notes, on considère que sa moyenne est nulle. Si le nom donné n’est pas dans les résultats, la fonction renvoie None.
|
1423 |
-
Écrire le code de la fonction moyenne.
|
1424 |
-
Exemples :
|
1425 |
-
>>> moyenne("Dupont", resultats)
|
1426 |
-
14.5
|
1427 |
-
>>> moyenne("Durand", resultats)
|
1428 |
-
8.5
|
1429 |
-
;;;
|
1430 |
-
c = 0
|
1431 |
-
resultats = {'Dupont': {'DS1': [15.5, 4],'DM1': [14.5, 1],'DS2': [13, 4],'PROJET1': [16, 3],'DS3': [14, 4]},'Durand': {'DS1': [6 , 4],'DS2': [8, 4],'PROJET1': [9, 3],'IE1': [7, 2],'DS3': [12, 4]}}
|
1432 |
-
if round(moyenne("Dupont", resultats)) == 15:
|
1433 |
-
print("Test 1 : OK")
|
1434 |
-
c += 1
|
1435 |
-
else :
|
1436 |
-
print("Test 1 : échec")
|
1437 |
-
if round(moyenne("Durand", resultats)) == 9:
|
1438 |
-
print("Test 2 : OK")
|
1439 |
-
c += 1
|
1440 |
-
else :
|
1441 |
-
print("Test 2 : échec")
|
1442 |
-
if c == 2 :
|
1443 |
-
print("OK")
|
1444 |
-
else :
|
1445 |
-
print("KO")
|
1446 |
-
;;;
|
1447 |
-
parcours suffixe d'un arbre binaire (N3 T)
|
1448 |
-
;;;
|
1449 |
-
Écrire une fonction parcours_suffixe qui prend en paramètre une instance de la classe arbre T et renvoie un tableau (de type list) contenant les valeurs des noeuds de l'arbre T classés dans un ordre compatible avec un parcours suffixe de l'arbre binaire T.
|
1450 |
-
La classe Arbre possède 3 méthodes :
|
1451 |
-
- get_gauche() renvoie l'arbre gauche
|
1452 |
-
- get_droit() renvoie l'arbre droit
|
1453 |
-
- get_valeur() renvoie la valeur du noeud
|
1454 |
-
;;;
|
1455 |
-
class ArbreBinaire:
|
1456 |
-
def __init__(self, valeur):
|
1457 |
-
self.valeur = valeur
|
1458 |
-
self.enfant_gauche = None
|
1459 |
-
self.enfant_droit = None
|
1460 |
-
def insert_gauche(self, valeur):
|
1461 |
-
if self.enfant_gauche == None:
|
1462 |
-
self.enfant_gauche = ArbreBinaire(valeur)
|
1463 |
-
else:
|
1464 |
-
new_node = ArbreBinaire(valeur)
|
1465 |
-
new_node.enfant_gauche = self.enfant_gauche
|
1466 |
-
self.enfant_gauche = new_node
|
1467 |
-
def insert_droit(self, valeur):
|
1468 |
-
if self.enfant_droit == None:
|
1469 |
-
self.enfant_droit = ArbreBinaire(valeur)
|
1470 |
-
else:
|
1471 |
-
new_node = ArbreBinaire(valeur)
|
1472 |
-
new_node.enfant_droit = self.enfant_droit
|
1473 |
-
self.enfant_droit = new_node
|
1474 |
-
def get_valeur(self):
|
1475 |
-
return self.valeur
|
1476 |
-
def get_gauche(self):
|
1477 |
-
return self.enfant_gauche
|
1478 |
-
def get_droit(self):
|
1479 |
-
return self.enfant_droit
|
1480 |
-
racine = ArbreBinaire('A')
|
1481 |
-
racine.insert_gauche('B')
|
1482 |
-
racine.insert_droit('F')
|
1483 |
-
|
1484 |
-
b_node = racine.get_gauche()
|
1485 |
-
b_node.insert_gauche('C')
|
1486 |
-
b_node.insert_droit('D')
|
1487 |
-
|
1488 |
-
f_node = racine.get_droit()
|
1489 |
-
f_node.insert_gauche('G')
|
1490 |
-
f_node.insert_droit('H')
|
1491 |
-
|
1492 |
-
c_node = b_node.get_gauche()
|
1493 |
-
c_node.insert_droit('E')
|
1494 |
-
|
1495 |
-
g_node = f_node.get_gauche()
|
1496 |
-
g_node.insert_gauche('I')
|
1497 |
-
|
1498 |
-
h_node = f_node.get_droit()
|
1499 |
-
h_node.insert_droit('J')
|
1500 |
-
c = 0
|
1501 |
-
if parcours_suffixe(racine) == ['E', 'C', 'D', 'B', 'I', 'G', 'J', 'H', 'F', 'A']:
|
1502 |
-
print("Test 1 : OK")
|
1503 |
-
c += 1
|
1504 |
-
else :
|
1505 |
-
print("Test 1 : échec")
|
1506 |
-
if c == 1 :
|
1507 |
-
print("OK")
|
1508 |
-
else :
|
1509 |
-
print("KO")
|
1510 |
-
;;;
|
1511 |
-
nombre de boites (N4)
|
1512 |
-
;;;
|
1513 |
-
On dispose d’un ensemble d’objets dont on connaît, pour chacun, la masse. On souhaite ranger l’ensemble de ces objets dans des boites identiques de telle manière que la somme des masses des objets contenus dans une boîte ne dépasse pas la capacité c de la boîte. On souhaite utiliser le moins de boîtes possibles pour ranger cet ensemble d’objets.
|
1514 |
-
Pour résoudre ce problème, on utilisera un algorithme glouton consistant à placer chacun des objets dans la première boîte où cela est possible.
|
1515 |
-
Par exemple, pour ranger dans des boîtes de capacité c = 5 un ensemble de trois objets dont les masses sont représentées en Python par la liste [1, 5, 2], on procède de la façon suivante :
|
1516 |
-
• Le premier objet, de masse 1, va dans une première boite.
|
1517 |
-
• Le deuxième objet, de masse 5, ne peut pas aller dans la même boite que le premier objet car cela dépasserait la capacité de la boite. On place donc cet objet dans une deuxième boîte.
|
1518 |
-
• Le troisième objet, de masse 2, va dans la première boîte.
|
1519 |
-
On a donc utilisé deux boîtes de capacité c = 5 pour ranger les 3 objets.
|
1520 |
-
Écrire la fonction Python empaqueter qui prend en paramètre la liste des masses liste_masses et la capacité de la boite c. La fonction empaqueter doit renvoyer le nombre de boîtes de capacité c nécessaires pour empaqueter un ensemble d’objets dont les masses sont contenues dans la liste liste_masses. On supposera que toutes les masses sont inférieures ou égales à c.
|
1521 |
-
Exemples :
|
1522 |
-
>>> empaqueter([1, 2, 3, 4, 5], 10)
|
1523 |
-
2
|
1524 |
-
>>> empaqueter([1, 2, 3, 4, 5], 5)
|
1525 |
-
4
|
1526 |
-
>>> empaqueter([7, 6, 3, 4, 8, 5, 9, 2], 11)
|
1527 |
-
5
|
1528 |
-
;;;
|
1529 |
-
c = 0
|
1530 |
-
if empaqueter([1, 2, 3, 4, 5], 10) == 2:
|
1531 |
-
print("Test 1 : OK")
|
1532 |
-
c += 1
|
1533 |
-
else :
|
1534 |
-
print("Test 1 : échec")
|
1535 |
-
if empaqueter([1, 2, 3, 4, 5], 5) == 4:
|
1536 |
-
print("Test 2 : OK")
|
1537 |
-
c += 1
|
1538 |
-
else :
|
1539 |
-
print("Test 2 : échec")
|
1540 |
-
if empaqueter([7, 6, 3, 4, 8, 5, 9, 2], 11) == 5:
|
1541 |
-
print("Test 3 : OK")
|
1542 |
-
c += 1
|
1543 |
-
else :
|
1544 |
-
print("Test 3 : échec")
|
1545 |
-
if c == 3 :
|
1546 |
-
print("OK")
|
1547 |
-
else :
|
1548 |
-
print("KO")
|
1549 |
-
;;;
|
1550 |
-
un sur deux (N2)
|
1551 |
-
;;;
|
1552 |
-
Écrire une fonction un_sur_deux qui prend en paramètre un tableau non vide tab contenant des entiers. Cette fonction renvoie un autre tableau contenant les entiers contenu dans tab, mais en gardant uniquement un entier sur deux.
|
1553 |
-
Exemples :
|
1554 |
-
>>> un_sur_deux([3, 4, 12, 8, 43,7])
|
1555 |
-
[3, 12, 43]
|
1556 |
-
>>> un_sur_deux([54, 5, 7, 14, 26, 54, 32])
|
1557 |
-
[54, 7, 26,32]
|
1558 |
-
;;;
|
1559 |
-
c = 0
|
1560 |
-
if un_sur_deux([3, 4, 12, 8, 43,7]) == [3, 12, 43]:
|
1561 |
-
print("Test 1 : OK")
|
1562 |
-
c += 1
|
1563 |
-
else :
|
1564 |
-
print("Test 1 : échec")
|
1565 |
-
if un_sur_deux([54, 5, 7, 14, 26, 54, 32]) == [54, 7, 26,32]:
|
1566 |
-
print("Test 2 : OK")
|
1567 |
-
c += 1
|
1568 |
-
else :
|
1569 |
-
print("Test 2 : échec")
|
1570 |
-
if c == 2 :
|
1571 |
-
print("OK")
|
1572 |
-
else :
|
1573 |
-
print("KO")
|
1574 |
-
;;;
|
1575 |
-
plus grande somme (N4 T)
|
1576 |
-
;;;
|
1577 |
-
On considère un tableau non vide de nombres entiers, positifs ou négatifs, et on souhaite déterminer la plus grande somme possible de ses éléments consécutifs.
|
1578 |
-
Par exemple, dans le tableau [1, -2, 3, 10, -4, 7, 2, -5], la plus grande somme est 18 obtenue en additionnant les éléments 3, 10, -4, 7, 2.
|
1579 |
-
Pour cela, on va résoudre le problème par programmation dynamique. Si on note tab le tableau considéré et i un indice dans ce tableau, on se ramène à un problème plus simple : déterminer la plus grande somme possible de ses éléments consécutifs se terminant à l’indice i.
|
1580 |
-
Si on connait la plus grande somme possible de ses éléments consécutifs se terminant à l’indice i-1, on peut déterminer la plus grande somme possible de ses éléments consécutifs se terminant à l’indice i :
|
1581 |
-
• soit on obtient une plus grande somme en ajoutant tab[i] à cette somme précédente ;
|
1582 |
-
• soit on commence une nouvelle somme à partir de tab[i].
|
1583 |
-
Remarque : les sommes considérées contiennent toujours au moins un terme.
|
1584 |
-
Écrire la fonction somme_max qui prend en paramètre un tableau (de type list) et qui renvoie la plus grande somme possible des éléments consécutifs.
|
1585 |
-
Exemples :
|
1586 |
-
>>> somme_max([1, 2, 3, 4, 5])
|
1587 |
-
15
|
1588 |
-
>>> somme_max([1, 2, -3, 4, 5])
|
1589 |
-
9
|
1590 |
-
>>> somme_max([1, 2, -2, 4, 5])
|
1591 |
-
10
|
1592 |
-
>>> somme_max([1, -2, 3, 10, -4, 7, 2, -5])
|
1593 |
-
18
|
1594 |
-
>>> somme_max([-3,-2,-1,-4])
|
1595 |
-
-1
|
1596 |
-
;;;
|
1597 |
-
c = 0
|
1598 |
-
if somme_max([1, 2, 3, 4, 5]) == 15:
|
1599 |
-
print("Test 1 : OK")
|
1600 |
-
c += 1
|
1601 |
-
else :
|
1602 |
-
print("Test 1 : échec")
|
1603 |
-
if somme_max([1, 2, -3, 4, 5]) == 9:
|
1604 |
-
print("Test 2 : OK")
|
1605 |
-
c += 1
|
1606 |
-
else :
|
1607 |
-
print("Test 2 : échec")
|
1608 |
-
if somme_max([1, 2, -2, 4, 5]) == 10:
|
1609 |
-
print("Test 3 : OK")
|
1610 |
-
c += 1
|
1611 |
-
else :
|
1612 |
-
print("Test 3 : échec")
|
1613 |
-
if somme_max([1, -2, 3, 10, -4, 7, 2, -5]) == 18 :
|
1614 |
-
print("Test 4 : OK")
|
1615 |
-
c += 1
|
1616 |
-
else :
|
1617 |
-
print("Test 4 : échec")
|
1618 |
-
if somme_max([-3,-2,-1,-4]) == -1 :
|
1619 |
-
print("Test 5 : OK")
|
1620 |
-
c += 1
|
1621 |
-
else :
|
1622 |
-
print("Test 5 : échec")
|
1623 |
-
if c == 5 :
|
1624 |
-
print("OK")
|
1625 |
-
else :
|
1626 |
-
print("KO")
|
1627 |
-
;;;
|
1628 |
-
taille d'un arbre binaire (N2 T)
|
1629 |
-
;;;
|
1630 |
-
Écrire une fonction taille qui prend en paramètre une instance de la classe arbre T et renvoie la taille de l'arbre binaire T.
|
1631 |
-
La classe Arbre possède 3 méthodes :
|
1632 |
-
- get_gauche() renvoie l'arbre gauche
|
1633 |
-
- get_droit() renvoie l'arbre droit
|
1634 |
-
- get_valeur() renvoie la valeur du noeud
|
1635 |
-
;;;
|
1636 |
-
class ArbreBinaire:
|
1637 |
-
def __init__(self, valeur):
|
1638 |
-
self.valeur = valeur
|
1639 |
-
self.enfant_gauche = None
|
1640 |
-
self.enfant_droit = None
|
1641 |
-
def insert_gauche(self, valeur):
|
1642 |
-
if self.enfant_gauche == None:
|
1643 |
-
self.enfant_gauche = ArbreBinaire(valeur)
|
1644 |
-
else:
|
1645 |
-
new_node = ArbreBinaire(valeur)
|
1646 |
-
new_node.enfant_gauche = self.enfant_gauche
|
1647 |
-
self.enfant_gauche = new_node
|
1648 |
-
def insert_droit(self, valeur):
|
1649 |
-
if self.enfant_droit == None:
|
1650 |
-
self.enfant_droit = ArbreBinaire(valeur)
|
1651 |
-
else:
|
1652 |
-
new_node = ArbreBinaire(valeur)
|
1653 |
-
new_node.enfant_droit = self.enfant_droit
|
1654 |
-
self.enfant_droit = new_node
|
1655 |
-
def get_valeur(self):
|
1656 |
-
return self.valeur
|
1657 |
-
def get_gauche(self):
|
1658 |
-
return self.enfant_gauche
|
1659 |
-
def get_droit(self):
|
1660 |
-
return self.enfant_droit
|
1661 |
-
racine = ArbreBinaire('A')
|
1662 |
-
racine.insert_gauche('B')
|
1663 |
-
racine.insert_droit('F')
|
1664 |
-
|
1665 |
-
b_node = racine.get_gauche()
|
1666 |
-
b_node.insert_gauche('C')
|
1667 |
-
b_node.insert_droit('D')
|
1668 |
-
|
1669 |
-
f_node = racine.get_droit()
|
1670 |
-
f_node.insert_gauche('G')
|
1671 |
-
f_node.insert_droit('H')
|
1672 |
-
|
1673 |
-
c_node = b_node.get_gauche()
|
1674 |
-
c_node.insert_droit('E')
|
1675 |
-
|
1676 |
-
g_node = f_node.get_gauche()
|
1677 |
-
g_node.insert_gauche('I')
|
1678 |
-
|
1679 |
-
h_node = f_node.get_droit()
|
1680 |
-
h_node.insert_droit('J')
|
1681 |
-
c = 0
|
1682 |
-
if taille(racine) == 10:
|
1683 |
-
print("Test 1 : OK")
|
1684 |
-
c += 1
|
1685 |
-
else :
|
1686 |
-
print("Test 1 : échec")
|
1687 |
-
if c == 1 :
|
1688 |
-
print("OK")
|
1689 |
-
else :
|
1690 |
-
print("KO")
|
1691 |
-
;;;
|
1692 |
-
sépare 0 et 1 (N3)
|
1693 |
-
;;;
|
1694 |
-
Écrire la fonction separe qui prend en paramètre un tableau tab dont les éléments sont des 0 et des 1. Cette fonction renvoie un tableau qui contient autant de 0 et de 1 qu'il y en a dans tab mais où tous les 0 sont placés en début de tableau et tous les 1 sont placés en fin de tableau.
|
1695 |
-
Exemples :
|
1696 |
-
>>> separe([1, 0, 1, 0, 1, 0, 1, 0])
|
1697 |
-
[0, 0, 0, 0, 1, 1, 1, 1]
|
1698 |
-
>>> separe([1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0])
|
1699 |
-
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
|
1700 |
-
;;;
|
1701 |
-
c = 0
|
1702 |
-
if separe([1, 0, 1, 0, 1, 0, 1, 0]) == [0, 0, 0, 0, 1, 1, 1, 1]:
|
1703 |
-
print("Test 1 : OK")
|
1704 |
-
c += 1
|
1705 |
-
else :
|
1706 |
-
print("Test 1 : échec")
|
1707 |
-
if separe([1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0]) == [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]:
|
1708 |
-
print("Test 2 : OK")
|
1709 |
-
c += 1
|
1710 |
-
else :
|
1711 |
-
print("Test 2 : échec")
|
1712 |
-
if c == 2 :
|
1713 |
-
print("OK")
|
1714 |
-
else :
|
1715 |
-
print("KO")
|
1716 |
-
;;;
|
1717 |
-
élève du mois (N3)
|
1718 |
-
;;;
|
1719 |
-
On considère la fonction eleves_du_mois prenant en paramètres eleves et notes deux tableaux de même longueur, le premier contenant le nom des élèves et le second, des entiers positifs désignant leur note à un contrôle de sorte que eleves[i] a obtenu la note notes[i].
|
1720 |
-
Cette fonction renvoie le couple constitué de la note maximale attribuée et des noms des élèves ayant obtenu cette note regroupés dans un tableau.
|
1721 |
-
Écrire la fonction eleves_du_mois.
|
1722 |
-
Exemples :
|
1723 |
-
>>> eleves_nsi = ['a','b','c','d','e','f','g','h','i','j']
|
1724 |
-
>>> notes_nsi = [30, 40, 80, 60, 58, 80, 75, 80, 60, 24]
|
1725 |
-
>>> eleves_du_mois(eleves_nsi, notes_nsi)
|
1726 |
-
(80, ['c', 'f', 'h'])
|
1727 |
-
>>> eleves_du_mois([],[])
|
1728 |
-
(0, [])
|
1729 |
-
;;;
|
1730 |
-
c = 0
|
1731 |
-
eleves_nsi = ['a','b','c','d','e','f','g','h','i','j']
|
1732 |
-
notes_nsi = [30, 40, 80, 60, 58, 80, 75, 80, 60, 24]
|
1733 |
-
if eleves_du_mois(eleves_nsi, notes_nsi) == (80, ['c', 'f', 'h']):
|
1734 |
-
print("Test 1 : OK")
|
1735 |
-
c += 1
|
1736 |
-
else :
|
1737 |
-
print("Test 1 : échec")
|
1738 |
-
if eleves_du_mois([],[]) == (0, []):
|
1739 |
-
print("Test 2 : OK")
|
1740 |
-
c += 1
|
1741 |
-
else :
|
1742 |
-
print("Test 2 : échec")
|
1743 |
-
if c == 2 :
|
1744 |
-
print("OK")
|
1745 |
-
else :
|
1746 |
-
print("KO")
|
1747 |
-
;;;
|
1748 |
-
parcours préfixe d'un arbre binaire (N3 T)
|
1749 |
-
;;;
|
1750 |
-
Écrire une fonction parcours_prefixe qui prend en paramètre une instance de la classe arbre T et renvoie un tableau (de type list) contenant les valeurs des noeuds de l'arbre T classés dans un ordre compatible avec un parcours préfixe de l'arbre binaire T.
|
1751 |
-
La classe Arbre possède 3 méthodes :
|
1752 |
-
- get_gauche() renvoie l'arbre gauche
|
1753 |
-
- get_droit() renvoie l'arbre droit
|
1754 |
-
- get_valeur() renvoie la valeur du noeud
|
1755 |
-
;;;
|
1756 |
-
class ArbreBinaire:
|
1757 |
-
def __init__(self, valeur):
|
1758 |
-
self.valeur = valeur
|
1759 |
-
self.enfant_gauche = None
|
1760 |
-
self.enfant_droit = None
|
1761 |
-
def insert_gauche(self, valeur):
|
1762 |
-
if self.enfant_gauche == None:
|
1763 |
-
self.enfant_gauche = ArbreBinaire(valeur)
|
1764 |
-
else:
|
1765 |
-
new_node = ArbreBinaire(valeur)
|
1766 |
-
new_node.enfant_gauche = self.enfant_gauche
|
1767 |
-
self.enfant_gauche = new_node
|
1768 |
-
def insert_droit(self, valeur):
|
1769 |
-
if self.enfant_droit == None:
|
1770 |
-
self.enfant_droit = ArbreBinaire(valeur)
|
1771 |
-
else:
|
1772 |
-
new_node = ArbreBinaire(valeur)
|
1773 |
-
new_node.enfant_droit = self.enfant_droit
|
1774 |
-
self.enfant_droit = new_node
|
1775 |
-
def get_valeur(self):
|
1776 |
-
return self.valeur
|
1777 |
-
def get_gauche(self):
|
1778 |
-
return self.enfant_gauche
|
1779 |
-
def get_droit(self):
|
1780 |
-
return self.enfant_droit
|
1781 |
-
racine = ArbreBinaire('A')
|
1782 |
-
racine.insert_gauche('B')
|
1783 |
-
racine.insert_droit('F')
|
1784 |
-
|
1785 |
-
b_node = racine.get_gauche()
|
1786 |
-
b_node.insert_gauche('C')
|
1787 |
-
b_node.insert_droit('D')
|
1788 |
-
|
1789 |
-
f_node = racine.get_droit()
|
1790 |
-
f_node.insert_gauche('G')
|
1791 |
-
f_node.insert_droit('H')
|
1792 |
-
|
1793 |
-
c_node = b_node.get_gauche()
|
1794 |
-
c_node.insert_droit('E')
|
1795 |
-
|
1796 |
-
g_node = f_node.get_gauche()
|
1797 |
-
g_node.insert_gauche('I')
|
1798 |
-
|
1799 |
-
h_node = f_node.get_droit()
|
1800 |
-
h_node.insert_droit('J')
|
1801 |
-
c = 0
|
1802 |
-
if parcours_prefixe(racine) == ['A', 'B', 'C', 'E', 'D', 'F', 'G', 'I', 'H', 'J']:
|
1803 |
-
print("Test 1 : OK")
|
1804 |
-
c += 1
|
1805 |
-
else :
|
1806 |
-
print("Test 1 : échec")
|
1807 |
-
if c == 1 :
|
1808 |
-
print("OK")
|
1809 |
-
else :
|
1810 |
-
print("KO")
|
1811 |
-
;;;
|
1812 |
-
recherche dichotomique (N2)
|
1813 |
-
;;;
|
1814 |
-
Écrire une fonction dichotomie qui prend en paramètre un tableau tab d'entiers triés et un entier n. Cette fonction renvoie True si n est présent dans le tableau et False dans le cas contraire.
|
1815 |
-
Attention : vous devez utiliser l'algorithme de la recherche dichotomique
|
1816 |
-
Exemples :
|
1817 |
-
>>> dichotomie([15, 16, 18, 19, 23, 24, 28, 29, 31, 33],28)
|
1818 |
-
True
|
1819 |
-
>>> dichotomie([15, 16, 18, 19, 23, 24, 28, 29, 31, 33],27)
|
1820 |
-
False
|
1821 |
-
;;;
|
1822 |
-
c = 0
|
1823 |
-
if dichotomie([15, 16, 18, 19, 23, 24, 28, 29, 31, 33],28):
|
1824 |
-
print("Test 1 : OK")
|
1825 |
-
c += 1
|
1826 |
-
else :
|
1827 |
-
print("Test 1 : échec")
|
1828 |
-
if not dichotomie([15, 16, 18, 19, 23, 24, 28, 29, 31, 33],27):
|
1829 |
-
print("Test 2 : OK")
|
1830 |
-
c += 1
|
1831 |
-
else :
|
1832 |
-
print("Test 2 : échec")
|
1833 |
-
if c == 2 :
|
1834 |
-
print("OK")
|
1835 |
-
else :
|
1836 |
-
print("KO")
|
1837 |
-
;;;
|
1838 |
-
parcours infixe d'un arbre binaire (N3 T)
|
1839 |
-
;;;
|
1840 |
-
Écrire une fonction parcours_infixe qui prend en paramètre une instance de la classe arbre T et renvoie un tableau (de type list) contenant les valeurs des noeuds de l'arbre T classés dans un ordre compatible avec un parcours infixe de l'arbre binaire T.
|
1841 |
-
La classe Arbre possède 3 méthodes :
|
1842 |
-
- get_gauche() renvoie l'arbre gauche
|
1843 |
-
- get_droit() renvoie l'arbre droit
|
1844 |
-
- get_valeur() renvoie la valeur du noeud
|
1845 |
-
;;;
|
1846 |
-
class ArbreBinaire:
|
1847 |
-
def __init__(self, valeur):
|
1848 |
-
self.valeur = valeur
|
1849 |
-
self.enfant_gauche = None
|
1850 |
-
self.enfant_droit = None
|
1851 |
-
def insert_gauche(self, valeur):
|
1852 |
-
if self.enfant_gauche == None:
|
1853 |
-
self.enfant_gauche = ArbreBinaire(valeur)
|
1854 |
-
else:
|
1855 |
-
new_node = ArbreBinaire(valeur)
|
1856 |
-
new_node.enfant_gauche = self.enfant_gauche
|
1857 |
-
self.enfant_gauche = new_node
|
1858 |
-
def insert_droit(self, valeur):
|
1859 |
-
if self.enfant_droit == None:
|
1860 |
-
self.enfant_droit = ArbreBinaire(valeur)
|
1861 |
-
else:
|
1862 |
-
new_node = ArbreBinaire(valeur)
|
1863 |
-
new_node.enfant_droit = self.enfant_droit
|
1864 |
-
self.enfant_droit = new_node
|
1865 |
-
def get_valeur(self):
|
1866 |
-
return self.valeur
|
1867 |
-
def get_gauche(self):
|
1868 |
-
return self.enfant_gauche
|
1869 |
-
def get_droit(self):
|
1870 |
-
return self.enfant_droit
|
1871 |
-
racine = ArbreBinaire('A')
|
1872 |
-
racine.insert_gauche('B')
|
1873 |
-
racine.insert_droit('F')
|
1874 |
-
|
1875 |
-
b_node = racine.get_gauche()
|
1876 |
-
b_node.insert_gauche('C')
|
1877 |
-
b_node.insert_droit('D')
|
1878 |
-
|
1879 |
-
f_node = racine.get_droit()
|
1880 |
-
f_node.insert_gauche('G')
|
1881 |
-
f_node.insert_droit('H')
|
1882 |
-
|
1883 |
-
c_node = b_node.get_gauche()
|
1884 |
-
c_node.insert_droit('E')
|
1885 |
-
|
1886 |
-
g_node = f_node.get_gauche()
|
1887 |
-
g_node.insert_gauche('I')
|
1888 |
-
|
1889 |
-
h_node = f_node.get_droit()
|
1890 |
-
h_node.insert_droit('J')
|
1891 |
-
c = 0
|
1892 |
-
if parcours_infixe(racine) == ['C', 'E', 'B', 'D', 'A', 'I', 'G', 'F', 'H', 'J']:
|
1893 |
-
print("Test 1 : OK")
|
1894 |
-
c += 1
|
1895 |
-
else :
|
1896 |
-
print("Test 1 : échec")
|
1897 |
-
if c == 1 :
|
1898 |
-
print("OK")
|
1899 |
-
else :
|
1900 |
-
print("KO")
|
1901 |
-
;;;
|
1902 |
-
palindrome (N3)
|
1903 |
-
;;;
|
1904 |
-
Un mot palindrome peut se lire de la même façon de gauche à droite ou de droite à gauche : kayak, radar, et non sont des mots palindromes.
|
1905 |
-
Écrire une fonction est_palidrome qui prend en paramètre une chaine de caractère mot. Cette fonction renvoie True si mot est un palindrome et False dans le cas contraire.
|
1906 |
-
Exemples :
|
1907 |
-
>>> est_palindrome('kayak')
|
1908 |
-
True
|
1909 |
-
>>> est_palindrome('NSI')
|
1910 |
-
False
|
1911 |
-
;;;
|
1912 |
-
c = 0
|
1913 |
-
if est_palindrome('kayak'):
|
1914 |
-
print("Test 1 : OK")
|
1915 |
-
c += 1
|
1916 |
-
else :
|
1917 |
-
print("Test 1 : échec")
|
1918 |
-
if not est_palindrome('NSI'):
|
1919 |
-
print("Test 2 : OK")
|
1920 |
-
c += 1
|
1921 |
-
else :
|
1922 |
-
print("Test 2 : échec")
|
1923 |
-
if c == 2 :
|
1924 |
-
print("OK")
|
1925 |
-
else :
|
1926 |
-
print("KO")
|
1927 |
-
;;;
|
1928 |
-
hauteur d'un arbre binaire (N2 T)
|
1929 |
-
;;;
|
1930 |
-
Écrire une fonction hauteur qui prend en paramètre une instance de la classe arbre T et renvoie la hauteur de l'arbre binaire T.
|
1931 |
-
La classe Arbre possède 3 méthodes :
|
1932 |
-
- get_gauche() renvoie l'arbre gauche
|
1933 |
-
- get_droit() renvoie l'arbre droit
|
1934 |
-
- get_valeur() renvoie la valeur du noeud
|
1935 |
-
;;;
|
1936 |
-
class ArbreBinaire:
|
1937 |
-
def __init__(self, valeur):
|
1938 |
-
self.valeur = valeur
|
1939 |
-
self.enfant_gauche = None
|
1940 |
-
self.enfant_droit = None
|
1941 |
-
def insert_gauche(self, valeur):
|
1942 |
-
if self.enfant_gauche == None:
|
1943 |
-
self.enfant_gauche = ArbreBinaire(valeur)
|
1944 |
-
else:
|
1945 |
-
new_node = ArbreBinaire(valeur)
|
1946 |
-
new_node.enfant_gauche = self.enfant_gauche
|
1947 |
-
self.enfant_gauche = new_node
|
1948 |
-
def insert_droit(self, valeur):
|
1949 |
-
if self.enfant_droit == None:
|
1950 |
-
self.enfant_droit = ArbreBinaire(valeur)
|
1951 |
-
else:
|
1952 |
-
new_node = ArbreBinaire(valeur)
|
1953 |
-
new_node.enfant_droit = self.enfant_droit
|
1954 |
-
self.enfant_droit = new_node
|
1955 |
-
def get_valeur(self):
|
1956 |
-
return self.valeur
|
1957 |
-
def get_gauche(self):
|
1958 |
-
return self.enfant_gauche
|
1959 |
-
def get_droit(self):
|
1960 |
-
return self.enfant_droit
|
1961 |
-
racine = ArbreBinaire('A')
|
1962 |
-
racine.insert_gauche('B')
|
1963 |
-
racine.insert_droit('F')
|
1964 |
-
|
1965 |
-
b_node = racine.get_gauche()
|
1966 |
-
b_node.insert_gauche('C')
|
1967 |
-
b_node.insert_droit('D')
|
1968 |
-
|
1969 |
-
f_node = racine.get_droit()
|
1970 |
-
f_node.insert_gauche('G')
|
1971 |
-
f_node.insert_droit('H')
|
1972 |
-
|
1973 |
-
c_node = b_node.get_gauche()
|
1974 |
-
c_node.insert_droit('E')
|
1975 |
-
|
1976 |
-
g_node = f_node.get_gauche()
|
1977 |
-
g_node.insert_gauche('I')
|
1978 |
-
|
1979 |
-
h_node = f_node.get_droit()
|
1980 |
-
h_node.insert_droit('J')
|
1981 |
-
c = 0
|
1982 |
-
if hauteur(racine) == 4:
|
1983 |
-
print("Test 1 : OK")
|
1984 |
-
c += 1
|
1985 |
-
else :
|
1986 |
-
print("Test 1 : échec")
|
1987 |
-
if c == 1 :
|
1988 |
-
print("OK")
|
1989 |
-
else :
|
1990 |
-
print("KO")
|
1991 |
-
;;;
|
1992 |
-
dico vers tableau (N2)
|
1993 |
-
;;;
|
1994 |
-
Écrire une fonction dico_tab qui prend en paramètre un dictionnaire d. Cette fonction renvoie un tableau contenant des tuples, chaque tuple contient en première position une clé du dictionnaire d et en deuxième position la valeur associée à cette clé.
|
1995 |
-
Exemples :
|
1996 |
-
>>> dico_tab({"pommes" : 3, "cerises" : 15, "fraises" : 7})
|
1997 |
-
[("pommes", 3), ("cerises", 15), ("fraises", 7)]
|
1998 |
-
>>> dico_tab({"pêches" : 2})
|
1999 |
-
[("pêches", 2)]
|
2000 |
-
>>> dico_tab({})
|
2001 |
-
[]
|
2002 |
-
;;;
|
2003 |
-
c = 0
|
2004 |
-
if dico_tab({"pommes" : 3, "cerises" : 15, "fraises" : 7}) == [("pommes", 3), ("cerises", 15), ("fraises", 7)]:
|
2005 |
-
print("Test 1 : OK")
|
2006 |
-
c += 1
|
2007 |
-
else :
|
2008 |
-
print("Test 1 : échec")
|
2009 |
-
if dico_tab({"pêches" : 2}) == [("pêches", 2)]:
|
2010 |
-
print("Test 2 : OK")
|
2011 |
-
c += 1
|
2012 |
-
else :
|
2013 |
-
print("Test 2 : échec")
|
2014 |
-
if dico_tab({}) == []:
|
2015 |
-
print("Test 3 : OK")
|
2016 |
-
c += 1
|
2017 |
-
else :
|
2018 |
-
print("Test 3 : échec")
|
2019 |
-
if c == 3 :
|
2020 |
-
print("OK")
|
2021 |
-
else :
|
2022 |
-
print("KO")
|
2023 |
-
;;;
|
2024 |
-
prix article (N2)
|
2025 |
-
;;;
|
2026 |
-
On considère le stock d'un magasin. Ce stock est géré à l'aide d'un tableau contenant des dictionnaires. Chaque dictionnaire est composé de 3 clés : nom, prix et quantité
|
2027 |
-
Exemple :
|
2028 |
-
[{"nom": "T-shirt", "prix": 15, "quantité": 10}, {"nom": "Pantalon", "prix": 30, "quantité": 5}, {"nom": "Chaussures", "prix": 60, "quantité": 2}]
|
2029 |
-
|
2030 |
-
Écrire une fonction prix_article qui prend en paramètres inventaire (le tableau de dictionnaires représentant l'inventaire) et nom_article (le nom de l'article recherché). La fonction doit renvoyer le prix de l'article si celui-ci est présent dans l'inventaire, et -1 sinon.
|
2031 |
-
Exemples :
|
2032 |
-
>>> inv = [{"nom": "T-shirt", "prix": 15, "quantité": 10}, {"nom": "Pantalon", "prix": 30, "quantité": 5}, {"nom": "Chaussures", "prix": 60, "quantité": 2}]
|
2033 |
-
>>> prix_article(inv, "Pantalon")
|
2034 |
-
30
|
2035 |
-
>>> prix_article(inv, "Pull")
|
2036 |
-
-1
|
2037 |
-
;;;
|
2038 |
-
c = 0
|
2039 |
-
inv = [{"nom": "T-shirt", "prix": 15, "quantité": 10}, {"nom": "Pantalon", "prix": 30, "quantité": 5}, {"nom": "Chaussures", "prix": 60, "quantité": 2}]
|
2040 |
-
if prix_article(inv, "Pantalon") == 30:
|
2041 |
-
print("Test 1 : OK")
|
2042 |
-
c += 1
|
2043 |
-
else :
|
2044 |
-
print("Test 1 : échec")
|
2045 |
-
if prix_article(inv, "Pull") == -1 :
|
2046 |
-
print("Test 2 : OK")
|
2047 |
-
c += 1
|
2048 |
-
else :
|
2049 |
-
print("Test 2 : échec")
|
2050 |
-
if c == 2 :
|
2051 |
-
print("OK")
|
2052 |
-
else :
|
2053 |
-
print("KO")
|
2054 |
-
;;;
|
2055 |
-
info météo (N2)
|
2056 |
-
;;;
|
2057 |
-
On considère un relevé météo implémenté à l'aide de dictionnaires. On trouve dans ce relevé les informations suivantes pour chaque ville :
|
2058 |
-
- la température maxi
|
2059 |
-
- la température mini
|
2060 |
-
- la pluviométrie
|
2061 |
-
|
2062 |
-
Exemple :
|
2063 |
-
|
2064 |
-
donnees_meteo = {
|
2065 |
-
"Paris": {"max": 25, "min": 15, "precip": 5},
|
2066 |
-
"Lyon": {"max": 28, "min": 18, "precip": 2},
|
2067 |
-
"Marseille": {"max": 30, "min": 20, "precip": 0},
|
2068 |
-
"Toulouse": {"max": 27, "min": 17, "precip": 10}
|
2069 |
-
}
|
2070 |
-
|
2071 |
-
Écrire une fonction info_meteo qui prend en paramètre un relevé météo et renvoie un tuple contenant :
|
2072 |
-
- le nom de la ville où la plus petite température a été relevée
|
2073 |
-
- le nom de la ville où la plus haute température a été relevée
|
2074 |
-
- la somme de toutes la pluviométrie.
|
2075 |
-
|
2076 |
-
Exemple :
|
2077 |
-
|
2078 |
-
>>> donnees_meteo = {
|
2079 |
-
"Paris": {"max": 25, "min": 15, "precip": 5},
|
2080 |
-
"Lyon": {"max": 28, "min": 18, "precip": 2},
|
2081 |
-
"Marseille": {"max": 30, "min": 20, "precip": 0},
|
2082 |
-
"Toulouse": {"max": 27, "min": 17, "precip": 10}
|
2083 |
-
}
|
2084 |
-
>>> info_meteo(donnees_meteo)
|
2085 |
-
("Paris", "Marseille", 17)
|
2086 |
-
;;;
|
2087 |
-
c = 0
|
2088 |
-
donnees_meteo = {
|
2089 |
-
"Paris": {"max": 25, "min": 15, "precip": 5},
|
2090 |
-
"Lyon": {"max": 28, "min": 18, "precip": 2},
|
2091 |
-
"Marseille": {"max": 30, "min": 20, "precip": 0},
|
2092 |
-
"Toulouse": {"max": 27, "min": 17, "precip": 10}
|
2093 |
-
}
|
2094 |
-
if info_meteo(donnees_meteo) == ("Paris", "Marseille", 17):
|
2095 |
-
print("Test 1 : OK")
|
2096 |
-
c += 1
|
2097 |
-
else :
|
2098 |
-
print("Test 1 : échec")
|
2099 |
-
if c == 1 :
|
2100 |
-
print("OK")
|
2101 |
-
else :
|
2102 |
-
print("KO")
|
2103 |
-
;;;
|
2104 |
-
moyenne notes (N3)
|
2105 |
-
;;;
|
2106 |
-
Un professeur utilise un dictionnaire pour stocker les notes de ses élèves. Chaque clé du dictionnaire est le nom de l'élève (une chaîne de caractères), et la valeur associée est une liste de notes (des nombres décimaux).
|
2107 |
-
Dans le cas où un élève est absent à une évaluation, on saisie la chaine "abs" à la place de la note.
|
2108 |
-
Exemple :
|
2109 |
-
notes = {
|
2110 |
-
"Martine" : [12, 11, 8, 15],
|
2111 |
-
"Anne-Marie" : [15, 17, 12, 12],
|
2112 |
-
"Patrick" : ["abs", 12, 11, 10],
|
2113 |
-
"Pierre-Louis" : ["abs", "abs", "abs", "abs"]
|
2114 |
-
}
|
2115 |
-
Écrire une fonction liste_moyenne qui prend un paramètre un dictionnaire (même structure que ci-dessus). Cette fonction renvoie un tableau contenant des tuples. Chaque tuple contient le prénom de l'élève en première position et la moyenne de l'élève en deuxième position.
|
2116 |
-
Exemple :
|
2117 |
-
>>> notes = {
|
2118 |
-
"Martine" : [12, 11, 8, 15],
|
2119 |
-
"Anne-Marie" : [15, 17, 12, 12],
|
2120 |
-
"Patrick" : ["abs", 12, 11, 10],
|
2121 |
-
"Pierre-Louis" : ["abs", "abs", "abs", "abs"]
|
2122 |
-
}
|
2123 |
-
>>> liste_moyenne(notes)
|
2124 |
-
[('Martine', 11.5), ('Anne-Marie', 14.0), ('Patrick', 11.0), ('Pierre-Louis', 'abs')]
|
2125 |
-
;;;
|
2126 |
-
c = 0
|
2127 |
-
notes = {
|
2128 |
-
"Martine" : [12, 11, 8, 15],
|
2129 |
-
"Anne-Marie" : [15, 17, 12, 12],
|
2130 |
-
"Patrick" : ["abs", 12, 11, 10],
|
2131 |
-
"Pierre-Louis" : ["abs", "abs", "abs", "abs"]
|
2132 |
-
}
|
2133 |
-
if liste_moyenne(notes) == [('Martine', 11.5), ('Anne-Marie', 14.0), ('Patrick', 11.0), ('Pierre-Louis', 'abs')]:
|
2134 |
-
print("Test 1 : OK")
|
2135 |
-
c += 1
|
2136 |
-
else :
|
2137 |
-
print("Test 1 : échec")
|
2138 |
-
if c == 1 :
|
2139 |
-
print("OK")
|
2140 |
-
else :
|
2141 |
-
print("KO")
|
2142 |
-
;;;
|
2143 |
-
calculatrice RPN (N3)
|
2144 |
-
;;;
|
2145 |
-
Écrire une fonction Python rpn_calcul qui prend en paramètre une expression expr sous forme d'un tableau de chaînes de caractères représentant une expression en notation polonaise inverse. Cette fonction doit renvoiyer le résultat du calcul. L'expression contient des nombres et des opérateurs (+, -, *, /).
|
2146 |
-
Utilisez une pile pour effectuer le calcul.
|
2147 |
-
Parcourez la liste expression.
|
2148 |
-
Si un élément est un nombre, empilez-le sur la pile.
|
2149 |
-
Si un élément est un opérateur (+, -, *, /), dépilez les deux derniers éléments de la pile, effectuez l'opération correspondante, et empilez le résultat.
|
2150 |
-
Exemples :
|
2151 |
-
>>> rpn_calcul(["3", "4", "+"])
|
2152 |
-
7
|
2153 |
-
>>> rpn_calcul(["4", "2", "-"])
|
2154 |
-
2
|
2155 |
-
>>> rpn_calcul(["10", "5", "+", "3", "*"])
|
2156 |
-
45
|
2157 |
-
>>> rpn_calcul(["15", "7", "1", "+", "-", "3", "*"])
|
2158 |
-
21
|
2159 |
-
;;;
|
2160 |
-
c = 0
|
2161 |
-
if rpn_calcul(["3", "4", "+"]) == 7:
|
2162 |
-
print("Test 1 : OK")
|
2163 |
-
c += 1
|
2164 |
-
else :
|
2165 |
-
print("Test 1 : échec")
|
2166 |
-
if rpn_calcul(["4", "2", "-"]) == 2:
|
2167 |
-
print("Test 2 : OK")
|
2168 |
-
c += 1
|
2169 |
-
else :
|
2170 |
-
print("Test 2 : échec")
|
2171 |
-
if rpn_calcul(["10", "5", "+", "3", "*"]) == 45:
|
2172 |
-
print("Test 3 : OK")
|
2173 |
-
c += 1
|
2174 |
-
else :
|
2175 |
-
print("Test 3 : échec")
|
2176 |
-
if rpn_calcul(["15", "7", "1", "+", "-", "3", "*"]) == 21:
|
2177 |
-
print("Test 4 : OK")
|
2178 |
-
c += 1
|
2179 |
-
else :
|
2180 |
-
print("Test 4 : échec")
|
2181 |
-
if c == 4 :
|
2182 |
-
print("OK")
|
2183 |
-
else :
|
2184 |
-
print("KO")
|
2185 |
-
;;;
|
2186 |
-
supprimer doublon (N2)
|
2187 |
-
;;;
|
2188 |
-
Écrire une fonction supprimer_doublons qui prend en argument un tableau tab et qui renvoie un tableau contenant les mêmes éléments que la liste d'entrée, mais sans doublons.
|
2189 |
-
Exemples :
|
2190 |
-
>>> supprimer_doublons([1, 2, 2, 3, 4, 4, 5, 3, 1])
|
2191 |
-
[1, 2, 3, 4, 5]
|
2192 |
-
>>> supprimer_doublons([5, 15, 22, 5, 8, 2, 5, 2, 1])
|
2193 |
-
[5, 15, 22, 8, 2, 1]
|
2194 |
-
>>> supprimer_doublons([1, 1, 1, 1, 1])
|
2195 |
-
[1]
|
2196 |
-
>>> supprimer_doublons([])
|
2197 |
-
[]
|
2198 |
-
;;;
|
2199 |
-
c = 0
|
2200 |
-
if supprimer_doublons([1, 2, 2, 3, 4, 4, 5, 3, 1]) == [1, 2, 3, 4, 5]:
|
2201 |
-
print("Test 1 : OK")
|
2202 |
-
c += 1
|
2203 |
-
else :
|
2204 |
-
print("Test 1 : échec")
|
2205 |
-
if supprimer_doublons([5, 15, 22, 5, 8, 2, 5, 2, 1]) == [5, 15, 22, 8, 2, 1]:
|
2206 |
-
print("Test 2 : OK")
|
2207 |
-
c += 1
|
2208 |
-
else :
|
2209 |
-
print("Test 2 : échec")
|
2210 |
-
if supprimer_doublons([1, 1, 1, 1, 1]) == [1]:
|
2211 |
-
print("Test 3 : OK")
|
2212 |
-
c += 1
|
2213 |
-
else :
|
2214 |
-
print("Test 3 : échec")
|
2215 |
-
if supprimer_doublons([]) == []:
|
2216 |
-
print("Test 4 : OK")
|
2217 |
-
c += 1
|
2218 |
-
else :
|
2219 |
-
print("Test 4 : échec")
|
2220 |
-
if c == 4 :
|
2221 |
-
print("OK")
|
2222 |
-
else :
|
2223 |
-
print("KO")
|
2224 |
-
;;;
|
2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