Professional Documents
Culture Documents
CS Stacks
CS Stacks
#program
SItem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
stack=[]
def push():
for k in SItem:
if SItem[k]>75:
stack.append(k)
print(stack)
push()
3.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 stack
named "status":
(i) Push_element(Visitors) - To Push an object containing Gender of visitor
who are in the age range of 15 to 20.
(ii) Pop_element() - To Pop the objects from the stack and count and 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 of 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 output should be
F
M
M
The output should be:
Female: 1
Male: 2
Done’’’
visitors=[[’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]]
status=[]
def push_element(visitors):
for i in visitors:
if i[4]>=15 and i[4]<20:
status.append(i[3])
print(status)
push_element(visitors)
def pop_element():
Fc=Mc=0
while len(status)!=0:
e=status.pop()
print(e)
if e==’M’:
Mc+=1
else:
Fc+=1
print("Female:",Fc)
print("Male:",Mc)
print("Done")
pop_element()
5.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.
6.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 elements of the stack on
its call. Also write the function calls.