0% found this document useful (0 votes)
8 views7 pages

Python Stack Operations for Books and Tours

The document outlines a series of Python programs that implement stack operations for various data types, including book records, tourist information, customer details, student marks, and numerical data. Each section defines user-defined functions for pushing and popping elements from stacks, along with specific conditions for the operations. The examples illustrate how to manage stacks using lists in Python, demonstrating basic stack functionalities such as push, pop, and peep.

Uploaded by

tharun.dv.engg
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)
8 views7 pages

Python Stack Operations for Books and Tours

The document outlines a series of Python programs that implement stack operations for various data types, including book records, tourist information, customer details, student marks, and numerical data. Each section defines user-defined functions for pushing and popping elements from stacks, along with specific conditions for the operations. The examples illustrate how to manage stacks using lists in Python, demonstrating basic stack functionalities such as push, pop, and peep.

Uploaded by

tharun.dv.engg
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

Stack Programs

1. You have a stack named BooksStack that contains records of books. Each
book record is represented as a list containing book_title, author_name, and
publication_year. Write the following user-defined functions in Python to
perform the specified operations on the stack BooksStack:
(I) push_book(BooksStack, new_book): This function takes the stack
BooksStack and a new book record new_book as arguments and pushes the new
book record onto the stack.
(II) pop_book(BooksStack): This function pops the topmost book record from
the stack and returns it. If the stack is already empty, the function should
display "Underflow".
(III) peep(BookStack): This function displays the topmost element of the stack
without deleting it. If the stack is empty, the function should display 'None'.
Answer:
(I) def push_book(BooksStack, new_book):
[Link](new_book)
(II) def pop_book(BooksStack):
if len(BooksStack)==0:
print("Underflow")
else:
return([Link]())
(III) def peep(BooksStack):
if len(BooksStack)==0:
print("None")
else:
print(BookStack[-1])

2. A dictionary contains records of a tourist place like :


Tour_dict = {'Name' : 'Goa', 'PeakSeason' : 'December', 'Budget' :
15000 ,'Famous' : 'Beaches'}
Write the following user defined functions to perform given operations on the
stack named„tour‟:

(i) Push_tour(Tour_dict) – To Push a list containing values for Name and


PeakSeason where value of budget is less than 10000 into the tour stack.

(ii) Pop_tour() – To Pop the list objects from the stack and display them. Also,
display “Nomore tours” when there are no elements in the stack.
(i)def push(Tour_dict):
if Tour_dict['Budget']>10000:
[Link]([Tour_dict['Name'],Tour_dict['PeakSeason']])
top=len(s)-1
(ii)def pop():
top=len(s)-1
while(top>=0):
print("popped item", [Link]())
top=top-1
else:
print("stack empty")
Tour_dict = {'Name' : 'Goa', 'PeakSeason' : 'December', 'Budget' :
15000 ,'Famous' : 'Beaches'}
s=[]
top=None
push(Tour_dict)
pop()
3. A list contains following record of a customer: [Customer_name,
Phone_number,City]
Write the following user defined functions to perform given operations on the
stack named status:
(i) Push_element(). To Push an object containing name and Phone
number of customers who live in Goa to the Stack.
(ii) Pop_element(). To Pop the objects from the stack and display them.
Also, display “Stack Empty” when there are no elements in the stack.
For example:
If the lists of customer details are:
[ “Gurda”,”99999999”,”Goa]
[“Julee”,”8888888888”, “Mumbai”]
[“Ashmit”, “101010101” , “Goa”]
The stack should contain
[“Ashmit”, “101010101”]
[ “Gurda”,”99999999”]
status=[]
(i)def Push_element(cust):
if cust[2]==”Goa”:
[Link]([[cust[0],cust[1]])
(ii)def Pop_element():
ln=len(status)
while ln!=0:
popped=[Link]()
print(popped)
ln=ln-1

4. Vedika has created a dictionary containing names and marks as key-value


pairs of 5 students. Write a program, with separate user-defined functions to
perform the following operations:
Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 70. Pop and display the content of
the stack.
The dictionary should be as follows:
d={“Ramesh”:58, “Umesh”:78, “Vishal”:90, “Khushi”:60, “Ishika”:95}
The Stack should contain: [“Umesh”, “Vishal”, “Ishika”]
Answer:
def push(d):
for i in d:
if d[i]>70:
[Link](i)
top=len(s)-1
def display(d):
top=len(s)-1
while(top>=0):
print("popped item" ,[Link]())
top=len(s)-1
else:
print("stack empty")
s=[]
top=None
d={"Ramesh":58,"Umesh":78,"Vishal":90,"Khushi":60,"Ishika":95}
push(d)
display(d)

5.A list of numbers is used to populate the contents of a stack using a function
push(stack, data) where stack is an empty list and data is the list of numbers.
The function should push all the numbers that are even to the stack. Also write
the function pop(stack) that removes the top element of the stack on its each
call. Also write the function calls.
The number of list: [1,2,3,4,5,6,7,8,9,10]
The Stack should contain:[2,4,6,8,10]
def push(s,data):
for i in data:
if i%2==0:
[Link](i)
top=len(s)-1
def pop(s):
top=len(s)-1
while(top>=0):
print("popped item", [Link]())
top=top-1
else:
print("stack empty")
n=[1,2,3,4,5,6,7,8]
s=[]
top=None
push(s,n)
pop(s)

6. Abi has created a list of elements. Help her to write a program in python with
functions, PushNum(S,Num) and PopNum (S) to add a new element and delete
an element from a List of element named ‘S’ considering them to act as push
and pop operations of the Stack data structure . Push the element into the stack
only when the element is divisible by 4.
For eg: if L=[2,5,6,8,24,32]
then stack content will be
32 <- Top
24
8
Answer:
def PushNum(S, Num):
[Link](Num)
top=len(S)-1
def PopNum(S):
if S!=[]:
return [Link]()
top=len(S)-1
else:
return None
S=[]
top=None
N=[1,2,3,4,5,6,7,8,9,10]
for i in N:
if i%4==0:
PushNum(S,i)
while True:
if S!=[]:
print("Popped item",PopNum(S))
else:
break
7. Jina has a list containing 10 integers. You need to help her create a program
with two user defined functions to perform the following operations based on
this list.
(i) Traverse the content of the list and push those numbers into a stack which are
divisible by both 5 and 3.
(ii) Pop and display the content of the stack.
For example: If the sample content of the list is as follows:
L=[5,15,18,20,23,24,30,45,50,60] Sample Output of the code should be: 60 45
30 15
Answer:
def push(list):
for a in list:
if a%5==0 and a%3==0:
[Link](a)
top=len(s)-1
def pop():
top=len(s)-1
while top>=0:
print("Popped Item is ",[Link]())
top=top-1
else: print("Stack is Empty")
l=[5,15,18,20,23,24,30,45,50,60]
s=[]
top=None
push(l)
pop()

You might also like