xxxxxxxxxx
"""
This implementation demonstrates how to
generate the elements of the Pascal's
triangle
(https://en.wikipedia.org/wiki/Pascal%27s_triangle).
Let n be the number of rows to create.
Time complexity: O(n^2)
Space complexity: O(1)
"""
def generate_triangle(num_rows):
triangle = []
for row_idx in range(num_rows):
current_row = [None] * (row_idx+1)
# first and last elements of current row are always 1
current_row[0], current_row[-1] = 1, 1
for col_idx in range(1, row_idx):
above_to_left_elt = triangle[row_idx - 1][col_idx-1]
above_to_right_elt = triangle[row_idx - 1][col_idx]
current_row[col_idx] = above_to_left_elt + above_to_right_elt
triangle.append(current_row)
return triangle
# Below prints: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
print(generate_triangle(5))
xxxxxxxxxx
def printPascal(n):
for i in range(1,n + 1):
m = 1;
for j in range(1,i + 1):
print(m, end = " ")
m = int(m * (i - j) / j)
print("")
printPascal(int(input()))
#code by fawlid
xxxxxxxxxx
def solve(A):
pascals_triangle = []
for i in range(A):
pascals_triangle.append([1]*(i+1))
for i in range(2,A):
for j in range(1,i):
pascals_triangle[i][j] = pascals_triangle[i-1][j-1] + pascals_triangle[i-1][j]
return pascals_triangle
xxxxxxxxxx
"""
This implementation demonstrates how to
generate the elements of Pascal's
triangle (https://en.wikipedia.org/wiki/Pascal%27s_triangle).
Let n be the number of rows to create.
Time complexity: O(n^2)
Space complexity: O(1)
"""
def generate_triangle(num_rows):
triangle = []
for row_idx in range(num_rows):
current_row = [None] * (row_idx+1)
# first and last elements of current row are always 1
current_row[0], current_row[-1] = 1, 1
for col_idx in range(1, row_idx):
above_to_left_elt = triangle[row_idx - 1][col_idx-1]
above_to_right_elt = triangle[row_idx - 1][col_idx]
current_row[col_idx] = above_to_left_elt + above_to_right_elt
triangle.append(current_row)
return triangle
# Below prints: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
print(generate_triangle(5))