DAY 10 - Python Program to Print the Pascal’s triangle for n number of rows given by the user It's the very basic problem statement in python. Following is an 5 level Pascal's triangle: Sample Input: 5 Sample Output: [1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1] Solution: import pandas as pd import numpy as np n=int(input("Enter number of rows: ")) nested_list = [] for i in range(n): k = 0 nested_list.append([]) # keep on adding a new list in each iteration of i for j in range(i+1): #keep on appending list[i] if i <=1: # logic for 1st and 2nd list nested_list[i].append(1) else: # logic for 3rd list onwards if j == 0: # fisrt element of list[i] nested_list[i].append(1) ...
Comments
Post a Comment