-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPascalsTriangle.py
59 lines (42 loc) · 1.61 KB
/
PascalsTriangle.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
'''
In mathematics, Pascal's triangle is a triangular array of the binomial coefficients expressed with formula (n k) = n!/(n-k)!, where n denotes a row of the triangle, and k is a position of a term in the row.
Task
Write a function that, given a depth n, returns n top rows of Pascal's Triangle flattened into a one-dimensional list/array.
n guarantees that terms of the Triangle won't overflow.
Example:
n = 1: [1]
n = 2: [1, 1, 1]
n = 4: [1, 1, 1, 1, 2, 1, 1, 3, 3, 1]
RESULT :
***********************[1]************************
**********************[1, 1]**********************
********************[1, 2, 1]*********************
*******************[1, 3, 3, 1]*******************
*****************[1, 4, 6, 4, 1]******************
'''
def pascals_triangle(n):
a, temp, temp2 = [1], [1], [1]
space = 50
print(f'{temp}'.center(50,'*'))
for i in range(1,n):
for j in range(len(temp)):
try:temp2.insert(j+1,temp[j] + temp[j+1])
except: temp2.append(1)
temp = temp2
print(f'{temp2}'.center(space,'*'))
for d in temp2: a.append(d)
temp2 = [1]
return a
print(pascals_triangle(5))
# *************************************************************************************
# this version return a nested array
def pascal(n):
a, temp, temp2 = [[1]], [1], [1]
for i in range(1,n):
for j in range(len(temp)):
try:temp2.insert(j+1,temp[j] + temp[j+1])
except: temp2.append(1)
temp = temp2
a.append(temp2)
temp2 = [1]
return a