PY
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
# 3-SUM-TRIANGLE CHALLENGE
def show_matrix(arr):
for row in arr:
print(row)
print("")
def show_tree(arr):
for i in range(0,len(arr)):
for x in range(0,i+1):
print(arr[i][x],end="\t")
print()
print("\nHope you like it !")
def solve(rows):
arr = [] # create a N*N matrix filled with '1'
for i in range(rows):
arr.append([1]*rows)
stop = 1 # loop until you reached the diagonal
i = 0
while i < stop and i < rows-1:
j = 0
while j <= stop and j < rows-1:
if i != j: # calculate value for current position
arr[i+1][j+1] = arr[i][j] + arr[i][j+1] + arr[i+1][j]
j += 1
stop += 1
i += 1
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run