KEMBAR78
Stack Question Answer | PDF | Computing | Algorithms And Data Structures
0% found this document useful (0 votes)
76 views9 pages

Stack Question Answer

The document contains a series of Python programming tasks related to stack operations using lists. Each task involves defining functions for pushing and popping elements based on specific conditions, such as student names, employee salaries, and various numerical lists. The document provides code examples for each task, demonstrating how to implement stack behavior in Python.

Uploaded by

priyacsavnagai
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)
76 views9 pages

Stack Question Answer

The document contains a series of Python programming tasks related to stack operations using lists. Each task involves defining functions for pushing and popping elements based on specific conditions, such as student names, employee salaries, and various numerical lists. The document provides code examples for each task, demonstrating how to implement stack behavior in Python.

Uploaded by

priyacsavnagai
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
You are on page 1/ 9

1.

Write a function push (student) and pop (student) to add a new student name
and remove a student name from a list student, considering them to act as PUSH
and POP operations of stack Data Structure in Python.

Ans.:

def push(student):
name=input("Enter student name:")
student.append(name)

def pop(student):
if student==[]:
print("Underflow")
else:
student.pop()
2. Write PUSH(Names) and POP(Names) methods in python to add Names and Remove
names considering them to act as Push and Pop operations of Stack.

def PUSH(Names):
name=input("Enter name:")
Names.append(name)

def POP(Names):
if Names==[]:
print("Underflow")
else:
Names.pop()
3. Ram has created a dictionary containing names and age 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(age) is lesser than 40. Pop and display the content of the stack.

For example: If the sample content of the dictionary is as follows:

R={“OM”:35,”JAI”:40,”BOB”:53,”ALI”:66,”ANU”:19}

The output from the program should be:

ANU OM

R={"OM":35,"JAI":40,"BOB":53,"ALI":66,"ANU":19}

def Push(stk,n):
stk.append(n)

def Pop(stk):
if stk!=[]:
return stk.pop()
else:
return None
s=[]
for i in R:
if R[i]<40:
Push(s,i)

while True:
if s!=[]:
print(Pop(s),end=" ")
else:
break
4. SHEELA has a list containing 5 integers. You need to help Her create a program with
separate user defined functions to perform the following operations based on this list.

1. Traverse the content of the list and push the odd numbers into a stack.
2. Pop and display the content of the stack.

For Example:

If the sample Content of the list is as follows:

N=[79,98,22,35,38]

Sample Output of the code should be:

35,79

N=[79,98,22,35,38]

def Push(stk,on):
stk.append(on)

def Pop(stk):
if stk==[]:
return None
else:
return stk.pop()
stk=[]

for i in N:
if i%2!=0:
Push(stk,i)

while True:
if stk!=[]:
print(Pop(stk),end=" ")
else:
break
5. Write a function in Python PUSH_IN(L), where L is a list of numbers. From this list,
push all even numbers into a stack which is implemented by using another list.

N=[79,98,22,35,38]

def Push(stk,on):
stk.append(on)

def Pop(stk):
if stk==[]:
return None
else:
return stk.pop()
stk=[]

for i in N:
if i%2==0:
Push(stk,i)

while True:
if stk!=[]:
print(Pop(stk),end=" ")
else:
break
6. Write a function in Python POP_OUT(Stk), where Stk is a stack implemented by a
list of numbers. The function returns the value which is deleted/popped from the stack.

def POP_OUT(Stk):
if Stk==[]:
return None
else:
return Stk.pop()
7. 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:

1. Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 75.
2. 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, “BOB”: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(stk,n):
stk.append(n)

def Pop(stk):
if stk!=[]:
return stk.pop()
else:
return None
s=[]
for i in R:
if R[i]>75:
Push(s,i)

while True:
if s!=[]:
print(Pop(s),end=" ")
else:
break
8. Raju has created a dictionary containing employee names and their salaries as key
value pairs of 6 employees. Write a program, with separate user defined functions to
perform the following operations:

1. Push the keys (employee name) of the dictionary into a stack, where the
corresponding value (salary) is less than 85000.
2. Pop and display the content of the stack.

For example:

If the sample content of the dictionary is as follows:

Emp={“Ajay”:76000, “Jyothi”:150000, “David”:89000, “Remya”:65000, “Karthika”:90000,


“Vijay”:82000}

The output from the program should be:

Vijay Remya Ajay

Emp={"Ajay":76000, "Jyothi":150000, "David":89000, "Remya":65000, "Karthika":90000,


"Vijay":82000}

def Push(stk,sal):
stk.append(sal)

def Pop(stk):
if stk==[]:
return None
else:
return stk.pop()

stk=[]
for i in Emp:
if Emp[i]<85000:
Push(stk,i)

while True:
if stk!=[]:
print(Pop(),end=" ")
else:
break
9. Anjali has a list containing temperatures of 10 cities. You need to help her create a
program with separate user-defined functions to perform the following operations based on
this list.

1. Traverse the content of the list and push the negative temperatures into a stack.
2. Pop and display the content of the stack.

For Example:

If the sample Content of the list is as follows:

T=[-9, 3, 31, -6, 12, 19, -2, 15, -5, 38]

Sample Output of the code should be: -5 -2 -6 -9

T= [-9, 3, 31, -6, 12, 19, -2, 15, -5, 38]

def Push(s,n):
s.append(n)

def Pop(s):
if s!=[]:
return s.pop()
else:
return None

s=[]
for i in T:
if i<0:
Push(s,i)

while True:
if s!=[]:
print(Pop(s),end = " ")
else:
break
10. Ms.Suman has a list of integers. Help her to create separate user defined functions to
perform following operations on the list.

1. DoPush(elt) to insert only prime numbers onto the stack.


2. DoPop() to pop and display content of the stack.

For eg:if L=[2.5,6,11,18,24,32,37,42,47] then stack content will be 2 5 11 37 47

def DoPush(elt):
L= [2,5,6,11,18,24,32,37,42,47]
for i in L:
for j in range(2,i):
if i % j ==0:
break
else:
elt.append(i)

def DoPop(s):
if s!=[]:
return s.pop()
else:
return None

s=[]
DoPush(s)

while True:
if s!=[]:
print(DoPop(s),end=" ")
else:
break
11. Mr. Ramesh has created a dictionary containing Student IDs and Marks as key
value pairs of students. Write a program to perform the following operations Using
separate user defined functions.

1. Push the keys (IDs) of the dictionary into the stack, if the corresponding marks is
>50
2. Pop and display the content of the stack

For eg:if D={2000:58,2001:45,2002:55,2003:40} Then output will be: 2000,2002

Do Yourself…

12. Write AddNew (Book) and Remove(Book) methods in Python to add a new Book
and Remove a Book from a List of Books Considering them to act as PUSH and POP
operations of the data structure Stack?

def AddNew(Book):
name=input("Enter Name of Book:")
Book.append(name)

def Remove(Book):
if Book==[]:
print("Underflow")
else:
Book.pop()
13. Assume a dictionary names RO having Regional Offices and Number of nodal
centre schools as key-value pairs. Write a program with separate user-defined functions
to perform the following operations:

1. Push the keys (Name of Region Office) of the dictionary into a stack, where the
corresponding value (Number of Nodal Centre Schools) is more than 100.
2. Pop and display the content of the stack.

For example

If the sample content of the dictionary is as follows:

RO={“AJMER”:185, “Panchkula”:95, “Delhi”:207, “Guwahati”:87, “Bubaneshwar”:189}

The output from the program should be:

AJMER DELHI BHUBANESHWAR

Do yourself…

14. Write a function in Python PUSH (Lst), where Lst is a list of numbers. From this list
push all numbers not divisible by 7 into a stack implemented by using a list. Display the
stack if it has at least one element, otherwise display appropriate error message.

def PUSH(Lst):
stk=[]
for i in range(len(Lst)):
if Lst[i]%7==0:
stk.append(Lst[i])

if len(stk)==0:
print("Stack is underflow")
else:
print(stk)
15. Write a function in Python POP(Lst), where Lst is a stack implemented by a list of
numbers. The function returns the value deleted from the stack.

def POP(Lst):
if len(stk)==0:
return None
else:
return Lst.pop()
16. Reva has created a dictionary containing Product names and prices as key value
pairs of 4 products. Write a user defined function for the following:
PRODPUSH() which takes a list as stack and the above dictionary as the parameters. Push
the keys (Pname of the product) of the dictionary into a stack, where the corresponding price
of the products is less than 6000. Also write the statement to call the above function.

For example: If Reva has created the dictionary is as follows:

Product={“TV”:10000, “MOBILE”:4500, “PC”:12500, “FURNITURE”:5500}

The output from the program should be: [ ‘FURNITURE’, ‘MOBILE’]

Do Yourself…

17. Pankaj has to create a record of books containing BookNo, BookName and
BookPrice. Write a user- defined function to create a stack and perform the following
operations:

1. Input the Book No, BookName and BookPrice from the user and Push into the
stack.
2. Display the status of stack after each insertion.

def Push():
books=[]
stk=[]
bno=int(input("Enter Book Number:"))
bname=input("Enter Book Name:")
bprice=input("Enter Book Price:")
books=[bno,bname,bprice]
stk.append(books)
print(stk)
18. Write a function in Python PUSH(mydict),where mydict is a dictionary of
phonebook(name and mobile numbers), from this dictionary push only phone numbers
having last digit is greater than or equal to 5 to a stack implemented by using list. Write
function POP() to pop and DISPLAY() to display the contents.

if it has at least one element, otherwise display “stack empty” message.

>>> mydict={9446789123:”Ram”,8889912345:”Sam”,7789012367:”Sree”}

>>> push(mydict)

Phone number: 9446789123 last digit is less than five which can’t be pushed

Stack elements after push operation : [7789012367, 8889912345]

mydict={9446789123:"Ram",8889912345:"Sam",7789012367:"Sree"}

def Push(mydict):
stk=[]
for i in mydict:
if i%10>=5:
stk.append(i)
print(stk)

Push(mydict)
19. Write a function to push an element in a stack which adds the name of passengers
on a train, which starts with capital ‘S’. Display the list of passengers using stack.

For example: L = [‘Satish’,’Manish’,’Sagar’,’Vipul’]

Output will be: Satish Sagar

L = ['Satish','Manish','Sagar','Vipul']
def Push(L,name):
L.append(name)
stk=[]

for i in L:
if i[0]=='S':
Push(stk,i)

print(stk)

You might also like