Wednesday, 7 August 2019

Prog-10 Write a Python program to implement a Queue using a list data-structure.


class Queue:
    def __init__(self):
        self.items = []
    def isEmpty(self):
        return self.items == []
    def enqueue(self, item):
        self.items.insert(0,item)
    def dequeue(self):
        return self.items.pop()
    def size(self):
        return len(self.items)
 
q = Queue()
while True:
    print('Press 1 for insert')
    print('Press 2 for delete')
    print('Press 3 for quit')
    do = int(input('What would you like to do'))
    if do == 1:
        n=int(input("enter a number to push"))
        q.enqueue(n)
    elif do == 2:
        if q.isEmpty():
            print('Queue is empty.')
        else:
            print('Deleted value: ', q.dequeue())
    elif do == 3:
        break


Input and Output

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
 RESTART: C:/Users/Student/AppData/Local/Programs/Python/Python37-32/qrks.py

Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do1
enter a number to push5
Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do1
enter a number to push2
Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do1
enter a number to push10
Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do 2
Deleted value:  5
Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do 2
Deleted value:  2
Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do 2
Deleted value:  10
Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do 2
Queue is empty.
Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do 2
Queue is empty.
Press 1 for insert
Press 2 for delete
Press 3 for quit
What would you like to do 3
>>>


Explaination:

self 

The word 'self' is used to represent the instance of a class. By using the "self" keyword we access the attributes and methods of the class in python.

__init__ method

"__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.
Find out the cost of a rectangular field with breadth(b=120), length(l=160). It costs x (2000) rupees per 1 square unit
class Rectangle:
   def __init__(self, length, breadth, unit_cost=0):
       self.length = length
       self.breadth = breadth
       self.unit_cost = unit_cost
   def get_area(self):
       return self.length * self.breadth
   def calculate_cost(self):
       area = self.get_area()
       return area * self.unit_cost
# breadth = 120 units, length = 160 units, 1 sq unit cost = Rs 2000
r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s sq units" % (r.get_area()))
This gives the output
Area of Rectangle: 19200 sq units
Cost of rectangular field: Rs.38400000

No comments:

Post a Comment