Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 06faaea

Browse files
committed
feat: finish pascal tringle in python
1 parent 8a4b7f5 commit 06faaea

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎alternative/easy/pascal_triangle.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def generate(num_rows):
2+
result = []
3+
4+
for i in range(num_rows):
5+
row = []
6+
7+
for j in range(i + 1):
8+
if j == 0 or j == i:
9+
row.append(1)
10+
else:
11+
row.append(result[i - 1][j - 1] + result[i - 1][j])
12+
13+
result.append(row)
14+
15+
return result
16+
17+
def get_row(row_index):
18+
result = generate(row_index + 1)
19+
20+
return result.pop()
21+
22+
# Test cases
23+
assert generate(5) == [
24+
[1],
25+
[1, 1],
26+
[1, 2, 1],
27+
[1, 3, 3, 1],
28+
[1, 4, 6, 4, 1]
29+
]
30+
assert get_row(3) == [1, 3, 3, 1]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /