On crée une matrice de la façon suivante en indiquant les coordonnées (ligne, colonne) dans chaque cellule :
Écrire la fonction creeMatrice
sous forme récursive.
- un code python
- remarque : python2 vs python3
Une solution Python :
def creeMatrice(n, p, i = 0, j = 0, L = []) :
if i == n : return L
else :
if j == 0 : L.append([])
L[i].append((i+1,j+1))
if j == p-1 : return creeMatrice(n, p, i+1, 0, L)
else : return creeMatrice(n, p, i, j+1, L)
print( creeMatrice(5,4) )
Le code donné dans l'énoncé pour print
est un code suivant la syntaxe python2. En python3, on
écrira :
def creeMatrice(n,p) :
L=[ [] for i in range(n) ]
for i in range(n) :
for j in range(p) :
L[i].append((i+1,j+1))
return L
nbLigne = 5
nbColonne = 4
M = creeMatrice(nbLigne, nbColonne)
for i in range(nbLigne) :
for j in range(nbColonne) :
print(M[i][j], end=' ')
print()