Practical Questions For Record

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

PRACTICAL QUESTIONS BINARY FILES, CSV FILES AND STACKS

SN PROGRAM
O
1 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.
SOLUTION:

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 number of records copied to the file BASKET.DAT.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 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:
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.
wonCount() – to count the number of students who have won any event. As a
Python expert, help him complete the task.
SOLUTION:

4 Write a Program in Python that defines and calls the following user defined
functions:
a) 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.
b) COUNTR() – To count the number of records present in the CSV file named
‘record.csv’.
SOLUTION:
5 Write a Program in Python that defines and calls the following user defined
functions:
a) 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.
b) search()- To display the records of the furniture whose price is more than 10000.
SOLUTION:

6 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.
SOLUTION:
7 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.
SOLUTION:

8 Write a Python program to implement a stack using list.


Solution:
stack=[]
choice="y"
while (choice=="y"):
print("1.Push")
print("2.Pop")
print("3.Show")
your_choice=int(input("Enter your choice"))
if (your_choice==1):
num=input("Input any number")
stack.append(num)
elif(your_choice==2):
if (stack==[]):
print("Stack is empty")
else:
print("The deleted element is:",stack.pop())
elif (your_choice==3):
l=len(stack)
for i in range(l-1,-1,-1):
print(stack[i])
else:
print("Wrong choice selected")
choice=input("Do you want to continue")
9 Write a program to create a stack for storing only odd numbers out of all the
numbers entered by the user.
Display the content of the stack along with the largest odd number in the stack.
(Hint: Keep popping out the elements from stack and maintain the largest element
retrieved so far in a variable. Repeat till stack is empty)
SOLUTION:

10 Write a random number generator that generates random numbers between 1 and 6
(simulates a dice).
Solution:

import random
min = 1
max = 6
roll_again = "y"
while roll_again == "y" or roll_again == "Y":
print("Rolling the dice...")
val = random.randint (min, max)
print("You get... :", val)
roll_again = input("Roll the dice again? (y/n)...")

You might also like