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

Comments

  1. Pascal triangle - easy way to understand
    1. 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)

    ReplyDelete
  2. n=int(input())

    # 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

    ReplyDelete

Post a Comment

Popular posts from this blog

Day 32 - Python Script to track Available COVID-19 Vaccine Slots for 18+ in India

DAY 13 - Import data dynamically from Oracle DB using Python script in Power BI Desktop

Day 26 - Call Power BI REST APIs to get POWER BI REPORT Details