-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPascalTriangle.py
More file actions
85 lines (69 loc) · 2.67 KB
/
PascalTriangle.py
File metadata and controls
85 lines (69 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
File: PascalTriangle.py
Authors:
- Amey Thakur (https://github.com/Amey-Thakur)
- Mega Satish (https://github.com/msatmod)
Repository: https://github.com/Amey-Thakur/PYTHON-SHORTS
Release Date: January 9, 2022
License: MIT License
Description:
A high-fidelity computational utility for generating Pascal's Triangle.
This module implements an iterative Dynamic Programming approach to
efficiently compute binomial coefficients for triangular matrices.
Mathematical Logic:
Pascal's Triangle is a triangular array of binomial coefficients. The
value at the n-th row and k-th column is given by the formula:
C(n, k) = n! / (k! * (n-k)!)
The iterative construction follows Pascal's Identity:
P(n, k) = P(n-1, k-1) + P(n-1, k)
"""
from typing import List
class PascalTriangleGenerator:
"""Scholarly implementation of Pascal's Triangle generation services."""
@staticmethod
def generate(rows: int) -> List[List[int]]:
"""
Generates a Pascal's Triangle with the specified number of rows.
Args:
rows (int): The number of rows to generate.
Returns:
List[List[int]]: A list of lists representing the triangle.
"""
if rows <= 0:
return []
triangle = [[1]]
for i in range(1, rows):
prev_row = triangle[-1]
# Pascal's Identity: Sum of two adjacent elements in the previous row
row = [1]
for j in range(1, len(prev_row)):
row.append(prev_row[j-1] + prev_row[j])
row.append(1)
triangle.append(row)
return triangle
@staticmethod
def display(rows: int):
"""
Displays the Pascal's Triangle in a formatted matrix.
Args:
rows (int): The number of rows to display.
"""
triangle = PascalTriangleGenerator.generate(rows)
# Calculate padding for alignment based on the largest value (center of the last row)
max_val = triangle[-1][len(triangle[-1]) // 2]
width = len(str(max_val)) + 1
print(f"--- Pascal's Triangle Generator ({rows} Rows) ---")
for i, row in enumerate(triangle):
# Center the row for triangular aesthetic
row_str = " ".join(f"{val:{width}}" for val in row)
print(f"Row {i:2}: {row_str.center(width * rows)}")
print("-" * 40)
def run_demo():
"""Execution demo showcasing combinatorial matrices."""
generator = PascalTriangleGenerator()
# Standard 7-row triangle
generator.display(7)
# Extended 10-row triangle
generator.display(10)
if __name__ == "__main__":
run_demo()