Professional Documents
Culture Documents
Corrigé TD Listes
Corrigé TD Listes
[ ]: ## Correction TD : Listes
## Exercice 1 :
# Q 1 :
def smil(nb,ls) :
n = len(ls)
for i in range(n) :
ls[i] = ls[i] * nb
return ls
print(smil(2,[1, 2, 3]))
# Q 2 :
def vsom(l1,l2) :
n = len(l1)
l = []
for i in range(n) :
l.append(l1[i] + l2[i] )
return l
# Q 3 :
def vdif(l1,l2) :
n = len(l1)
l = []
for i in range(n) :
l.append(l1[i] - l2[i] )
return l
## Exercice 2 :
def diviseurs(n) :
l = []
# On cherche les diviseurs compris entre 1 et (n//2) + 1
1
for i in range(1,(n//2)+1) :
if n%i == 0 :
l.append(i)
return l
## Exercice 3 :
def Maximum(ls) :
max=ls[0]
for i in range(1,len(ls)) :
if max< ls[i] :
max=ls[i]
return max
L=[16,2,23,14,55,6]
print(Maximum(L))
## Exercice 4 :
def position(ls,nb) :
n = len(ls)
for i in range(n) :
if ls[i] == nb :
return i
return None
## Exercice 5 :
def positives(ls) :
n = len(ls)
c = 0
for i in range(n) :
if ls[i] >= 0 :
c = c + 1
return c
##Exercice 6 :
def verifier(l1,l2) :
if len(l1) != len(l2) :
return False
n = len(l1)
for i in range(n) :
2
if l1[i] != l2[i] :
return False
return True
## Exercice 7 :
def recherche_rec(ls,nb) :
if ls == [] :
return False
elif ls[0] == nb :
return True
else :
return recherche_rec(ls[1:],nb)
## Exercice 8 :
def decomposer(nombre) :
bit = []
for i in range(8) :
bit.append(nombre%2)
nombre = nombre // 2
return bit
## Exercice 9:
def decimale(bit) :
n = len(bit)
s = 0
for i in range(n) :
s = s + bit[i] * (2**i)
return s