0% found this document useful (0 votes)
48 views11 pages

CS Practicals 11-17

The document describes programs to perform various operations using stacks and files in Python: 1. It provides code to implement a stack using a list data structure with functions to push, pop, peek and display elements. 2. Additional code samples demonstrate reading and writing to text and binary files, searching files, and updating records in a binary file. 3. Functions are defined to add and remove books from a stack to simulate push and pop operations.

Uploaded by

ankushrio83006
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)
48 views11 pages

CS Practicals 11-17

The document describes programs to perform various operations using stacks and files in Python: 1. It provides code to implement a stack using a list data structure with functions to push, pop, peek and display elements. 2. Additional code samples demonstrate reading and writing to text and binary files, searching files, and updating records in a binary file. 3. Functions are defined to add and remove books from a stack to simulate push and pop operations.

Uploaded by

ankushrio83006
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/ 11

11.

Write a Program that reads a text file and counts the number of occurences
of the words ‘me’ and ‘my’ (both in uppercase and loweercase).
SOURCE CODE:
file = open('Test4.txt','r')
n = file.read()
m = n.split()
ec = 0
yc = 0
for c in m:
if c in ['me','Me','mE','ME']:
ec+=1
elif c in ['my','My','mY','MY']:
yc+=1
else:
pass
print("No. of me in file: ",ec)
print("No. of my in file: ",yc)
file.close

Text4.txt
Everyone asks me how?
They ask me why?
But no one asked me Are you tired?
My life is full of surprises
My efforts are now tired

Sample Output
No. of me in file = 3
No. of my in file = 2
12. Write a program that reads text file and create a new file after adding ‘ing’
to all words ending with ‘t’,’p’,’d’
SOURCE CODE:
file1 = open(r"C:\UsersDesktop\file.txt",'r')
file2 = open(r'C:\Users\Desktop\file1.txt','w')
l,l1 = [],[]
while True:
a = file1.readline()
if not a:
break
b = a.split()
b.append("\n")
for i in b:
if i[-1] == 't' or i[-1] == 'p' or i[-1] == 'd':
i = i + "ing"
l.append(i)
for i in l:
i = i + ""
l1.append(i)
file2.writelines(i)
file1.close
file2.close

file.txt
Hey there!
Let
me up
my skills
now
End
File1.txt
Letting
13. Create a binary file with name and roll number. Search for a given roll
number and display the name. If not found display appropriate message
SOURCE CODE:
import pickle
d = {}
file = open("stu.dat",'wb')
n = int(input("Enter no. of students: "))
for i in range(n):
d['Roll'] = int(input("Enter roll no.: "))
d["Name"] = input("Enter name: ")
pickle.dump(d,file)
file.close()
x = int(input("Enter roll no. to be searched: "))
f = open("stu.dat",'rb')
k = {}
m=0
try:
while True:
k = pickle.load(f)
if k ["Roll"] == x:
m = m+1
except EOFError:
f.close()
if m == 0:
print("Student not found")

Sample Output
Enter no. of students: 3
Enter roll no.: 12343
Enter name: Em
Enter roll no.: 12337
Enter name: Aay
Enter roll no.: 12317
Enter name: Rosy
Enter roll no. to be searched: 13331
Student not found

14. Create a binary file with roll number, name and marks. Input a roll
number abd update the marks.
SOURCE CODE:
import pickle
d = {}
f = open("Stu1.dat","wb")
n = int(input("Enter no. of students: "))
for i in range (n):
d['Roll'] = int(input("Enter Roll no.: "))
d['Name'] = input("Enter Name no.: ")
d['Marks'] = int(input("Enter Marks: "))
pickle.dump(d,f)
f.close()
x = int(input("Enter roll no: "))
b = int(input("Enter updated marks: "))
with open("stu1.dat",'rb+')as f1:
r = f1.tell()
a = pickle.load(f1)
if d['Roll'] == x:
a['Marks'] = b
f1.seek(r)
pickle.dump(a,f1)
print(a)
else:
print("No such roll no. found!")
Sample Output
Enter no. of students: 2
Enter Roll no.: 123
Enter Name no.: Amy
Enter Marks: 39
Enter Roll no.: 124
Enter Name no.: Calub
Enter Marks: 41
Enter roll no: 123
Enter updated marks: 30
{'Roll': 123, 'Name': 'Amy', 'Marks': 30}

15. Create a csv file by entering user-id and password. Read and search
password for given user-id.
SOURCE CODE:
import csv
m = []
d = int(input("Enter no. of id's: "))
for i in range (d):
a = input("Enter user id: ")
b = input("Password: ")
l = [a,b]
m.append(l)
with open(r"C:\Users\Desktop\csvfile.csv",'w+',newline = "") as f:
cw = csv.writer(f)
cw.writerows(m)
f.seek(0)
read = csv.reader(f)
while True:
c = input("Do you want to search? Y/N: ")
if c == 'N' or c == 'n':
break
if c == 'Y' or c == 'y':
ud = input("Enter user id to be searched: ")
for i in read:
if i[0] == ud:
print("user exists")
print("Password: ",i[1])
elif i[0] != ud:
print("User doesn't exist")

Sample Output
Enter no. of id's: 2
Enter user id: jewel_head
Password: @its_me
Enter user id: ruby_23
Password: smile1212
Do you want to search? Y/N: Y
Enter user id to be searched: jewel_head
user exists
Password: @its_me
16. Write a Python program to implement a stack using a list data-structure
SOURCE CODE:
def isempty(stk):
if stk == []:
return True
else:
return False

def Push(stk,elt):
if isempty(stk):
print("Stack is empty...underflow case...")
else:
print("Deleted element is: ",stk.pop())

def Peek(stk):
if isempty(stk):
print("Stack is empty...")
else:
print("Element at top of the stack: ",stk[-1])

def Display(stk):
if isempty(stk):
print("Stack Empty...")
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
stack = []
while True:
print("....STACK OPERATION....")
print(" 1.PUSH")
print(" 2.POP")
print(" 3.PEEK")
print(" 4.DISPLAY")
print(" 5.EXIT")
ch = int(input("Enter your choice: "))
if ch == 1:
element = int(input("Enter the element which you want to push: "))
Push(stack,element)
if ch == 2:
pop(stack)
elif ch == 3:
Peek(stack)
elif ch == 4:
Display(stack)
elif ch == 5:
break

Sample Output
....STACK OPERATION....
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter your choice:1
Enter the element which you want to push: 2
Element,inserted!
....STACK OPERATION....
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter your choice:1
Enter the element which you want to push: 3
Element,inserted!
....STACK OPERATION....
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter your choice:2
Enter the element which you want to push: 3
Deleted element: 3
....STACK OPERATION....
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter your choice:3
Enter at top of stack: 2
....STACK OPERATION....
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter your choice:4
2
....STACK OPERATION....
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter your choice:5

17. Write PUSH(Book) and POP(Book) methods in python to add Books and
remove Books considering them to act as Push and Pop operations of stack
SOURCE CODE:
def POP(Book):
Book.pop()
return Book
def Push(Book):
stack.append(Book)
stack = []
while True:
print()
print("Enter your choice as per given- ")
print("For insterting data, Enter insert")
print("For deleting data, Enter delete")
print("To exit, Enter exit")
print()
user = input("Enter your choice: ")
if user == "insert":
Book = input("Enter name of Book: ")
Push(Book)
elif user == "delete":
if stack == []:
print("underflow")
else:
stack
else:
break
print("Now stack is: ",stack)

Sample Output
Enter your choice as per given-
For insterting data, Enter insert
For deleting data, Enter delete
To exit, Enter exit

Enter your choice: insert


Enter name of Book: Atomic Habits

Enter your choice as per given-


For insterting data, Enter insert
For deleting data, Enter delete
To exit, Enter exit

Enter your choice: insert


Enter name of Book: Abdul Kalam

Enter your choice as per given-


For insterting data, Enter insert
For deleting data, Enter delete
To exit, Enter exit

Enter your choice: delete

Enter your choice as per given-


For insterting data, Enter insert
For deleting data, Enter delete
To exit, Enter exit

Enter your choice: exit


Now stack is: ['Atomic Habits']

You might also like