-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpascal_triangle.py
More file actions
57 lines (51 loc) · 1.47 KB
/
pascal_triangle.py
File metadata and controls
57 lines (51 loc) · 1.47 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys
def beautify_list(list_object):
beautified_string = ""
max_len = 0
"""
This function is used to beautify a list with 2 dimension for Pascal's Triangle
"""
for i in list_object[-1]:
max_len += len(str(i))
max_len = max_len * 2
for row in list_object:
temp = ""
for item in row:
# if len(temp) < len(row)+1:
# temp += str(item)+" "
# else:
temp += str(item)+" "
temp = temp.center(max_len, ' ')
print(temp)
def draw_pascals(no_of_rows):
zen = 1
output_list = []
"""
This function is used to generate a 2 dimensional list for Pascal's Triangle
"""
for i in range(no_of_rows):
j = 0
output_list.append([])
while (j <= i):
if j == 0 or j == i:
output_list[i].append(zen)
else:
output_list[i].append(output_list[i-1][j]+output_list[i-1][j-1])
j += 1
beautify_list(output_list)
while True:
try:
user_entry = input("Enter number of rowz : ")
if user_entry.lower() == "quit":
break
# sys.exit()
user_entry = int(user_entry)
if user_entry > 0:
draw_pascals(user_entry)
else:
print("Length should be greater than zero")
except ValueError:
print("Enter only numbers")
except KeyboardInterrupt:
print("\nThankzzz")
sys.exit()