G12 CS Worksheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SHREE NIKETAN PATASALA

CLASS XII - COMPUTER SCIENCE WORKSHEET


TOPIC: CSV FILES, STACK
DATE: 20.07.2024 Time: 1.5 Hrs
1. 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 ‘status’:
Push_element() - To Push an record of student 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.

2. A csv file "PLANTS.csv" has structure [ID, NAME, PRICE].


• WRITEREC() - to input data for records from the user and write them to
the file PLANTS.csv. Each record consists of a list with field elements as
ID, NAME and PRICE to store plant id, plant name and price
respectively.
• SHOWHIGH() - To read the records of PLANTS.csv and displays those
records for which the PRICE is more than 500.

3. Write a function in python, PushNames(Names) and PopNames(Names) to


add a new name and delete a name, considering them to act as push and
pop operations of the Stack data structure.

4. Write a function in Python PUSH(A), where A is a list of numbers. From


this list push all even numbers into a stack implemented by using a list.
Display the stack if it has at least one element, otherwise display an
appropriate error message.

5. 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 tasks.
(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}
Then, the output from the program should be:
TOM
ANU
BOB
OM

1
6. Write a python function CSVCOPY() to take sourcefile, targetfile as
parameter and create a targetfile and copy the contents of sourcefile to
targetfile.

7. Write a Python function SNAMES() to read the content of file emp.csv and
display the employee record whose name begins from “S” also show no. of
employee with first letter “S” out of total record.
Consider the following CSV file (emp.csv):
1, Peter, 3500
2, Scott, 4000
3, Harry, 5000
4, Michael, 2500
5, Sam, 4200
Output should be:
2, Scott, 4000
5, Sam, 4200
Number of names starting with “S” are: 2/5

8. Pushpak is writing a program to create a CSV file “item.csv” which will


contain item name, Price and quantity of some entries. He has written the
following code. As a programmer, help him to successfully execute the
given task.
import _______________ # Line 1
def addInCsv(item,price,qty): # to write / add data into the CSV file
f=open(' item.csv', '_____') # Line 2
csvFileWriter = csv.writer(f)
csvFileWriter.writerow([item,price,qty])
_________________ # Line 3
#csv file reading code
def readFromCsv(): # to read data from CSV file
with open(' item.csv','r') as csvFile:
newFileReader = csv.____________(csvFile) # Line 4
for row in newFileReader:
print (row[0],row[1],row[2])

addInCsv('Note Book',45,100)
addInCsv('Text Book',60,150)
addInCsv('Ball Pen',10,100)
addInCsv('Pencil', 5,200)
readFromCsv() #Line 5
(a) Name the module he should import in Line 1.
(b) Which mode should be mentioned in Line 2, to open the file to add
data into it?
2
(c) Fill in the blank in Line 3 to close the file.
(d) Fill in the blank in Line 4 to read the data from a csv file.
(e) Write the output he will obtain while executing Line 5.

9. Write a function in Python, Push(book) where, book is a dictionary


containing the details of a book in form of {bookno : price}. The function
should push the book in the stack which have price greater than 300.
Also display the count of elements pushed into the stack.
For example:
If the dictionary contains the following data:
Dbook={"Python":350, "Hindi":200, "English":270, "Physics":600,
“Chemistry”:550}
The stack should contain
Chemistry
Physics
Python
The output should be: The count of elements in the stack is 3

10. A file “Emp.csv”, contains the following details:


[Emp_id, Emp_name, Emp_salary].
Write a function RECSHOW() to display only those records who are
earning more than 7000.

You might also like