Data Structures Worksheet
Data Structures Worksheet
SCHOOL
CHETPET, CHENNAI – 600 031.
For example, if STRING = "Learning history helps to know about history with
interest in history" and SEARCH = 'history'
9. CBG is a dictionary that stores the name of the country and their respective
Central Banks Governor’s name as shown below.
CBG={"Germany":"JoachimNagel","France":"FrancoisVilleroy de Galhau",
"India":"ShaktikantaDas","UnitedKingdom":"AndrewBailey",
"UnitedStates":"Jer ome Powell"}
Page 1 of 11
Write a function getNames(CBG) that will take the dictionary as its parameter
and will display only those Governor’s names which are more than 20
characters long including the spaces in between.
Example:
If L contains [67,153,311,96,370,405,371,955,407]
Example: If the list initially contains [2, 15, 3, 14, 7, 9, 19, 6, 1, 10] and n=2
then function should return [3, 14, 7, 9, 19, 6, 1, 10, 2, 15]
If the list initially contains [2, 15, 3, 14, 7, 9, 19, 6, 1, 10] and n=4
then function should return [7, 9, 19, 6, 1, 10, 2, 15, 3, 14]
For example:
If L contains [1,2,3,4,5,6,7,8]
16.Write a user defined function change(L) to accept a list of numbers and replace
the numbers in the list with their sum of digits.
Example Input : [32,142,215,26,7]
Output : [5, 7 , 8 , 8, 8,7]
17.“Stack is a linear data structure which follows a particular order in which the
operations are performed”.
Name the list method/function available in Python which is used to remove the
last element from a list implemented as stack.
Page 2 of 11
Also write an example using Python statements for removing the last element
of the list.
23.Consider the following operations are done on a stack. What will be the final
status of the stack after all the operations are performed.
(a) Push(True)
(b) Push(False)
(c) Push(10)
(d) Pop()
(e) Push(50)
(f) Push(70)
(g) Pop()
(h) Pop()
24.Find the final contents of a stack on which the following operations are done.
1. Push(100)
2. Push(200)
3. Push(50)
4. Push(50)
5. Pop()
6. Push()
7. Pop()
25.Consider STACK=['a','b','c','d']
Write the STACK content after each operations:
a) STACK.pop( ) b) STACK.append('e')
c) STACK.append('f') d) STACK.pop( )
26.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.
Page 3 of 11
Display the stack if it has at least one element, otherwise display appropriate
error message.
28.Write a function push (student) and pop (student) to add a new student name
and remove student name from a list student, considering them to act as PUSH
and POP operations of stack Data Structure in Python.
31.Madhu has a message (string) which has a few digits in it. Write a program,
with separate user defined functions to perform the following operations:
● Push the digits in the string into a stack.
● Pop and display the content of the stack.
34.Raghav has created a vocabulary list. 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 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’]
35.Varun has a list containing 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 2 digit 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=[2, 131, 34, 56, 21, 379, 98, -22, 35, 38]
Page 4 of 11
Sample Output of the code should be:
38 35 -22 98 21 56 34
36.Write the following user defined functions to perform the given operations on
the stack:
Stackpush() – To insert a name of the person into the stack whose age is
greater than 18.
Stackpop() – To pop the elements from the stack and display them. Display
the message “Candidates eligible to vote” after displaying all the names.
37.A nested list contains the data of visitors in a museum. Each of the inner lists
contains the following data of a visitor:
[V_no (int), Date (string), Name (string), Gender (String M/F), Age (int)]
Write the following user defined functions to perform given operations on the
stack named "status":
(ii) Pop_element() - To Pop the objects from the stack and count the display
the number of Male and Female entries in the stack. Also, display “Done” when
there are no elements in the stack.
(ii) Pop(st) to pop all the elements from the stack st and display them. It
should also display the message 'Stack Empty' when the stack becomes empty.
For example:
If the expression is:
42*5.8*16/24-8+2
39.John 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.
Page 5 of 11
For Example:
If the sample Content of the list is as follows:
N=[12,13,34,56,21,79,98,22,35,38]
40.A linear stack called Emp contains the following information : Phone number
of Employee, Name of Employee. Write the following methods to perform given
operations on the stack:
(i) Push_element( ) To Push an object containing Phone number of Employee
and Name of Employee into the stack.
41.A list contains following record of a student: [Rno, Name, Dob, Class] Write
the following user defined functions to perform given operations on the stack
named ‘student’:
(i) Push_element() - To Push an record of student to the stack
(ii) 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 List_of_books contains
[“Wings of fire”, “Don Quixote”, “Alice's Adventures in Wonderland”,
“The Adventures of Huckleberry Finn”, ”The Adventures of Tom Sawyer”,
“Treasure Island”, ”Pride and Prejudice”, “Wuthering Heights”]
Treasure Island
The Adventures of Tom Sawyer
The Adventures of Huckleberry Finn
43.Jacob 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={"Hari":76, "Jai":45, "Shiv":89, "Anup":65, "Fin":90, "Tom":82}
Page 6 of 11
For example:
If the dictionary contains the following data:
Dbook={"Python":350,"Hindi":200,"English":270,"Physics":600,
“Chemistry”:550}
45.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
(a) Push the keys (name of the student) of the dictionary into a stack, where
the corresponding value (marks) is greater than 75.
(b) 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}
46.A dictionary stu contains rollno and marks of students. Two empty lists
stack_roll and stack_mark will be used as stack. Two functions push_stu()
and pop_stu() are defined and perfom following operations:
(a)Push_stu() : It reads dictionary stu and add keys into stack_roll and values
into stack_marks for all students who secured more than 60 marks.
(b)Pop_stu() : It removes last rollno and marks from both list and print
“underflow” if there is nothing to remove.
For example
stu={1:56,2:45,3:78,4:65,5:35,6:90}
For example:
If the dictionary Stu_dict contains the following data:
Stu_dict ={5:(87,68,89), 10:(57,54,61), 12:(71,67,90), 14:(66,81,80),
18:(80,48,91)}
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]
Write the following user defined functions to perform the following on a stack
i) Push(items): It takes the nested list as its argument and pushes a list object
containing itemno and itemname where stock is less than 10.
ii) Popitems(): It pops the objects one by one from the stack and also displays
a message ‘Stack empty’ at the end.
50.A list contains following record of a student: [student_name, age, hostel] Write
the following user defined functions to perform given operations on the stack
named ‘stud_details’:
(ii) 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:
[“Barsat”,17,”Ganga”] [“Ruben”, 16,”Kaveri”] [“Rupesh”,19,”Yamuna”]
Page 8 of 11
52.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 are no elements in the stack.
For example,
If the dictionary contains the following data:
Vehicle={“Santro”:”Hyundai”,”Nexon”:”TATA”,”Safari”:”Tata”}
(ii) Pop and display the content of the stack. The dictionary should be as
follows:
d={“Ramesh”:58, “Umesh”:78, “Vishal”:90, “Khushi”:60, “Ishika”:95}
55.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.
● Push the keys (name of the Department) of the dictionary into a stack, where
the corresponding value (Number of PC) is 25 or more.
Page 9 of 11
For example:
If the sample content of the dictionary is as follows:
SETUP={"HR":10, "QUALITY":25, "SUPPORT":50, "PRODUCTION":20,
"SUPPLY":25, }
57.Nivedita has started a new year’s resolution to read 12 non fiction books by
the end of 2022. For this purpose she has bought 20 such books. She stores
the data in a dictionary as book_name,rating as key, value pair. Help her out
a bit to organize her collection with help of Stack Data Structure. Write a
program, with separate user defined functions to perform the following
operations:
● Push the keys (book_name) of the dictionary into a stack, where the
corresponding value (rating) are more than 8.
For example:
If the sample content of the dictionary is as follows:
B={"AI 2041: Ten Visions for Our Future":7.9, "Beginners: The Transformative
Joy of Lifelong Learning":8.5, "Bravey: Chasing Dreams, Befriending Pain, and
Other Big Ideas":9, "Chatter: The Voice in Our Head, Why It Matters, and How
to Harness It":8.2, "The Code Breaker: Jennifer Doudna, Gene Editing, and the
Future of the Human Race":7.5}
58.Akash has a list containing 10 students marks . 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 higher than 33 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]
59.YASH MOTORS have a dictionary of top performer EMPLOYEES and their SALES
as key value pairs of COMPANY. Write a program, with separate user defined
functions to perform the following operations:
● Push the keys (name of the EMPLOYEE) of the dictionary into a stack, where
the corresponding value (SALES) is greater than 500000.
● Pop and display the content of the stack.
For example:
If the sample content of the dictionary is as follows:
SALES={"SUNIL":700000,"ROHIT":400000,"RAJEEV":350000,
"MAYANK":750000, "RAHUL":1000000}
Page 10 of 11
(ii)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 lisr 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’]
61.Two lists Lname and Lage contain name of person and age of person
respectively. A list named Lnameage is empty. Write functions as per details
given below:
(i) Push_na() – It will push the tuple containing pair of name and age from
Lname and Lage whose age is above 50 into the stack Lnameage.
(ii) Pop_na() – It will remove the last pair of name and age and also print name
and age of the removed person. It should also print “underflow” if there is
nothing to remove.
*****************
Page 11 of 11