0% found this document useful (0 votes)
95 views10 pages

CSV Practice

The document discusses questions and answers related to queue data structures. It defines a queue as a first-in, first-out structure where elements are added to the rear and removed from the front. The major queue operations are listed as insertion, deletion, traversal, and searching. Algorithms for queue insertion and deletion are provided using pseudocode. Python code examples are given for implementing queue operations on cities, places, customers, and fruits. Finally, important questions related to reading, writing, searching, and modifying CSV files are discussed.
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)
95 views10 pages

CSV Practice

The document discusses questions and answers related to queue data structures. It defines a queue as a first-in, first-out structure where elements are added to the rear and removed from the front. The major queue operations are listed as insertion, deletion, traversal, and searching. Algorithms for queue insertion and deletion are provided using pseudocode. Python code examples are given for implementing queue operations on cities, places, customers, and fruits. Finally, important questions related to reading, writing, searching, and modifying CSV files are discussed.
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/ 10

POSSIBLE BOARD DATA STRUCTURES- QUEUE QUESTIONS

1. Define Queue.
Answer:
Queue is a first-in, first-out (FIFO) data structure, i.e. the element added first to the queue
will be the one to be removed first. Elements are always added to the rear of the queue
and removed from the front of the queue.

2. Write all the operations possible in data structure.


Answer:
The major operations are:

1.Traversal

2.Insertion

3. Deletion
4. Searching

1. What are the two major queue operations?


Answer:
Addition of element is known as INSERT operation, also known as enqueuing. It is done
using rear terminal position, i.e. tail end. Removal of element is known as DELETE
operation, also known as dequeueing.

2. Write an algorithm to implement insertion operation of queue.


Answer:

1. Start
2. Check FRONT and REAR value,
1. if both the values are -1, then FRONT and REAR are incremented
by 1
2. other wise Rear is incremented by one.
3. queue [Rear]=new element.
4. Stop

Question 4:
Write an algorithm to implement deletion operation of queue.
Answer:

1. Start
2. Check for underflow situation by checking value of Front=-1
1. If it is display appropriate message and stop
2. Otherwise Deleted item=queue [Front]
3. If Front=Rear then Front=Rear=-1 Otherwise Front is incremented by one
4. Print “Item Deleted”
5. Stop
Question 1:
Write Insert (city) and Delete (City) methods in Python to add City and REmOve City
considering them to act as Insert and Delete operations of the data structure Queue.
Answer:

city=[ ]
def Insert (city):
a = raw_input(“Enter city”)
city. append (a)
def Delete (city):
if (city = = []):
print “Queue empty”
else:
print ( “Deleted element is”,city.pop[0])

Question 5:
Write Insert (Place) and Delete (Place) methods in Python to be add Place and Remove Place
considering them to act as Insert and Delete operations of the data structure Queue.
Answer:
place=[]
def Insert (place):
a=raw_input (“Enter city”)
place. append (a)
def delete (place):
if (place = = []):
print “Queue empty”
else:
print (“Deleted element is”, place.pop [0])

Question 6:

Write a function to add any customer’s information to queue.


Answer:
def insert(queue):
customer=[]
print “QUEUE BEFORE INSERT” display(queue)
customer.append(input(“Enter customer number?’’))
customer.append(raw_input(“Enter customer name”))
customer.append(input(“Enter customer phone number”))
queue. append(customer)
def display (queue):
l=len(queue)
for i in range(O,l):
print queue[i]

TRY THIS:
1. Write Add(Fruit) and Remove (Fruit) methods in Python to insert name of a
Fruit and to delete name of a Fruit considering them to act as Insert and Delete
operations of the data structure Queue.
2. Write the deletion and display operation of queue containing characters IN A
LIST.
3. Write the deletion and display operation of queue containing numbers.

IMPORTANT QUESTIONS OF CSV FILE

1.Write a program to read entire data from


file data.csv
import csv
f=open("data.csv", 'r')
d=csv.reader(f)
for row in d:
print(row)
OUTPUT:
['Admno', 'Name', 'Class', 'Sec', 'Marks']
['1231', 'Amit', 'XII', 'A', '45']
['1224', 'Anil', 'XII', 'B', '49']
['1765', 'Suman', 'XI', 'A', '42']
['2132', 'Naman', 'XII', 'C', '38']

OR
We can also write the program in the following way

import csv
with open("data.csv",'r') as f:
d=csv.reader(f)
for row in d:
print(row)
2. Write a program to search the record from
“data.csv” according to the admission
number input from the user. Structure of
record saved in “data.csv” is Adm_no, Name,
Class, Section, Marks
import csv
f = open("data.csv",'r')
d=csv.reader(f)
next(f) #To Skip Header Row

adm = int(input("Enter admission number"))


for row in d:
if int(row[0])==adm:
print("Adm no = ", row[0])
print("Name = ", row[1])
print("Class = ", row[2])
print("Section = ", row[3])
print("Marks = ", row[4])
break
else:
print("Record Not Found")

OUTPUT :
Enter admission number1231
Adm no = 1231
Name = Amit
Class = XII
Section = A
Marks = 45
3.Write a program to add/insert records in
file “data.csv”. Structure of a record is roll
number, name and class.
import csv
field = ["Roll no" , "Name" , "Class"]
f = open("data.csv" , 'w')
d=csv.writer(f)
d.writerow(field)
ch='y'
while ch=='y' or ch=='Y':
rn=int(input("Enter Roll number: "))
nm = input("Enter name: ")
cls = input("Enter Class: ")
rec=[rn,nm,cls]
d.writerow(rec)
ch=input("Enter more record??(Y/N)")
f.close()

4. Write a program to copy the data from


“data.csv” to “temp.csv”
import csv
f=open("data.csv","r")
f1=open("temp.csv",'w')
d=csv.reader(f)
d1=csv.writer(f1)
for i in d:
d1.writerow(i)
f.close( )
f1.close( )
5.Write a program to read all content of
“student.csv” and display records of only
those students who scored more than 80
marks. Records stored in students is in
format : Rollno, Name, Marks

import csv
f=open("student.csv","r")
d=csv.reader(f)
next(f)
print("Students Scored More than 80")
print()
for i in d:
if int(i[2])>80:
print("Roll Number =", i[0])
print("Name =", i[1])
print("Marks =", i[2])
print("--------------------")
f.close( )

OUTPUT
Students Scored More than 80

Roll Number = 1
Name = Amit
Marks = 81
--------------------------
Roll Number = 2
Name = Suman
Marks = 85
-------------------------
Roll Number = 4
Name = Deepika
Marks = 89
-------------------------

6.Write a program to display all the records


from product.csv whose price is more than
300. Format of record stored in product.csv is
product id, product name, price,.
import csv
f=open("product.csv" , "r")

d=csv.reader(f)

next(f)

print("product whose price more than 300")

print()

for i in d:

if int(i[2])>300:

print("Product id =", i[0])

print("Product Name =", i[1])

print("Product Price =", i[2])

print("--------------------")

f.close( )

OUTPUT:
product whose price more than 300

Product id =2
Product Name = Mouse
Product Price = 850
---------------------------------
Product id =3
Product Name = RAM
Product Price = 1560
--------------------------------
7.Write a program to calculate the sum of all
the marks given in the file “marks.csv.
Records in “marks.csv” are as follows :
Rollno, Name, Marks
1, Suman, 67
2, Aman,71
3, Mini, 68
4, Amit, 80

import csv
f=open("marks.csv","r")
d=csv.reader(f)
next(f)
s=0
for i in d:
s=s + int(i[2])
print("Total Marks are " ,s)
f.close( )

8.Write a program to count number of


records present in “data.csv” file.
import csv
f = open("data.csv" , "r")
d = csv.reader(f)
next(f) #to skip header row
r=0
for row in d:
r = r+1
print("Number of records are " , r)
f.close()

9.Write a program to modify the record of a


student in a file “data.csv”. Following records
are saved in file.
Rollno, Name, Marks
1, Aman, 35
2, Kanak, 1
3, Anuj, 33
4, suman, 25

import csv
import os
f=open("data.csv","r")
f1 = open("temp.csv","w")
d=csv.reader(f)
d1=csv.writer(f1)
next(f)
s=0
rollno = int(input("Enter roll number :"))
mn=input("Enter modified name :")
mm = int(input("Enter modified marks :"))
mr=[rollno,mn,mm]
header =["Rollno", "Name", "Marks"]
d1.writerow(header)
for i in d:
if int(i[0])==rollno:
d1.writerow(mr)
else:
d1.writerow(i)
os.remove("data.csv")
os.rename("temp.csv","data.csv")
f.close()
f1.close()

10.Write a program to show the detail of the


student who scored the highest marks. Data
stored in “Data.csv” is given below :
Rollno, Name, Marks
1, Aman, 35
2, Kanak, 1
3, Anuj, 33
4, suman, 25

import csv

f=open("data.csv","r")

d=csv.reader(f)

next(f)

max=0

for i in d:

if int(i[2])>max:

max=int(i[2])

f.close()

f=open("data.csv","r")

d=csv.reader(f)

next(f)

for i in d:

if int(i[2])==max:

print(i)

f.close()

X
STD 11TH 12TH THEOR TOTAL
MAR MAR MAR Y PRACTICAL/ MARK
K K K TOTAL INTERNAL S

You might also like