0% found this document useful (0 votes)
38 views6 pages

CW - Files & Stack - CW With Solution

Uploaded by

mayukhnandi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

CW - Files & Stack - CW With Solution

Uploaded by

mayukhnandi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CLASS WORK ON STACK & FILE SYSTEM

1. 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
Ans.
travel= []
def Push_element(NList):
for L in NList:
if L[1] != "India" and L[2]<3500:
travel.append([L[0],L[1]])
def Pop_element():
while len(travel):
print(travel.pop())
else:
print("Stack Empty")
2. Mr.Ajay has created a list of elements. Help him to write a program in python with
functions,
PushEl(N) and PopEl() to add a new element and delete an element from a List of
element Description, considering them to act as push and pop operations of the
Stack data structure . Push the element into the stack only when the element is
divisible by 4.
For eg: if L=[2,5,6,8,24,32]
then stack content will be 32 24 8
Ans.
ST=[]
def PushEl(N):
for i in N:
if i%4 == 0:
ST.append(i)
print(ST)

def PopEl() :
while len(ST)!=0:
print(ST.pop(),end=" ")

3. Write a function in Python to read a text file, Alpha.txt and displays those lines which
begin with the word ‘You’.
Ans.
4. Write a function, vowelCount() in Python that counts and displays the number of
vowels in the text file named Poem.txt.
Ans.

5. 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.
Ans.
import csv
def Accept():
sid=int (input ("Enter Student ID "))
sname=input ("Enter Student Name ")
game= input ("Enter name of game ")
res=input ("Enter Result")
headings=["Student ID", "Student Name", " Game Name", "Result"]
L1=[sid, sname, game, res]
f=open('Result.csv','a', newline='\n')
data=csv.writer(f)
data.writerow(headings)
data.writerow(L1)
f.close()

def wonCount():
f=open('Result.csv', 'r')
data=csv.reader(f)
for i in data:
if i[3]=="Won":
print(i)
f.close()

6. 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.
Ans.
7. 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.
Ans.
f = open('CINEMA.dat','rb')
mtype=input('give mtype')
try:
while True:
data=pickle.load(f)
for i in data:
if(data[i][1]==mtype):
print(i,data[i][0])
except:
f.close()

8. 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.
Ans.

You might also like