Interview pattern programming logical question, pascal triangle, pyramid, right angle.

learn most important pattern programming question that are ask in various jobs, MNCs, interview. python code of designing pascal triangle and more..
Please Note: Python Language is more resrict about white spaces and indentation, so check it before running this given code below.

 

1. Draw a pattern programe of full pyramid triangle in python using function and class.

 
n=int(input())

class Fun:
    def fun1(self):
        for i in range(n):
            for j in range(n*3-i-1):
                print(" ", end="")


            for j  in range(i*2+1):
                print("*", end="")
            print("")

        c=n
        for i in  range (n):
            c-=1
            for j in range(n*2):
                print(" ", end="")
        
            for j in range(i):
                print(" ",end="")
            for j in  range(c*2+1,0,-1 ):
                print("*", end="")

            
            print("")



object1=Fun()            
object1.fun1()


 2.1 Draw a right angle triange with joining reverse right angle triange in python using private function calling.

2.2 Find the factorial of n number using private method.

 
class Fun: def __fun2(self,n=5): for i in range(n): for j in range(i): print("*", end="") print("") for i in range(n): n-=1 for j in range(n,0,-1): print("*", end="") print("") def __factorial(self , n): # return 1 if (n==0 or n==1) else self.__factorial(n-1)*n if n==0 or n==1: return 1 else: return self.__factorial(n-1)*n def calling_factorial(self, n): return self.__factorial(n) def design_pyramid(self, t): return self.__fun2(t) object2=Fun() object2.design_pyramid(n) print("Factorial of {} is {}".format( n, object2.calling_factorial(n)))

3. Draw a right angle triangle in python using Constructor

 
#this programe is implemented through the concept of constructor in python . n=int(input()) class Fun3 (): def __init__(self): for i in range(n): for j in range(n-i-1): print(" ", end="") for i in range(i+1): print("*", end="") print("") object3= Fun3()

4.1 Design a pattern of Pascal Triangle in python using Abstract class and functions.

4.2 Design a pyramid with joining reverse pyramid in python.

 
# Print Pascal's Triangle in Python from abc import ABC,abstractmethod from math import factorial n=int(input()) class Pascal(ABC): @abstractmethod def pascal_triangle(self):pass class Fun4( Pascal ): def pascal_triangle(self): for i in range(n): for j in range(n-i+1): # for left spacing print(" ", end=" ") for j in range(i+1): # nCr = n!/((n-r)!*r!) print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ") # for new line print() def inverse_pyramid(self): c=n for i in range (n): c-=1 for j in range(n*2): print(" ", end="") for j in range(i): print(" ",end="") for j in range(c*2+1,0,-1 ): print("*", end="") print("") for i in range(n): for j in range(n*3-i-1): print(" ", end="") for j in range(i*2+1): print("*", end="") print("") def t(): pass f4=Fun4() f4.pascal_triangle() f4.inverse_pyramid()

About the author

D Shwari
I'm a professor at National University's Department of Computer Science. My main streams are data science and data analysis. Project management for many computer science-related sectors. Next working project on Al with deep Learning.....

Post a Comment