Stacks & File Handling Worksheet-1 Class 12 CS
Stacks & File Handling Worksheet-1 Class 12 CS
2. i. Write a function in Python PUSH (Lst), where Lst is a list of numbers. From this list push all
numbers not divisible by 6 into a stack implemented by using a list. Display the stack if it has at
least one element, otherwise display appropriate error message.
ii. 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.
5. Write a method COUNTLINES() in Python to read lines from text file ‘TESTFILE.TXT’ and display
the lines which are not starting with any vowel.
Example:
If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.
The COUNTLINES() function should display the output as:
The number of lines which are not starting with any vowel is: 1
6. Write a function ETCount() in Python, which should read each character of a text file
“TESTFILE.TXT” and then count and displaythe count of occurrence of alphabets E and T
individually (includingsmall cases e and t too).
Example:
If the file content is as follows:
Today is a pleasant day.It might rain
today.
It is mentioned on weather sites
The ETCount() function should display the output as:
E or e: 6
T or t: 9
7. Write the following function definitions to perform the operations on a Stack:
FruitList=['Apple','Banana','Orange','Guava','Watermelon']
a. PEEK(fruitstk) function that displays the topmost Fruit in FruitList Stack and print STACK
EMPTY if there are no elements.
b. POP_DISP(fruitstk) function that deletes the top element and displays the Stack and print
STACK EMPTY if there are no elements.
8. Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the function.
The function returns another list named‘indexList’ that stores the indices of all Non-Zero Elements of
L.
For example:
If L contains [12,4,0,11,0,56]
The indexList will have - [0,1,3,5]
9. What will be stored in the file “School.csv” after the following code is executed?
data=[['DRSDelhi','Esha','Badminton'], ['BTSPatna','Abhi','Tennis']]
import csv
with open('School.csv','w+',newline='') as cfile:
wr=csv.writer(cfile)
wr.writerow(['School','Nickname','Sport'])
wr.writerows(data)
11. Deepan is a student of non-IT stream. However, he wants to learn Python also as it will be helpful to
him in his field. While learning Python, his teacher has assigned him an assignment to complete an
incomplete program given below.
The program intends to print from a csv file “People.csv” that stores comma separated fields as:
EmpID Fname Lname Phone
201 Vibhu Jain 295000
202 Nooh Kirk 610011
203 Haris Jamil 92055
204 Kulwant Singh 69700
The incomplete code given to Deepan is as follows:
import csv # Line 1
with open ( ________, ________, _________) as myfile: # Line 2
fin = csv. _______ ( myfile ) # Line 3
for row in _______ : # Line 4
print ( _______ ) # Line 5
a. Complete the Line 2 of the given code so that the file is opened in read mode for processing.
b. Complete Line 3 so that the csv file is read into the fin object.
c. Complete Line 4 & 5 so that records from the open csv file are printed line by line.
d. Modify Line 4 & 5 so that only the fourth field (Phone) from each record is printed.
e. Write the output obtained by running the code.
12. Ranjan Kumar of class 12 is writing a program to create a CSV file “user.csv” which will contain
user name and password for some entries. He has written the following code. As a programmer, help
him to successfully execute the given task.
13. Write a Python function/method Switch_Over(VAL) to swap the even and odd positions of the values
in the list VAL.
Note: Assume that the list has even number of values in it.
For example:
If the list contains:
[25,17,19,13,12,15]
After swapping the list it should be displayed as:
[17,25,13,19,15,12]
14. Write a function in Python PUSH (Lst), where Lst is a list of numbers. From this list push all
numbers not divisible by 6 into a stack implemented by using a list. Display the stack if it has at
least one element, otherwise display appropriate error message. 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.
15. Write a method in python to read lines from a text file “DIARY.TXT”, and display those lines, which
are starting with an alphabet ‘P’.
16. Write a method/function ABLINES() in python to read contents from a text file “LINES.TXT”, to
display those lines which are either starting with an alphabet ‘A’ or starting with an alphabet ‘B’.
For example:
If the file contains the following content:
A Boy is playing outside
The Playground is big
Banyan tree is in the ground
Your function should display the output as:
A Boy is playing outside
Banyan tree is in the ground
17. Write a method/function SHORTWORDS() in python to read lines from a text file
“WORDBANK.TXT” and display those words, which are lesser than 5 characters.
For example:
If the file contains the following content:
LOVE JOY PEACE
HOPE FAITH HOLY
HAPPY REJOICE
Your function should display the output as:
LOVE JOY
HOPE HOLY
18. Write a program to add records in pickle file ‘student.dat’ with the following fields (Using List):
[RollNo, Name, English, Maths, Science, Computer, Social, Total]
19. Write a function countINDIA() which read a text file ‘myfile.txt’ and print the frequency of the
words ‘India’ in it (ignoring case of the word).
20. Write a function countVowel() in Python, which should read each character of a text file “myfile.txt”
and then count and display the count of occurrence of vowels (including small cases and upper case).
Example: If the file content is as follows:
INDIA is my country. I live in India. India has many states.
The countVowel() function should display the output as:
Total number of vowels are: 20
21. Write a function lenFOURword(L), where L is the list of elements (list of words) passed as argument
to the function. The function returns another list named ‘indexList’ that stores the indices of all four
lettered word of L.
For example:
If L contains [“DINESH”, “RAMESH”, “AMAN”, “SURESH”, “KARN”]
The indexList will have [2, 4]
22. A list contains following record of a student:
[StudentName, Class, Section, MobileNumber]
Write the following user defined functions to perform given operations on the stack named ‘XIIA’:
(i) pushElement() - To Push an object containing name and mobile number of students who
belong to class XII and section ‘A’ to the stack
(ii) popElement() - 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 students details are:
[“Rajveer”, “99999999999”,”XI”, “B”]
[“Swatantra”, “8888888888”,”XII”, “A”]
[“Sajal”,”77777777777”,”VIII”,”A”]
[“Yash”, “1010101010”,”XII”,”A”]
23. Write a function in Python, Push(SItem) where, SItem is a dictionary containing the details of
stationary items– {Sname:price}. The function should push the names of those items in the stack
who have price greater than 25. Also display the count of elements pushed into the stack.
For example: If the dictionary contains the following data:
Ditem = {“Rubber”:5, "Pencil":5, "Pen":30, "Notebook": 60, "Eraser":5, “Watch”: 250}
The stack should contain
Pen
Notebook
Watch
The output should be: The count of elements in the stack is 3
24. Write a Program in Python that defines and calls the following user defined functions:
(i) ADD() – To accept and add data of a teacher to a CSV file ‘teacher.csv’. Each record consists
of a list with field elements as tid, name and mobile to store teacher id, teacher name and
teacher mobile number respectively.
(ii) COUNTRECORD() – To count the number of records present in the CSV file named
‘teacher.csv’.
25. Give any one point of difference between a binary file and a csv file. Write a Program in Python that
defines and calls the following user defined functions:
(i) add() – To accept and add data of an employee to a CSV file ‘employee.csv’. Each
record consists of a list with field elements as eid, name and salary to store employee id,
employee name and employee salary respectively.
(ii) search()- To display the records of the employee whose salary is more than 40000.
26. Write the following user defined functions to perform given operationson 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 noelements in the stack.
For example:
If the lists of customer details are:
[“Gurdas”, “99999999999”,”Goa”]
[“Julee”, “8888888888”,”Mumbai”]
[“Murugan”,”77777777777”,”Cochin”]
[“Ashmit”, “1010101010”,”Goa”]
27. Write a function in Python, Push (SItem), where SItem is a dictionary containing the details of
stationary items – {Sname:price}. The function should push the names of those items in the stack
who have price greater than 75. Also display the count of elements pushedinto the stack.
For example:
If the dictionary contains the following data:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain
Notebook
Pen
The output should be:
The count of elements in the stack is 2
28. Write a function in Python named CHANGE (array, size) which accepts an integer array (list) and its
size as arguments and replaces elements having even values with its half and elements having odd
values with twice its value.
Example: If an array (list) of five elements initially contains the elements as:
3,4,5,16,9
then the function should rearrange the content of the array (list) as:
6,2,10,8,18
29. Write the definition of a function Grace_Score (score, size) in Python, which should check all the
elements of the array and give an increase of 5 to those scores which are less than 40.
Example: if an array of seven integers is as follows:
45, 35, 85, 80, 33, 27, 90
After executing the function, the array content should be changed as follows:
45, 40, 85, 80, 38, 32, 90
30. Write a function, lenWords(STRING), that takes a string as an argument and returns a tuple containing length
of each word of a string.
For example, if the string is "Come let us have some fun", the tuple will have (4, 3, 2, 4, 4, 3)