0% found this document useful (0 votes)
17 views9 pages

Stack Worksheet

The document outlines a series of programming tasks that involve creating user-defined functions to manipulate stacks in Python. These tasks include pushing and popping elements based on specific criteria, such as customer details, words without vowels, integers divisible by certain numbers, and various other data structures. Each task provides examples and expected outputs to guide the implementation of the required functions.

Uploaded by

sbioaanitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Stack Worksheet

The document outlines a series of programming tasks that involve creating user-defined functions to manipulate stacks in Python. These tasks include pushing and popping elements based on specific criteria, such as customer details, words without vowels, integers divisible by certain numbers, and various other data structures. Each task provides examples and expected outputs to guide the implementation of the required functions.

Uploaded by

sbioaanitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Worksheet For Stack

1.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’:

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:

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

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

['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

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.

Write a program in Python to input 5 integers into a list named NUM.

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:

If the integers input into the list NUM are :

[10, 6, 14, 18, 30]

Then the stack Only 3_5 should store

[10, 6, 18, 30]

And the output should be displayed as

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.

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 the code should be:

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:

[City, Country, distance from Delhi]

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.

For example: If the nested list contains the following data:

NList=[["New York", "U.S.A.", 11734],

["Naypyidaw", "Myanmar", 3219],

["Dubai", "UAE", 2194],

["London", "England", 6693], ["Gangtok", "India", 1580],

["Columbo", "Sri Lanka", 3405]]

The stack should contain:

['Naypyidaw', 'Myanmar'],

['Dubai', 'UAE'],

['Columbo', 'Sri Lanka']

The output should be:

['Columbo', 'Sri Lanka']

['Dubai', 'UAE']

['Naypyidaw', 'Myanmar']

Stack Empty
11.A dictionary, d_city contains the records in the following format :

{state:city}

Define the following functions with the given specifications : 3

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

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.

Also write the function calls.

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.

Also write the function calls. For example:

If the dictionary contains the following data: EventDetails ={“Marriage”:300, “Graduation


Party”:1500,“Birthday Party”:80, “Get together” :150}

The stack should contain :- 3

Marriage Graduation Party

The output should be:

The count of elements in the stack is 2

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.

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 the code should be:

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.

Pop and display the content of the stack. For Example:

If the sample Content of the list is as follows: N=[2,5,10,13,20,23,45,56,60,78]

Sample Output of the code should be: 5,10,20,45,60

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]

Then the stack OddNumbers should store: [7, 9, 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 :

Push_Uniform(new_uniform):adds the new uniform record onto the stack


Pop_Uniform(): pops the topmost record from the stack and returns it. If the stack is already
empty, the function should display “underflow”.

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

You might also like