Le triangle de Pascal avec python
Question
Écrire un programme python permettant d'obtenir le triangle de Pascal.
Indice
Compléter le programme suivant (la 1re ligne de la fonction sert à créer un tableau triangulaire composé de 0 :
[[0], [0, 0], [0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0, 0], ...]
1
def pascal(n) :
2
l = [ [0]*i for i in range(n+1)]
3
for i in range(n+1) :
4
for j in range(i+1) :
5
6
if j == 0 :
7
l[i][j] = ...
8
elif j == ...:
9
l[i][j] = ...
10
else :
11
l[i][j] = ...
12
return(l)
13
14
15
for i in range(10):
16
print(pascal(10)[i])
17
Solution
1
def pascal(n) :
2
l = [ [0]*(i+1) for i in range(n+1)]
3
for i in range(n+1) :
4
for j in range(i+1) :
5
6
if j == 0 :
7
l[i][j] = 1
8
elif j == i:
9
l[i][j] = 1
10
else :
11
l[i][j] = l[i-1][j-1]+l[i-1][j]
12
return(l)
13
14
15
for i in range(11):
16
print(pascal(11)[i])
17