Suppose dictionary d has items and there cost
as give format {"Pen":16,"Pencil":119,"Notebook":80,"Eraser":25}
define push() to add item having cost more then 75 into stack s
d={"Pen":16,"Pencil":119,"Notebook":80,"Eraser":25}
s=[]
def push(d):
for i, j in d.items():
if(j>75):
s.append(i)
push(d)
print(s)
Output:
['Pencil', 'Notebook']
Q Consider a list named Nums which contain s random integers.
Write the following user defined functions in Python and perform the specified operations on a stack named BigNums.
(i) PushBig():It checks every number from the list Nums and pushes all such numbers which have 5 or more digits into the stack, BigNums.
(ii) PopBig(): It pops the numbers from the stack, BigNums and displays them.The function should also display "Stack Empty"when there are no more numbers left in the stack.
For example:If the list Nums contains the following data:
Nums=[213,10025,167,254923,14,1297653,31498,386,92765]
Then on execution of PushBig(),the stack BigNums should store:
[10025,254923,1297653,31498,92765]
And on execution of PopBig(),the following output should be displayed:
92765
31498
1297653
254923
10025
Stack Empty
Nums=[213,10025,167,254923,14,1297653,31498,386,92765]
BigNums=[]
def PushBig(Nums,BigNums):
for N in Nums:
if len(str(N))>=5:
BigNums.append(N)
def PopBig(BigNums):
while BigNums:
print(BigNums.pop())
else:
print("StackEmpty")
PushBig(Nums,BigNums)
PopBig(BigNums)
Q A list contains following record of course details for a University :
[Course_name, Fees, Duration]
Write the following user defined functions to perform given operations on the stack named 'Univ' :
(i) Push_element() To push an object containing the
Course_name, Fees and Duration of a course, which has fees
greater than 100000 to the stack.
(ii) Pop_element() To pop the object from the stack and display it.
also display "under flow " when stack is empty"""
courses=[["MCA",250000,3],["PGDCA",100000,1],["MSC",200000,2]]
Univ=[]
def Push_element():
for i in courses:
if(i[1]>100000):
Univ.append(i)
def Pop_element():
if len(Univ)==0:
print("Under Flow")
else:
print( Univ.pop() )
Push_element()
print(Univ)
Pop_element()
Pop_element()
Pop_element()
Output:
[['MCA', 250000, 3], ['MSC', 200000, 2]]
['MSC', 200000, 2]
['MCA', 250000, 3]
Under Flow
Q
Write separate user defined functions for the following :
(i) PUSH(N) This function accepts a list of names, N as parameter.
It then pushes only those names in the stack named OnlyA
which contain the letter 'A'.
(ii) POPA(OnlyA) This function pops each name from the stack
OnlyA and displays it. When the stack is empty, the message
"EMPTY" is displayed. For example :
If the names in the list N are
['ANKITA', 'NITISH', 'ANWAR', 'DIMPLE', 'HARKIRAT']
Then the stack OnlyA should store
['ANKITA', 'ANWAR', 'HARKIRAT']
And the output should be displayed as
HARKIRAT ANWAR ANKITA EMPTY """
N=['ANKITA', 'NITISH', 'ANWAR', 'DIMPLE', 'HARKIRAT']
ONLYA=[]
def PUSH(N):
for i in N:
if "A" in i:
ONLYA.append(i)
def POPA(ONLYA):
if(len(ONLYA)==0):
print("EMPTY")
else:
print( ONLYA.pop() )
PUSH(N)
print(ONLYA)
POPA(ONLYA)
POPA(ONLYA)
POPA(ONLYA)
POPA(ONLYA)
Output: : ['ANKITA', 'ANWAR', 'HARKIRAT']
HARKIRAT
ANWAR
ANKITA
EMPTY
Q
Write the following user defined functions :
(i) pushEven(N) This function accepts a list of integers named N
as parameter. It then pushes only even numbers into the stack
named EVEN.
(ii) popEven(EVEN) This function pops each integer from the
stack EVEN and displays the popped value. When the stack is
empty, the message "Stack Empty" is displayed.
For example :
If the list N contains
[10,5,3,8,15,4]
Then the stack, EVEN should store
[10,8,4]
And the output should be
4 8 10 Stack Empty
Ans:
N= [10,5,3,8,15,4]
EVEN=[]
def pushEven(N):
for i in N:
if(i%2==0):
EVEN.append(i)
def popEven(EVEN):
if(len(EVEN)==0):
print("Stack Empty")
else:
print(EVEN.pop())
output:
[10, 8, 4]
4
8
10
Stack Empty
Q
A dictionary, d_city contains the records in the following format :
{state:city}
Define the following functions with the given specifications :
(i) push_city(d_city): It takes the dictionary as an argument and
pushes all the cities in the stack CITY whose states are of more than 4 characters.
(ii) pop_city(): This function pops the cities and displays "Stack
empty" when there are no more cities in the stack. """
d_city={"Punjab":"Patiala","Rajasthan":"Jaipur","Hariyana":"Panipat"}
CITY=[]
def push_city(d_city):
for i in d_city.keys():
if(len(i)>4):
CITY.append(d_city[i])
def pop_city(CITY):
if(len(CITY)==0):
print("Stack empty")
else:
print(CITY.pop())
push_city(d_city)
print(CITY)
pop_city(CITY)
pop_city(CITY)
pop_city(CITY)
pop_city(CITY)
print(CITY)
output:-
['Patiala', 'Jaipur', 'Panipat']
Panipat
Jaipur
Patiala
Stack empty
[]