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

Solved Assignment Stack

The document outlines various Python programming tasks involving stack operations, including pushing and popping elements based on specific conditions such as student records, stationary items, city populations, and more. Each task includes a description of the required functions, example data, and expected outputs. The document serves as a guide for implementing stack-related functionalities in Python.

Uploaded by

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

Solved Assignment Stack

The document outlines various Python programming tasks involving stack operations, including pushing and popping elements based on specific conditions such as student records, stationary items, city populations, and more. Each task includes a description of the required functions, example data, and expected outputs. The document serves as a guide for implementing stack-related functionalities in Python.

Uploaded by

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

SOLVED ASSIGNMENT: STACK

1. A list contains following record of a student:[StudentName, Class, Section, MobileNumber]


Write the following user defined functions to perform given operations on the stack named ‘xiia’:
(i) pushElement() - To Push an object containing name and mobile number of students who
belong to class xii and section ‘a’ to the stack
(ii) popElement() - 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 students details are: [“Rajveer”, “99999999999”,”XI”, “B”]
[“Swatantra”, “8888888888”,”XII”, “A”]
[“Yash”, “1010101010”,”XII”,”A”]
The stack “xiia” should contain [“Swatantra”, “8888888888”]
[“Yash”, “1010101010”]
The output should be: [“Yash”, “1010101010”]
[“Swatantra”, “8888888888”]Stack Empty
Ans : xiia=[]
student= [['Rajveer', '99999999999','XI', 'B'],['Swatantra', '8888888888','XII', 'A'],
['Sajal', '77777777777','VIII','A'],['Yash', '1010101010','XII','A']]
def pushElement(student):
for d in student:
if d[2]=='XII' and d[3]=='A':
xiia.append([d[0],d[1]])
def popElement():
while len(xiia)!=0:
print(xiia.pop())
else:
print('Stack Empty')
pushElement(student)
print(xiia)
popElement()
2. 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 25.
Also display the count of elements pushed into the stack.
For example: If the dictionary contains the following data:
Ditem = {“Rubber”:5, "Pencil":5, "Pen":30, "Notebook": 60, "Eraser":5, “Watch”: 250}
The stack should contain Pen
Notebook
Watch
The output should be: The count of elements in the stack is 3
Ans : stackItem=[]
def Push(SItem):
count=0
for k in SItem:
if (SItem[k]>=25):
stackItem.append(k)
count=count+1
print("The count of elements in the stack is : ", count)

AMIT KUMAR KUSHWAHA(9891145409) Page 1


3. A dictionary contains the names of some cities and their population in crore. Write a python function
push(stack, data), that accepts an empty list, which is the stack and data, which is the dictionary and
pushes the names of those countries onto the stack whose population is greater than 25 crores.
For example :The data is having the contents {'India':140, 'USA':50, 'Russia':25, 'Japan':10}
then the execution of the function push() should push India and USA on the stack.
ANS : data={'India':140, 'USA':50, 'Russia':25, 'Japan':10}
stack=[]
def push(stack, data):
for x in data:
if data[x]>25:
stack.append(x)
push(stack, data)
print(stack)
4. Alam has a list containing 10 integers. You need to help him create a program with separate user
defined functions to perform the following operations based on this list.
a) Push_even(N) : Traverse the content of the list and push the even numbers into a stack named Even
.the function contains list of integers as ‘N’ as argument.
b) Pop_even(N) : Pop and display the content of the stack and generate message ‘underflow ‘if stack is
empty.
c) Disp_even() : to display all element of the stack without deleting them. If the stack is empty, the
function should display 'None'.
For Example:If the sample Content of the list is as follows:N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be:38 22 98 56 34 12
Ans : data = [12,13,34,56,21,79,98,22,35,38]
Even = []
def push(Even, data):
for x in data:
if x % 2 == 0:
Even.append(x)
def pop(Even):
if len(Even)==0:
return "stack empty"
else:
return Even.pop()
def disp_even(Even ):
if len(Even)==0:
print(“None”)
else:
print(Even[-1])

push(Even, data)
print(pop(Even))
disp_even(Even)
5.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
AMIT KUMAR KUSHWAHA(9891145409) Page 2
(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: [“Ashok”, “9999999999”,”Goa”]
[“Mahesh”,”77777777777”,”Cochin”]
[“Rakesh”, “66666666666”,”Goa”]
The stack should contain: [“Rakesh”,”66666666666”]
[“Ashok”,” 99999999999”]
The output should be: [“Rakesh”,”66666666666”]
[“Ashok”,”99999999999”]
Stack Empty
Ans : status=[]
def Push_element(cust):
if cust[2]=="Goa":
L1=[cust[0],cust[1]]
status.append(L1)
def Pop_element ():
while len(status)!=0:
print(status.pop())
else:
print("Stack Empty")
5. 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:
(i) Push the keys (name of the student) of the dictionary into a stack, where the corresponding
value (marks) is greater than 70.
(ii) 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}
Then the output will be: Umesh Vishal Ishika
Ans : stk=[]
d={"Ramesh":58, "Umesh":78, "Vishal":90, "Khushi":60, "Ishika":95}
def pus h(stk,item):
for i in item:
if item[i]>70:
stk.append(i)
def Pop(stk):
if stk==[]:
return None
else:
return stk.pop()
6. Write a function in Python, Push(book) where, book is a dictionary containing the details of a book in
form of {bookno : price}.The function should push the book in the stack which have price greater than
300. Also display the count of elements pushed into the stack.
For example: If the dictionary contains the following data:
Dbook={"Python":350,"Hindi":200,"English":270,"Physics":600, “Chemistry”:550}
The stack should contain : Chemistry
Physics
Python
AMIT KUMAR KUSHWAHA(9891145409) Page 3
The output should be: The count of elements in the stack is 3
Ans : Books=[]
def Push (books,Dbooks):
for b in Dbooks:
if Dbooks[b]>300:
Books.append(b)
def remove(books):
if books==[]:
print(“stack is empty”)
else:

print(“book:”,Books.pop(), “deleted”)
7. A nested list contains the data of visitors in a museum. Each of the inner lists contains the following data of
a visitor: [V_no (int), Date (string), Name (string), Gender (String M/F), Age (int)]

Write the following user defined functions to perform given operations on the stacknamed "status":
(i) Push_element(Visitors) - To Push an object containing Gender of visitor whoare in the age
range of 15 to 20.
(ii) Pop_element() - To Pop the objects from the stack and count the display the number of
Male and Female entries in the stack. Also, display “Done” when there are no elements in
the stack.
For example: If the list Visitors contains: [['305', "10/11/2022", “Geeta”,"F”, 35],
['306', "10/11/2022", “Arham”,"M”, 15],
['307', "11/11/2022", “David”,"M”, 18],
['308', "11/11/2022", “Madhuri”,"F”, 17],
['309', "11/11/2022", “Sikandar”,"M”, 13]]
The stack should contain : F
M
M
The output should be: Done
Female: 1
Male: 2
Ans : status=[]
def Push_Element(visitors):
global status
m_c=0
f_c=0
for i in visitors:
if i[4]>=15 and i[4]<=20:
status.append( i[3])
if i[3]=='M':
m_c+=1
if i[3]=='F':

AMIT KUMAR KUSHWAHA(9891145409) Page 4


f_c+=1
print("Males:",m_c)
print("FeMales:",f_c)
def Pop_Element():
global status
if status!=[]:
return status.pop()
else:
return "Done"
Push_Element(visitors)
for i in range(len(status)+1):
print(Pop_Element())
8A 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 “No more
tours” when there are no elements in the stack.
For example if the following dictionaries are passed to Push_tour( ) function in the following sequence:
{ ‘Name’: ‘Goa’ , ‘PeakSeason’ : ‘December’ , ‘Budget’ : 15000 , ‘Famous’:’Beaches’}
{ ‘Name’: ‘Nainital’ , ‘PeakSeason’ : ‘May’ , ‘Budget’ : 9000 , ‘Famous’:’Nature’}
{ ‘Name’: ‘Sikkim’ , ‘PeakSeason’ : ‘May’ , ‘Budget’ : 9500 , ‘Famous’:’Mountains’}
{ ‘Name’: ‘Orissa’ , ‘PeakSeason’ : ‘January’ , ‘Budget’ : 8000 , ‘Famous’:’Temples’}
Then the stack ‘tour’ will contain : [ ‘Orrisa’ , ‘January’]
[‘Sikkim’, ‘May’]
[‘Nainital’ ,’ May’]
The output produced when calling pop_tour( ) function should be : [ ‘Orrisa’ , ‘January’]
[‘Sikkim’, ‘May’]
[‘Nainital’ ,’ May’]
No more tours
Ans :tour = [ ]
def Push_tour(tour_dict) :
if tour_dict[‘Budget’] < 10000 :
LstTour = [ tour_dict[‘Name’] , tour_dict[‘PeakSeason’] ]
tour.append( LstTour )
def Pop_tour( ) :
while tour != [ ] :
T = tour.pop( )
print(T)
else:
print( “No more tours”)
9. Write a function in Python, Push(stack, SItem) where , SItem is a List containing the details of stationary
items in a format like – [Name , price , Unit , Company , MRP ].

AMIT KUMAR KUSHWAHA(9891145409) Page 5


The function should push the company names of those items in the stack whose price is 10 % percent less
than its MRP. Also write a function print_stack( ) to display the Item Names and the count of Items
pushed into the stack.
For example: If following data is passed to the function:
[ ‘Pen’ , 120.00 , ‘Pcs.’ , ‘Reynolds’ , 132.00 ]
[‘Paper’, 345.00 , ‘Rim’ , ‘Camel’, 500.00]
[Eraser , 100.00 , ‘Box’ , ‘IBP’ , 110.00
The stack should contain EraserPen
The output should be: The count of elements in the stack is 2
Ans : def Push(stack , SItem) :
if SItem[ 1] == SItem[4] * 0.90 :
Stack.append(SItem)

def print_stack( ):
Count = len(stack)
while stack != [ ]:
Item = stack.pop()
print( Item[ 0 ] )
else:
print(“Total items present in the stack is :”, count)
10. Write a function in Python, Push(KItem), where KItem is a dictionary containing the details of Kitchen
items– {Item:price}.
The function should push the names of those items in a stack which have price less than 100. Also display
the average price of elements pushed into the stack.
For example: If the dictionary contains the following data:
{"Spoons":116,"Knife":50,"Plates":180,"Glass":60}
The stack should contain Glass
Knife
The output should be: The average price of an item is 55.0
Ans : def Push(KItem):
st=[] #stack
c,s=0,0
for k,v in KItem.items():
if v<100:
st.append(k)
c+=1
s+=v
if c!=0:
av=s/c
print("The average price of an item is",av)
11. You have a stack named BooksStack that contains records of books. Each book record is represented as a
list containingbook_title,author_name, and publication_year.
Write the following user-defined functions in Python to perform the specified operations on the stack
BooksStack:
 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.
 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".

AMIT KUMAR KUSHWAHA(9891145409) Page 6


 peep(BookStack): This function displays the topmost element of the stack without deleting it. If
the stack is empty, the function should display 'None'.
Ans : def push_book(BooksStack, new_book):
BooksStack.append(new_book)
def pop_book(BooksStack):
if not BooksStack:
print("Underflow")
else:
return(BookStack.pop())
def peep(BooksStack):
if not BooksStack:
print("None")
else:
print(BookStack[-1])

AMIT KUMAR KUSHWAHA(9891145409) Page 7

You might also like