-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal_Triangle_2.py
More file actions
43 lines (37 loc) · 1.21 KB
/
Pascal_Triangle_2.py
File metadata and controls
43 lines (37 loc) · 1.21 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
"""
Given a height, return a Pascal's triangle
"""
import unittest
def pascal_row(row):
if len(row) == 1:
return [row[0],row[0]]
new = [1]
for val in range(1,len(row)):
new.append(row[val-1] + row[val])
new.append(1)
return new
def pascal(p):
current_row = [1]
r = [current_row]
for row in xrange(1,p):
current_row = pascal_row(current_row)
r.append(current_row)
return r
class TestFirst(unittest.TestCase):
def test_first(self):
test = self
test.assert_equals = self.assertEqual
Test = self
Test.assert_equals = self.assertEqual
test.assert_equals(pascal_row([1]), [1,1])
test.assert_equals(pascal_row([1,1]), [1,2, 1])
test.assert_equals(pascal_row([1,2, 1]), [1,3,3, 1])
test.assert_equals(pascal_row([1,3,3,1]), [1,4,6,4,1])
test.assert_equals(pascal(1), [[1]])
test.assert_equals(pascal(5), [
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
])