-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.py
More file actions
38 lines (33 loc) · 916 Bytes
/
Copy pathPascalTriangle.py
File metadata and controls
38 lines (33 loc) · 916 Bytes
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
class Solution:
# https://www.interviewbit.com/problems/pascal-triangle/
# @param A : integer
# @return a list of list of integers
def generate(self, A):
triangle = []
for x in range(A):
if x > 0:
triangle.append(self.generate_next_row(triangle[x-1]))
else:
triangle.append([1])
return triangle
def generate_next_row(self, row):
ans = []
for each in range(len(row)+1):
try:
c = row[each]
except Exception:
c = 0
try:
if each-1 >=0:
c1 = row[each-1]
else:
c1 = 0
except Exception:
c1 = 0
ele = c + c1
ans.append(ele)
return ans
s = Solution()
tri = s.generate(10)
for each in tri:
print each