-
Notifications
You must be signed in to change notification settings - Fork 17
/
pascals-triangle.py
66 lines (51 loc) · 1.43 KB
/
pascals-triangle.py
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
import unittest
from typing import List
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
result = [[1] * (row + 1) for row in range(numRows)]
for row in range(1, numRows):
for pos in range(1, row):
result[row][pos] = result[row - 1][pos - 1]
result[row][pos] += result[row - 1][pos]
return result
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_zero(self):
assert self.sol.generate(0) == []
def test_one(self):
assert self.sol.generate(1) == [[1]]
def test_two(self):
assert self.sol.generate(2) == [[1], [1, 1]]
def test_three(self):
assert self.sol.generate(3) == [
[1],
[1, 1],
[1, 2, 1],
]
def test_four(self):
assert self.sol.generate(4) == [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
]
def test_five(self):
assert self.sol.generate(5) == [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
]
def test_six(self):
assert self.sol.generate(6) == [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
]
if __name__ == "__main__":
unittest.main()