0% found this document useful (0 votes)
15 views13 pages

Rec Filenew

Uploaded by

pramiti.g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Rec Filenew

Uploaded by

pramiti.g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Q35. 1.

A list, NList contains following record as list elements:

[City, Country, distance from Delhi]

Each of these records are nested together to form a nested list. Write the

following user defined functions in Python to perform the specified

operations on the stack named travel.

Implement a menu driven program having the following menu:

1. Create stack # calls the Push_element function

2. Display stack # calls the Disp_stack function

3. Delete the stack # calls the Pop_element function

4. Exit

(i) Push_element(NList): It takes the nested list as an argument and pushes a list

object containing name of the city and country, which are not in India and

distance is less than 3500 km from Delhi.

(ii) Pop_element(): It pops the objects from the stack and displays them. Also,

the function should display “Stack Empty” when there are no elements in the

stack.

(iii) Disp_stack(): This function should display the stack.

CODE-

nlist=[]

travel=[]

n=int(input("enter number of records in the list: "))

for i in range (n):

city=input("enter city name: ")


country=input("enter country name: ")

distance=int(input("enter distance from dehli: "))

record=[city, country, distance]

nlist.append(record)

def push():

for i in nlist:

if i[2]<3500 and i[1]!="india":

travel.append(i)

print(travel)

def pop():

while len(travel):

print(travel.pop())

else:

print("Stack Empty")

def display():

print(travel[::-1])

push()

pop()

display()
OUTPUT-

enter number of records in the list: 6

enter city name: New York

enter country name: USA

enter distance from dehli: 11734

enter city name: Naypyidaw

enter country name: Myanmar

enter distance from dehli: 3219

enter city name: Dubai

enter country name: UAE

enter distance from dehli: 2194

enter city name: London

enter country name: England

enter distance from dehli: 6693

enter city name: Gangtok

enter country name: India


enter distance from dehli: 1580

enter city name: Columbo

enter country name: Sri Lanka

enter distance from dehli: 3405

['Columbo ', 'Sri Lanka ', 3405]

['Gangtok ', 'India ', 1580]

['Dubai ', 'UAE ', 2194]

['Naypyidaw ', 'Myanmar ', 3219]

Stack Empty

[]
Q2. Write a function in Python, Push(SItem) where , SItem is a dictionary

containing the details of stationary items– {Sname:price}.

The function should push the names of those items in the stack who

have price greater than 75. Also display the stack and count of elements pushed

into the stack.

CODE-

SItem={}

items=[]

count=0

n=int(input("enter no of records:"))

for i in range (n):

prod=input("enter name of prduct")


price=int(input("enter price:"))

SItem[prod]=price

def push():

global count

for price in SItem:

if SItem[price]> 75:

items.append(price)

count+=1

stack=items[::-1]

if items==[]:

print("No item is priced more than 75rs.")

else:

for i in stack:

print(i)

print("The count of elements in the stack is ",count)

push()
OUTPUT-

enter no of records:4

enter name of prductPen

enter price:106

enter name of prductpencil

enter price:59

enter name of prductnotebook

enter price:80

enter name of prducteraser

enter price:25

notebook

Pen

The count of elements in the stack is 2


Q3. Write a menu driven program for stacks with the following options. The stack

will hold alphabets:

1.Push [pushes one alphabet at a time]

2.Pop

3.Peek

4.Display [displays each alphabet twice]

5.Exit

CODE-

stack=eval(input("Enter list/stack:"))

lis=stack[::-1]

def push():

if stack==[]:
print("stack empty")

else:

for i in lis:

print(i, end=" ")

def pop():

if stack==[]:

print("stack empty")

else:

print(lis.pop(0))

def peek():

print(lis[0])

def display():

if stack==[]:

print("stack empty")

else:

for i in range (len(lis)):

print(lis[i], lis[i], sep="", end=" ")

def menu():

while True:
print("\nMenu:")

print("1. Push")

print("2. Pop")

print("3. Peek")

print("4. Display")

print("5. Exit")

choice = int(input("Enter your choice: "))

if choice==1:

push()

elif choice==2:

pop()

elif choice==3:

peek()

elif choice==4:

display()

elif choice==5:

break

menu()
OUTPUT-

Enter list/stack:['g', 'a', 'm', 'e']

Menu:

1. Push

2. Pop

3. Peek

4. Display

5. Exit

Enter your choice: 1


emag

Menu:

1. Push

2. Pop

3. Peek

4. Display

5. Exit

Enter your choice: 2

Menu:

1. Push

2. Pop

3. Peek

4. Display

5. Exit

Enter your choice: 3

Menu:

1. Push

2. Pop

3. Peek

4. Display

5. Exit
Enter your choice: 4

mm aa gg

Menu:

1. Push

2. Pop

3. Peek

4. Display

5. Exit

Enter your choice: 5

You might also like