DAY 10 - Python Program to Print the Pascal’s triangle for n number of rows given by the user
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)
elif j == i: #last element of list[i]
nested_list[i].append(1)
elif j > 0 and j < i: # logic for elements which are in between first and last element
value = nested_list[i-1][k] + nested_list[i-1][k+1]
nested_list[i].append(value)
k = k + 1
print(nested_list[i])
input: 5
o/p:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
#RR #Day10 #Python #Pascal’sTriangle #HappyLearning #WeLearnEveryday
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)
elif j == i: #last element of list[i]
nested_list[i].append(1)
elif j > 0 and j < i: # logic for elements which are in between first and last element
value = nested_list[i-1][k] + nested_list[i-1][k+1]
nested_list[i].append(value)
k = k + 1
print(nested_list[i])
input: 5
o/p:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
#RR #Day10 #Python #Pascal’sTriangle #HappyLearning #WeLearnEveryday
Pascal triangle - easy way to understand
ReplyDelete1. To get the value of any position in Pascal triangle. please use the below formula
nCr =n! / (k! * (n - k!))
here n is the row and k is the position of number in that row. both starts with 0.
3. Now write a function to get the factorial
def fact(x):
fact1 =1
for i in range(1,x+1):
fact1 = fact1*i
return fact1
4. now loop to get each element in the row
for i in range(n):
number=int( fact(n-1) /( fact(i) * fact(n-1-i))) # here (n-1 as the triangle formula start with zero)
pascal.append(number)
5. print the row ->> print(pascal)
Program -
n=int(input())
pascal=[]
def fact(x):
fact1 =1
for i in range(1,x+1):
fact1 = fact1*i
return fact1
for i in range(n):
n1=fact(n-1)
n2= fact(i) * fact(n-1-i)
number=int(n1/n2)
pascal.append(number)
print(pascal)
n=int(input())
ReplyDelete# we will make a list with n lists in it
# each nth list will have n numbers
#we put all 1 in all sub lists
#suppose n = 4
Pascals=[[1 for i in range(j)] for j in range(1, n+1)]
#Pascals = [[1], [1, 1], [1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
#we dont need to change first two sublists
for i in range(2, n):
for j in range(1,i):
Pascals[i][j]=Pascals[i-1][j-1]+Pascals[i-1][j]
#we add from previous sublist as shown in the link
print(Pascals[-1]) #print last element of the list