From efd0249cb6640d2c6a6785559947fb386a5e0f8b Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:36 +0100 Subject: [PATCH] Pascal's triangle --- python/calculators/Pascal's Triangle.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 python/calculators/Pascal's Triangle.py diff --git a/python/calculators/Pascal's Triangle.py b/python/calculators/Pascal's Triangle.py new file mode 100644 index 0000000..38b57de --- /dev/null +++ b/python/calculators/Pascal's Triangle.py @@ -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))