forked from fenyx-it-academy/Class8-Python-Module-Week3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascal_function.py
More file actions
28 lines (25 loc) · 960 Bytes
/
pascal_function.py
File metadata and controls
28 lines (25 loc) · 960 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
def pascal_triangle(number):
"""A function that accepts any integer n
and return the first n rows of pascal's triangle"""
row =[1]
for i in range(number):
for j in range(i+1):
print(row[j], end=" ")
row = [x+y for x, y in zip([0] + row , row + [0])]
print('\n')
if __name__ == "__pascal_triangle__":
pascal_triangle()
# .... the program below I wrote at first, which works perfectly but not efficient...........
# def pascal_triangle(number):
# for i in range (number):
# pascal_number = i
# for index in range(i+1):
# if index == 0:
# print(index + 1, end=" ")
# elif index == 1:
# print(i, end=" ")
# else:
# pascal_number *= (i-(index-1))/(index)
# print (int(pascal_number), end=" " )
# print('\n')
# pascal sequence: 1 , n , n(n−1)/2 , n(n−1)(n−2)/ 2*3 , n(n−1)(n−2)(n−3)/2*3*4......