Categories:Viewed: 5 - Published at: a few seconds ago

Write a Python Program to print the Reverse a Given Number

n=1023
rev=0
while(n>0):
    dig=n%10
    rev=rev*10+dig
    n=n//10
print("Reverse of the number:",rev)

Write a Python Program to Accept Three Digits and Print all Possible Combinations from the Digits

a=2
b=9
c=5
d=[]
d.append(a)
d.append(b)
d.append(c)
for i in range(0,3):
    for j in range(0,3):
        for k in range(0,3):
            if(i!=j&j!=k&k!=i):
                print(d[i],d[j],d[k])

Write a Python function to Print an Identity Matrix

def printidentitymatrix(n): for i in range(0,n): for j in range(0,n): if(i==j): print("1",sep=" ",end=" ") else: print("0",sep=" ",end=" ") print()

Write a Python Program Print Restaurant Menu using Class given menu and cost as list

class Food(object):
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def getprice(self):
        return self.price

    def __str__(self):
        return self.name + ' : ' + str(self.getprice())

def buildmenu(names, costs):
    menu = []
    for i in range(len(names)):
        menu.append(Food(names[i], costs[i]))
    return menu

names = ['Coffee', 'Tea', 'Pizza', 'Burger', 'Fries', 'Apple', 'Donut', 'Cake']

costs = [250, 150, 180, 70, 65, 55, 120, 350]

Foods = buildmenu(names, costs)

n = 1
for el in Foods:
    print(n,'. ', el)
    n = n + 1

Write a Python Program to print a list of fibonacci series for a given no using closer

def fib():
    cache = {1:1, 2:1}

    def calc_fib(n):
        if n not in cache:
            print(f'Calculating fib({n})')
            cache[n] = calc_fib(n - 1) + calc_fib(n - 2)
        return cache[n]
    return calc_fib

Write a Python Program to print a list of fibonacci series for a given no using class

class Fib:
    def __init__(self):
        self.cache = {1:1, 2:1}

    def fib(self, n):
        if n not in self.cache:
            print(f'Calculating fib({n})')
            self.cache[n] = self.fib(n-1) + self.fib(n-2)
        return self.cache[n]

Write a Python function to calculate factorial of a given no using closer

def fact():
    cache = {0:1, 1:1}

    def calc_fib(n):
        if n not in cache:
            print(f'Calculating fact({n})')
            cache[n] = calc_fib(n - 1) * n
        return cache[n]
    return calc_fib

Write a Python function to calculate factorial of a given no using class

class Fact:
    def __init__(self):
        self.cache = {0:1, 1:1}

    def fact(self, n):
        if n not in self.cache:
            self.cache[n] = self.fact(n-1) * n
        return self.cache[n]

Write a Python function to calculate dot product of two given sequence

def dot_product(a, b):
    return sum( e[0]*e[1] for e in zip(a, b))