Pascal's triangle

This commit is contained in:
newt! 2021-12-02 23:22:51 +00:00
parent cd2253f7fd
commit d880215d09
No known key found for this signature in database
GPG key ID: AE59B20CEDCE087D

View file

@ -0,0 +1,18 @@
def pascal(rowCount):
rows = [[1]]
for _ in range(rowCount):
previousRow = rows[-1]
newRow = [1] # starts with a 1
for j in range(len(previousRow) - 1):
newRow.append(previousRow[j] + previousRow[j + 1])
newRow.append(1) # ends with a 1
rows.append(newRow)
return rows
def nthRow(n):
rows = pascal(n)
return rows[n]
print(nthRow(10))