Pyqs of Data Structure - Stack For More Such Type of Questions and Detailed Video Explanation, Visit The Link

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Youtube Channel : Swati Chawla (Please Subscribe the channel for more updates)

PYQs of Data Structure – Stack


For More such type of Questions and
detailed Video explanation, visit the
link :
https://youtube.com/live/I_Ykx4m_tAM

1. Julie has created a dictionary containing names and marks


as key value pairs of 6 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
75.

● Pop and display the content of the stack. For example: If


the sample content of the dictionary is as follows:
R={"OM":76, "JAI":45, "B/OB":89, "ALI":65, "ANU":90, "TOM":82}
The output from the program should be: TOM ANU BOB OM

R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}


def Push(S,N):
S.append(N)

def Pop(St):
return St.pop()
St=[]
for i in R:
if R[i]>75:
Push(St,i)
while True:
if St!=[]:
print(Pop(St),end=' ')
else:
break

2. 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. ●
Traverse the content of the list and push the even numbers
into a stack. ● Pop and display the content of the stack. 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

Prepared By : Swati Chawla


Youtube Channel : Swati Chawla (Please Subscribe the channel for more updates)

the code should be: 38 22 98 56 34 12

N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
def Push(St,N):
St.append(N)
def Pop(St):
return St.pop()
St=[]
for i in N:
if i%2==0:
Push(St,i)
print(St)

while True:
if St!=[]:
print(Pop(St),end=' ')
else:
break

3. BCCI has created a dictionary containing top players and


their runs as key value pairs of cricket team. Write a
program, with separate user defined functions to perform the
following operations: ● Push the keys (name of the players) of
the dictionary into a stack, where the corresponding value
(runs) is greater than 49. ● Pop and display the content of
the stack. For example: If the sample content of the
dictionary is as follows: SCORE={"KAPIL":40, "SACHIN":55,
"SAURAV":80, "RAHUL":35, "YUVRAJ":110, } The output from the
program should be: YUVRAJ SAURAV SACHIN

def Push(St,N):
St.append(N)
def Pop(St):
return St.pop()

SCORE={"KAPIL":40, "SACHIN":55, "SAURAV":80, "RAHUL":35,


"YUVRAJ":110, }
St=[]
for i in SCORE:
if SCORE[i]>49:
Push(St,i)

while True:
if St!=[]:
print(Pop(St),end=' ')
else:
break

4.Raghav has created a vocabulary list. You need to help him

Prepared By : Swati Chawla


Youtube Channel : Swati Chawla (Please Subscribe the channel for more updates)

create a program with separate user defined functions to


perform the following operations based on this list. ●
Traverse the content of the list and push the entries having
less than 7 characters into a stack. ● Pop and display the
content of the stack. For Example: If the sample Content of
the list is as follows: W=['Elucidate', 'Haughty', 'Pacify',
'Quip', 'Rapport', 'Urbane', 'Young','Zenith'] Sample Output
of the code should be: Zenith Young Urbane Quip Pacify

def Push(St,N):
St.append(N)
def Pop(St):
return St.pop()
W=['Elucidate', 'Haughty', 'Pacify', 'Quip', 'Rapport',
'Urbane', 'Young','Zenith']
St=[]
for i in W:
if len(i)<7:
Push(St,i)

while True:
if St!=[]:
print(Pop(St),end=' ')
else:
break

5. 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

def Push(N):
for i in N:
if 'A' in i:
OnlyA.append(i)
def Pop(OnlyA):
while True:
if OnlyA!=[]:
print((OnlyA.pop()),end=' ')
else:
break
print("Empty")
Names=['ANKITA', 'NITISH', 'ANWAR', 'DIMPLE', 'HARKIRAT']
OnlyA=[]

Prepared By : Swati Chawla


Youtube Channel : Swati Chawla (Please Subscribe the channel for more updates)

Push(Names)
Pop(OnlyA)

6.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

def pushEven(N):
for i in N:
if i%2==0:
EVEN.append(i)

def popEven(EVEN):
while True:
if EVEN!=[]:
print((EVEN.pop()),end=' ')
else:
break
print("Stack Empty")

N=[10,5,3,8,15,4]
EVEN=[]
pushEven(N)
popEven(EVEN)

7. ● Write the definition of a user defined function


Push3_5(N) which accepts a list of integers in a parameter N
and pushes all those integers which are divisible by 3 or
divisible by 5 from the list N into a list named Only3_5. ●
Write a program in Python to input 5 integers into a list
named NUM. The program should then use the function Push3_5()
to create the stack of the list Only3_5. Thereafter pop each
integer from the list Only3_5 and display the popped value.
When the list is empty, display the message "StackEmpty". For
example: If the integers input into the list NUM are :
[10,6,14,18,30] Then the stack Only3_5 should store
[10,6,18,30] And the output should be displayed as 30 18 6 10
StackEmpty

def Push3_5(N):
for i in N:
if i%3==0 or i%5==0:
Only3_5.append(i)

Prepared By : Swati Chawla


Youtube Channel : Swati Chawla (Please Subscribe the channel for more updates)

NUM=[]
Only3_5=[]

for i in range(5):
a=int(input("Enter Number : "))
NUM.append(a)
print(NUM)
Push3_5(NUM)
while True:
if Only3_5!=[]:
print((Only3_5.pop()),end=' ')
else:
break
print("Stack Empty")

8. Write the definition of a function POP_PUSH(LPop, LPush,


N) in Python. The function should Pop out the last N elements
of the list LPop and Push them into the list LPush. For
example: If the contents of the list LPop are [10, 15, 20, 30]
And value of N passed is 2, then the function should create
the list LPush as [30, 20] And the list LPop should now
contain [10, 15] NOTE: If the value of N is more than the
number of elements present in LPop, then display the message
“Pop not possible”.

def POP_PUSH(LPop, LPush, N):


if N>len(LPop):
for i in range(N):
a=LPop.pop()
LPush.append(a)
else:
print("Pop Not Possible...")

LPop=[10, 15, 20, 30]


N=2
LPush=[]
POP_PUSH(LPop,LPush,N)
print(LPop)
print(LPush)

9. 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

Prepared By : Swati Chawla


Youtube Channel : Swati Chawla (Please Subscribe the channel for more updates)

are no elements in the stack. For example: If the lists of


customer details are: [“Gurdas”, “99999999999”,”Goa”]
[“Julee”, “8888888888”,”Mumbai”]
[“Murugan”,”77777777777”,”Cochin”] [“Ashmit”,
“1010101010”,”Goa”] The stack should contain
[“Ashmit”,”1010101010”] [“Gurdas”,”9999999999”] The output
should be: [“Ashmit”,”1010101010”] [“Gurdas”,”9999999999”]
Stack Empty
status=[]

def Push_element(cust) :
if cust[2]=='Goa':
status.append(cust[0],cust[1])

Check video for complete solution

10. 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 count of elements pushed into the stack. For
example: If the dictionary contains the following data:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25} The
stack should contain Notebook Pen The output should be: 16
The count of elements in the stack is 2
stackitem=[]

def Push(Sitem):
for i in Sitem:
if Sitem[i] > 75:
stackitem.append(i)
print("Total elements in stack:",len(stackitem))
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
Push(Ditem)

For More such type of Questions and


detailed Video explanation, visit the
link :
https://youtube.com/live/I_Ykx4m_tAM

Prepared By : Swati Chawla

You might also like