from ljouhet import affiche
# NON
def matrice_nulle_NON(n, p):
return [[0]*p]*n
m = matrice_nulle_NON(2, 3)
m[0][1]=2
print(m)
affiche("m")
# OUI
def matrice_nulle(n, p):
return [[0]*p for i in range(n)]
m = matrice_nulle(2, 3)
m[0][1]=2
print(m)
affiche("m")
# NON 1
def copie_NON1(m):
return m
a = [[1, 2, 3], [4, 5, 6]]
b = copie_NON1(a)
a[0][0] = 100
b[1][1] = -7
print(a, b)
affiche("a b")
# NON 2
def copie_NON2(m):
return [x for x in m]
a = [[1, 2, 3], [4, 5, 6]]
b = copie_NON2(a)
a[0][0] = 100
b[1][1] = -7
print(a, b)
affiche("a b")
# OUI 1
def copie_OUI1(m):
return [x[:] for x in m]
a = [[1, 2, 3], [4, 5, 6]]
b = copie_OUI1(a)
a[0][0] = 100
b[1][1] = -7
print(a, b)
affiche("a b")
# OUI 2
def copie(m):
n, p = len(m), len(m[0])
resultat = matrice_nulle(n, p)
for i in range(n):
for j in range(p):
resultat[i][j] = m[i][j]
return resultat
a = [[1, 2, 3], [4, 5, 6]]
b = copie(a)
a[0][0] = 100
b[1][1] = -7
print(a, b)
affiche("a b")