def ecrete(tab: list, a: int, b: int)->list:
tab_ecrete = tab[:] # copie du tableau passé en argument
for i in range(len(copie)) : # parcours par indice, seul moyen de pouvoir modifier les éléments du tableau
if tab_ecrete[i] < a:
tab_ecrete[i] = a
elif tab_ecrete[i] > b:
tab_ecrete[i] = b
return tab_ecrete
t = [35, 47, 47, 34, 82, 81, 1, 63, 60, 40, 83, 29, 66, 97, 54, 21, 73, 40, 3, 86, 25, 10, 59, 56, 72, 97, 62, 45, 54, 13, 30, 68, 12, 17, 68, 3, 54, 71, 85, 23, 45, 4, 9, 21, 45, 84, 62, 16, 3, 34]
a = 20
b = 80
print(ecrete(tab)) # affiche le tableau écrété
print(tab) # affiche le tableau d'origine, non modifié
from random import randint
def melanger(tab: list)->list:
# on ne fait pas de copie puisqu'on veut modifier directement le tableau
for i in range(len(tab)): # pour chaque élément successif du tableau,
indice = randint(i, len(tab)-1) # on, tire un indice au hasard entre l'indice courant et l'indice du dernier élément du tableau
tab[i], tab[indice] = tab[indice], tab[i] # on échange les deux éléments
return tab
t = [35, 47, 47, 34, 82, 81, 1, 63, 60, 40, 83, 29, 66, 97, 54, 21, 73, 40, 3, 86, 25, 10, 59, 56, 72, 97, 62, 45, 54, 13, 30, 68, 12, 17, 68, 3, 54, 71, 85, 23, 45, 4, 9, 21, 45, 84, 62, 16, 3, 34]
print(melange(tab))
Attention à l'instruction randint()
, qui ne fonctionne pas comme un range()
!
Si on supprime l'instruction return
, le tableau tab est quand même modifié, illustrant le caractère mutable d'un tableau : même si il n'est pas passé
en argument, il sera quand même modifié à l'intérieur de la fonction...
from random import randint
def compteSuprplus(L:list)->list:
tab = [0]*10
for i in range(10):
if L[i] > 25:
tab[i] = L[i] - 25
return tab
def repartitionPossible(L:list)->list:
L_modif = [0]*10
for i in range(10):
L_modif[i] = 25 - L[i]
suppressions = 0
places_dispos = 0
for i in range(10):
if L_modif[i] >= 0:
places_dispos += L_modif[i]
else:
suppressions -= L_modif[i]
print(suppressions, places_dispos)
if suppressions <= places_dispos:
return L_modif
else:
return 'Pas possible'
classes = [randint(20,30) for i in range(10)]
print(classes)
print(repartitionPossible(classes))