Stack Worksheet
Stack Worksheet
Write the following user defined functions to perform given operations on the stack named
‘status’:
Push_element() - To Push an object containing name and Phone number of customers who
live in Goa to the stack
Pop_element() - To Pop the objects from the stack and display them. Also, display “Stack
Empty” when there are no elements in the stack.
For example:
[“Julee”, “8888888888”,”Mumbai”]
[“Gurdas”,”9999999999”]
[“Gurdas”,”9999999999”]
Stack Empty
2.Write the definition of a user defined function PushNV(N) which accepts a list of strings in
the parameter N and pushes all strings which have no vowels present in it, into a list named
NoVowel.
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 stores 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
3.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.
The program should then use the function Push 3_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:
30 18 6 10 StackEmpty
4.Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push
all numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has
at least one element, otherwise display appropriate error message.
5.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.
For Example:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
38 22 98 56 34 12
6.Write a program to implement a stack for these book details (bookno, bookname). That is,
now each item node of the stack contains two types of information –a bookno and its name.
Just implement push and display operations.
7. Write a program to search input any customer name and display customer phone number
if the customer’s name is existed in the list.
8. You have a stack named BooksStack that contains records of books. Each book record is
represented as a list containing book_title, author_name, and publication_year.
9.Write the following user-defined functions in Python to perform the specified operations
on the stack BooksStack:
push_book(BooksStack, new_book): This function takes the stack BooksStack and a new
book record new_book as arguments and pushes the new book record onto the stack.
pop_book(BooksStack): This function pops the topmost book record from the stack and
returns it. If the stack is already empty, the function should display "Underflow".
peep(BookStack): This function displays the topmost element of the stack without deleting
it. If the stack is empty, the function should display 'None'.
10.A list, NList contains following record as list elements:
Each of these records are nested together to form a nested list. Write the following user
defined functions in Python to perform the specified operations on the stack named travel.
(i) Push_element(NList): It takes the nested list as an argument and pushes a list object
containing name of the city and country, which are not in India and distance is less than
3500 km from Delhi.
(ii) Pop_element(): It pops the objects from the stack and displays them. Also, the function
should display “Stack Empty” when there are no elements in the stack.
['Naypyidaw', 'Myanmar'],
['Dubai', 'UAE'],
['Dubai', 'UAE']
['Naypyidaw', 'Myanmar']
Stack Empty
11.A dictionary, d_city contains the records in the following format :
{state:city}
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
12.. A)
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.
Write the function pop(stack) that removes the top element of the stack on its each call.
13.
Write a function in Python push(EventDetails) where EventDetails is a dictionary
containing the number of persons attending the events–
{EventName : NumberOfPersons}.
The function should push the names of those events in the stack named ‘BigEvents’ which
have number of persons greater than 200. Also display the count of elements pushed on to
the stack.
Write the function pop(EventDetails) that removes the top element of the stack on its each
call.
14.Madhuri 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 ODD numbers into a stack.
13,21,89,35
15. Saroj have a list of 10 numbers . 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 numbers into a stack which are divisible by 5.
16. You have a stack named MovieStack that contains records of movies. Each movie record
is represented as a list containing movie_title, director_name, and release_year. Write the
following user-defined functions in Python to perform the specified operations on the stack
MovieStack:
push_movie(MovieStack, new_movie): This function takes the stack MovieStack and a new
movie record new_movie as arguments and pushes the new movie record onto the stack.
pop_movie(MovieStack): This function pops the topmost movie record from the stack and
returns it. If the stack is empty, the function should display "Stack is empty".
peek_movie(MovieStack): This function displays the topmost movie record from the stack
without deleting it. If the stack is empty, the function should display "None".
17. Write the definition of a user-defined function push_odd(M) which accepts a list of
integers in a parameter M and pushes all those integers which are odd from the list M into a
Stack named OddNumbers.
Write the function pop_odd() to pop the topmost number from the stack and return it. If the
stack is empty, the function should display "Stack is empty".
Write the function disp_odd() to display all elements of the stack without deleting them. If
the stack is empty, the function should display "None".
For example:
If the integers input into the list NUMBERS are: [7, 12, 9, 4, 15]
18. There is a stack named Uniform that contains records of uniforms Each record is
represented as a list containing uid, uame, ucolour, usize, uprice.
Write the following user-defined functions in python to perform the specified operations on
the stack Uniform :
Peep(): This function diplay the topmost element of the stack without deleting it.if the stack
is empty,the function should display ‘None’.
19.Write the definition of a user defined function push_words(N) which accept list of words
as parameter and pushes words starting with A into the stack named InspireA
Write the function pop_words(N) to pop topmost word from the stack and return it. if the
stack is empty, the function should display “Empty”.