VELAMMAL BODHI CAMPUS PONNERI
STACK RELATED QUESTIONS:
1. A School 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:
(a) Push the name of the players(Keys) of the dictionary into a
stack, where the corresponding runs (value) is greater than 49.
(b) Pop and display the content of the stack.
For Example
If dictionary has the following values:
Data={'Rohan':40, 'Rihaan':55, 'Tejas':80,'Ajay':90}
The output should be:
Ajay
Tejas
Rihaan
# Function to push players into stack
def push_players(data, stack):
for player in data:
if data[player] > 49:
[Link](player)
# Function to pop and display stack elements
def pop_players(stack):
while stack:
print([Link]())
# Main program
Data = {'Rohan':40, 'Rihaan':55, 'Tejas':80, 'Ajay':90}
stack = []
push_players(Data, stack)
pop_players(stack)
2. Priyanka has created a dictionary 'employee_data' containing
EmpCode and Salary as key value pairs for 5 Employees of Cyber
Intratech. Write a program, With separate user defined function, as
mentioned below, to perform the following operations:
(a) push_emp(): Push all those EmpCode, where the Salary is less
than 25000, from the dictionary into a stack 'stk_emp'
(b) pop_emp(): Remove all the elements from the stack, one at a
time, in a Last-In-First-Out(LIFO) manner and displays them. It also
displays 'Stack is empty' once all the element have been removed.
For Example:
If the sample content of the dictionary is as follows:
{'E001':15000,'E002':27000,'E003':30000,'E004':15000,'E005':19000}
, then the stack 'stk_emp' will contain EmpCode E001, E004, E005
after push_emp(). pop_emp() will pop and display employee record in
LIFO fashion and display 'Stack is empty' at last.
# Function to push employee codes into stack
def push_emp(employee_data, stk_emp):
for emp in employee_data:
if employee_data[emp] < 25000:
stk_emp.append(emp)
def pop_emp(stk_emp):
while stk_emp:
print(stk_emp.pop())
print("Stack is empty")
employee_data = { 'E001':15000, 'E002':27000, 'E003':30000,
'E004':15000, 'E005':19000}
stk_emp = []
push_emp(employee_data, stk_emp)
pop_emp(stk_emp)
6 - A school stores records of Class XII students using a list that
contains multiple lists as its elements. The structure of each such
element is [Student_Name, Marks, MainSubject]. Create user-defined
functions to perform the operations as mentioned below:
(a) Push_student(): To push the Student_Name and Marks of all those
students, who have Science as MainSubject, into a Stack StudentInfo
(b) Pop_student(): To delete all items (one at a time) from the stack
StudentInfo in LIFO order and display them. Also display "Empty
Stack" when there are no items remaining in the stack.
For Example:
If the stored information is:
[['Akansha',98,"Mathematics"],["Priti",96,"Science"],
["Garima",99,"Science"],["Ayushi",78,"English"]]
The stack should contain:
["Garima",99]
["Priti",96]
The output should be:
["Garima",99]
["Priti",96]
Empty Stack
# Function to push students into stack
def push_student(data, StudentInfo):
for record in data:
if record[2] == "Science":
[Link]([record[0], record[1]])
# Function to pop and display stack elements
def pop_student(StudentInfo):
while StudentInfo:
print([Link]())
print("Empty Stack")
# Main Program
students = [
['Akansha', 98, "Mathematics"],
['Priti', 96, "Science"],
['Garima', 99, "Science"],
['Ayushi', 78, "English"]
]
StudentInfo = []
push_student(students, StudentInfo)
pop_student(StudentInfo)
8 - Write a program in Python to input 5 words and push them one by
one into a list named All.
The program should then use the function PushNV() to create a stack
of words in the list NoVowel so that it store only those words which
do not have any vowel present in it, from the list All.
Thereafter, pop each word from the list NoVowel and display the
popped word. When the stack is empty display the message
'EmptyStack'.
For Example:
If the words accepted and pushed into the list All are
['DRY','LIKE','RHYTHM','WORK','GYM']
Then the stack NoVowel should store
['DRY','RHYTHM','GYM']
And the output should be displayed as
GYM RHYTHM DRY EmptyStack
# Function to push words without vowels into stack
def PushNV(All, NoVowel):
vowels = "AEIOUaeiou"
for word in All:
has_vowel = False
for ch in word:
if ch in vowels:
has_vowel = True
break
if not has_vowel:
[Link](word)
# Main Program
All = []
# Input 5 words
for i in range(5):
w = input("Enter word: ")
[Link](w)
NoVowel = []
PushNV(All, NoVowel)
# Pop and display words
while NoVowel:
print([Link](), end=" ")
print("EmptyStack")
9 - Write a program in Python to input 5 integers into a list named
NUM.
The program should then use the function Push3_5() to push all those
integers which are divisible by 3 or divisible by 5 from the list NUM
into 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
# Function to push numbers divisible by 3 or 5 into stack
def Push3_5(NUM, Only3_5):
for n in NUM:
if n % 3 == 0 or n % 5 == 0:
Only3_5.append(n)
# Main Program
NUM = []
# Input 5 integers
for i in range(5):
num = int(input("Enter number: "))
[Link](num)
Only3_5 = []
Push3_5(NUM, Only3_5)
# Pop and display elements
while Only3_5:
print(Only3_5.pop(), end=" ")
print("StackEmpty")
13 - 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':
(a) Push_element(): To push an object containing the Course_name,
Fees, and Duration of a course, which has fee greater than 100000 to
the stack.
(b) Pop_element(): To pop the object from the stack and display it.
Also, display "Underflow" when there is no element in the stack.
For Example:
If the lists of courses details are:
["MCA", 200000, 3]
["MBA", 500000, 2]
["BA", 100000, 3]
The stack should contain:
["MCA", 200000, 3]
["MBA", 500000, 2]
# Function to push course details into stack
def Push_element(courses, Univ):
for course in courses:
if course[1] > 100000: # Fees > 100000
[Link](course)
# Function to pop and display stack elements
def Pop_element(Univ):
while Univ:
print([Link]())
print("Underflow")
# Main Program
courses = [
["MCA", 200000, 3],
["MBA", 500000, 2],
["BA", 100000, 3]
]
Univ = []
Push_element(courses, Univ)
Pop_element(Univ)
14 - Write separate user defined functions for the following:(a)
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'
(b) 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','DIPLE','HARKIRAT']
Then the stack OnlyA should store
['ANKITA','ANWAR','HARKIRAT']
And the output should be displayed as
HARKIRAT ANWAR ANKITA EMPTY
# Function to push names containing 'A' into stack
def PUSH(N, OnlyA):
for name in N:
if 'A' in name or 'a' in name:
[Link](name)
# Function to pop and display stack elements
def POPA(OnlyA):
while OnlyA:
print([Link](), end=" ")
print("EMPTY")
# Main Program
N = ['ANKITA','NITISH','ANWAR','DIPLE','HARKIRAT']
OnlyA = []
PUSH(N, OnlyA)
POPA(OnlyA)