0% found this document useful (0 votes)
45 views5 pages

Binary File and CSV File and Stack

The document outlines various programming tasks in Python related to CSV and binary file operations, including functions for adding, counting, and searching records. It also includes tasks for managing data using stacks, such as pushing and popping elements based on specific criteria. Each task requires the implementation of user-defined functions to manipulate and retrieve data from files and stacks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

Binary File and CSV File and Stack

The document outlines various programming tasks in Python related to CSV and binary file operations, including functions for adding, counting, and searching records. It also includes tasks for managing data using stacks, such as pushing and popping elements based on specific criteria. Each task requires the implementation of user-defined functions to manipulate and retrieve data from files and stacks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

XII COMPUTER SCIENCE

WORKSHEET

CSV FILE
1. 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 ‘record.csv’. Each record
consists of a list with field elements as empid, name and mobile to store employee id,
employee name and employee salary respectively.
(ii) COUNTR() – To count the number of records present in the CSV file named
‘record.csv’.

2. 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 ‘furdata.csv’. Each record
consists of a list with field elements as fid, fname and fprice to store furniture id, furniture
name and furniture price respectively.
(ii) search()- To display the records of the furniture whose price is more than 10000.

3. Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has
created a csv file named Result.csv, to store the results of students in different sports events.
The structure of Result.csv is:
[St_Id, St_Name, Game_Name, Result]
Where
St_Id is Student ID (integer)
ST_name is Student Name (string)
Game_Name is name of game in which student is participating(string)
Result is result of the game whose value can be either 'Won', 'Lost'
or 'Tie'
For efficiently maintaining data of the event, Vedansh wants to write the following user
defined functions:
(i)Accept() – to accept a record from the user and add it to the file Result.csv. The column
headings should also be added on top of the csv file.
(ii)wonCount() – to count the number of students who have won any event. As a Python
expert, help him complete the task.

4. A csv file "Happiness.csv" contains the data of a survey. Each record of the file contains
the following data:
● Name of a country
● Population of the country
● Sample Size (Number of persons who participated in the survey in
that country)
● Happy (Number of persons who accepted that they were Happy)
For example, a sample record of the file may be:
[‘Signiland’, 5673000, 5000, 3426]
Write the following Python functions to perform the specified operations on
this file:
(I)Read all the data from the file in the form of a list and display all
those records for which the population is more than 5000000.
(II)Count the number of records in the file.

5. Create a function maxsalary() in Python to read all the records from an already existing file
record.csv which stores the records of various employees working in a department. Data is
stored under various fields as shown below:

Function should display the row where the salary is maximum. Note: Assume that all
employees have distinct salary.

BINARY FILE
1. Aman is a Python programmer. He has to write a code and to a create binary file record.dat
with employeeid, ename and salary. The file should contain 10 records. He also has to update
a record based on the employee id entered by the user and update the salary. The updated
record is then to be written in the file temp.dat. The records which are not to be updated also
have to be written to the file temp.dat. If the employee id is not found, an appropriate
message should to be displayed. As a Python expert, help him to complete the following code
based on the requirement

2. Consider a file, SPORT.DAT, containing records of the following structure:


[SportName, TeamName, No_Players]
Write a function, copyData(), that reads contents from the file SPORT.DAT and copies the
records with Sport name as “Basket Ball” to the file named BASKET.DAT. The function
should return the total number of records copied to the file BASKET.DAT.

3. A Binary file, CINEMA.DAT has the following structure: {MNO:[MNAME, MTYPE]}


Where
MNO – Movie Number
MNAME – Movie Name
MTYPE is Movie Type
Write a user defined function, findType(mtype), that accepts mtype as parameter and displays
all the records from the binary file CINEMA.DAT, that have the value of Movie Type as
mtype.

4. Consider a binary file 'INVENTORY.DAT' that stores information about products using
tuple with the structure (ProductID, ProductName, Quantity, Price). Write a Python function
expensiveProducts() to read the contents of 'INVENTORY.DAT' and display details of
products with a price higher than Rs. 1000. Additionally, calculate and display the total
count of such expensive products.
For example: If the file stores the following data in binary format
(1, 'ABC', 100, 5000)
(2, 'DEF', 250, 1000)
(3, 'GHI', 300, 2000)
then the function should display
Product ID: 1
Product ID: 3
Total expensive products: 2

5. Consider a file FLIGHT.DAT containing multiple records. The structure of each record is
as shown below: [Fno, FName, Fare, Source, Destination] .Write a function COPY_REC() in
Python that copies all those records from FLIGHT.DAT where the source is DELHI and the
destination is MUMBAI, into a new file RECORD.DAT

6. Consider a Binary file BOOK.DAT containing a dictionary having multiple elements. Each
element is in the form BNO:[BNAME,BTYPE,PRICE] as key:value pair where
BNO – Book Number
BNAME – Book Name
BTYPE - Book Type
PRICE – Book price
Write a user-defined function, findBook(price), that accepts price as parameter and displays
all those records from the binary file BOOK.DAT which has a book price more than or equal
to the price value passed as a parameter.

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’:
(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 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 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 pushed
into 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

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

4. A) 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.
Write the following user-defined functions in Python to perform the specified operations on
the stack BooksStack:
(I) 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.
(II) 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".
(III) peep(BookStack): This function displays the topmost element of the stack without
deleting it. If the stack is empty, the function should display 'None'.

5. Write the definition of a user-defined function “push_even(N)” which accepts a list of


integers in a parameter “N” and pushes all those integers which are even from the list `N` into
a Stack named “EvenNumbers”. Write function pop_even() to pop the topmost number from
the stack and returns it. If the stack is already empty, the function should display "Empty".
Write function Disp_even() to display all element 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 `VALUES` are:
[10, 5, 8, 3, 12]
Then the stack `EvenNumbers` should store:
[10, 8, 12]

6. Given a Dictionary Stu_dict containing marks of students for three test-series in the form
Stu_ID:(TS1, TS2, TS3) as key-value pairs. Write a Python program with the following user-
defined functions to perform the specified operations on a stack named Stu_Stk
(i) Push_elements(Stu_Stk, Stu_dict) : It allows pushing IDs of those students, from the
dictionary Stu_dict into the stack Stu_Stk, who have scored more than or equal to 80 marks
in the TS3
Test.
(ii) Pop_elements(Stu_Stk): It removes all elements present inside the stack in LIFO order
and prints them. Also, the function displays 'Stack Empty' when there are no elements in the
stack. Call both functions to execute queries.
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)}
After executing Push_elements(), Stk_ID should contain
[5,12,14,18]
After executing Pop_elements(), The output should be:
18
14
12
5
Stack Empty

You might also like