You are on page 1of 22

P.M.

S PUBLIC
SCHOOL

SESSION: 2022-23
CBSE (CENTRAL BOARD OF SECONDARY
EDUCATION)

COMPUTER SCIENCE
“Programming File + SQL”

Submitted By: Submitted to:


Luv Tandon Mr. Ashwani Sir
12th Sc.(B)
INDEX:
WAP to sort a list using Bubble Sort 1

WAP to sort a list using Insertion Sort 2

WAP to use Linear search in a list 3

WAP to use Binary search in a list 4-5

WAP to show Implementation of a 2-D List (Sum of all 5-6


elements in a matrix of order m X n)
WAP to create a text file 6-7

WAP to count number of lines in the text file 7

WAP to count number of lines starting with uppercase 8


in text file
WAP to count number of words starting with 8-9
lowercase vowel in text file
WAP to count number lowercase and uppercase 9
letters in text file
WAP to create a binary file by writing product no., 10
name and price
WAP to search any product in binary file using product 11
no.
WAP to count no. of products with price more than 12
100
WAP to update price of the product whose number is 13
entered by user
WAP to copy details of all the products having price 14
more than 100
WAP to create a CSV File with roll no., name and 15
marks of ‘n’ students
WAP to count no. of records having marks more than 16
90
WAP to show implementation of stack 17-19
# WAP to sort a list using Bubble Sort:
CODE:
l = []
n = int(input("Enter length of the list"))
for i in range(n):
e = int(input(f"Enter {n + 1} element:"))
l.append(e)
print("List entered is:",l)
for i in range(n):
for j in range(n-i-1):
if l[j]>l[j+1]:
l[j],l[j+1] = l[j+1],l[j]
print("Sorted list: ",l)

OUTPUT:

1
# WAP to sort a list using Insertion
Sort:
CODE:
l = []
n = int(input("Enter the length of list: "))
for i in range(n):
e = int(input("Enter the element: "))
l.append(e)
print("List entered is: ",l)
for i in range(1, n):
key = l[i]
j = i-1
while j >= 0 and key < l[j]:
l[j+1] = l[j]
j = j-1
else:
l[j+1] = key
print("List after using insertion sort: ", l)

OUTPUT:

2
# WAP to use Linear search in a list:
CODE:
def l_search(l, ele):
n = len(l)
found = False
for i in range(n):
if l[i] == ele:
print("Element found at index: ",i)
print("Element found at position: ", i + 1)
found = True
if not found:
print("Element not found")
L = []
n=int(input("Enter number of elements in the list: "))
for i in range(n):
ele = int(input("Enter element: "))
L.append(ele)
print("List created: ",L)
s=int(input("Enter element you want to search: "))
l_search(L, s)

OUTPUT:

# WAP to use Binary search in a list:


3
CODE:
def binary_search(l,x):
beg = 0
end = len(l) - 1
found = "false"
while beg <= end:
mid = (beg + end)//2
if x == l[mid]:
return mid
elif x > l[mid]:
beg = mid + 1
else:
end = mid - 1
else:
return found
l=[]
n=int(input("Enter length of the list: "))
for i in range(n):
x=int(input("Enter element: "))
l.append(x)
l.sort()
print("List created: ", l)
x = int(input("Enter element to be searched: "))
p = binary_search(l,x)
if p == "false":
print("Element not found.")
elif p != "false":
print(f"Element found at index: {p} and at position: {p + 1}")

OUTPUT:

4
# WAP to show Implementation of a 2-
D List (Sum of all elements in a matrix
of order m X n):
CODE:
s=0
a = []
m = int(input("Enter no. of rows: "))
n = int(input("Enter no. of columns: "))
for i in range(m):
r = []
for j in range(n):
x=int(input(f"Enter Element in {i} row and {j} column: "))
r.append(x)
a.append(r)
print("Entered list: ")
for i in range(m):
print(" ")
for j in range(n):
s += a[i][j]
print(a[i][j], end=" ")
print(" ")
print("Sum of all elements:",s)
OUTPUT:
5
# WAP to create a text file:
CODE:
f = open("text_file.txt",'w')
ch = 'y'
while ch=='y'.casefold():
d = input("Enter data to write on file:\n")
s = d + "\n"
f.write(s)
ch = input("Do you want to enter more data?\nEnter 'y' to continue.")
f.close()
f = open("text_file.txt",'r')
r = f.read()
print("The text file contains: ",r)
f.close()

OUTPUT:

6
# WAP to count number of lines in the
text file:
CODE:
f = open("text_file.txt",'r')
x = f.readlines()
s = len(x)
print("No. of lines present in file: ", s)
f.close()

OUTPUT:

7
# WAP to count number of lines
starting with uppercase in text file:
CODE:
c=0
f = open("text_file.txt",'r')
ch = f.readline()
while ch:
if ch[0].isupper():
c += 1
ch = f.readline()
print("No. of lines starting with uppercase letters are: ", c)
f.close()

OUTPUT:

# WAP to count number of words


starting with lowercase vowel in text
file:
CODE:
c=0
f = open("text_file.txt",'r')
x = f.read()
words = x.split()
for i in words:
if i[0] in ['a','e','i','o','u']:
c=c+1
print("No. of words starting with lowercase vowels are: ", c)
f.close()

8
OUTPUT:

# WAP to count number lowercase


and uppercase letters in text file:
CODE:
c1 = 0
c2 = 0
f = open("text_file.txt",'r')
x = f.read()
for j in x:
if j.islower():
c1 = c1 + 1
elif j.isupper():
c2 = c2 + 1
print("No. of lowercase letters: ", c1)
print("No. of uppercase letters: ", c2)
f.close()

OUTPUT:

9
# WAP to create a binary file by
writing product no., name and price:
CODE:
import pickle
f = open("Record.dat",'wb')
ch = 'y'
while ch == 'y' or ch == 'Y':
d = {}
d['P_no.'] = int(input("Enter product no.: "))
d['P_name'] = input("Enter Product Name: ")
d['Price'] = float(input("Enter Price: "))
pickle.dump(d,f)
ch = input("Enter Y to enter more data\nEnter N to exit:")
f.close()
f = open("Record.dat",'rb')
try:
while True:
x = pickle.load(f)
print(x)
except EOFError:
f.close()

OUTPUT:

10
# WAP to search any product in binary
file using product no.:
CODE:
import pickle
f = open("Record.dat",'rb')
y = int(input("Enter product no. for searching: "))
try:
while True:
l=pickle.load(f)
if l['P_no.'] == y:
print(l)
except EOFError:
f.close()

OUTPUT:

11
# WAP to count no. of products with
price more than 100:
CODE:
import pickle
c=0
f = open("Record.dat",'rb')
try:
while True:
x=pickle.load(f)
if x['Price'] > 100:
c += 1
except EOFError:
print("No. of products more than 100 are: ", c)
f.close()

OUTPUT:

12
# WAP to update price of the product
whose number is entered by user:
CODE:
import pickle
f = open("Record.dat",'rb')
w = int(input("Enter pr no. for the product to be updated: "))
y = float(input("Enter new price: "))
try:
print("Product details after updating: ")
while True:
x=pickle.load(f)
if x['P_no.'] == w:
x['Price'] = y
print(x)
except EOFError:
f.close()

OUTPUT:

13
# WAP to copy details of all the
products having price more than 100:
CODE:
import pickle
f=open("Record.dat", 'rb')
f1=open("temp.dat", 'wb')
try:
while True:
x=pickle.load(f)
if x['Price'] > 100:
pickle.dump(x, f1)
except EOFError:
f.close()
f1.close()
f1 = open("temp.dat", 'rb')
try:
print("File having details of products with price more than 100: ")
while True:
x=pickle.load(f1)
print(x)
except EOFError:
f1.close()

OUTPUT:

14
# WAP to create a CSV File with roll
no., name and marks of ‘n’ students:
CODE:
import csv
f = open("Student.csv",'w')
w = csv.writer(f)
w.writerow(['Roll_no.','Name','Marks'])
n=int(input("Enter no. of records to be added in file: "))
for i in range(n):
r = int(input("Enter Roll no.: "))
n = input("Enter name: ")
m = float(input("Enter marks: "))
s = [r,n,m]
w.writerow(s)
f.close()
with open("Student.csv", 'r', newline = '\r\n') as f:
r=csv.reader(f)
for x in r:
print(x)

OUTPUT:

15
# WAP to count no. of records having
marks more than 90:
CODE:
import csv
f = open("Student.csv",'w')
w = csv.writer(f)
w.writerow(['Roll_no.','Name','Marks'])
n=int(input("Enter no. of records to be added in file: "))
for i in range(n):
r = int(input("Enter Roll no.: "))
n = input("Enter name: ")
m = float(input("Enter marks: "))
s = [r,n,m]
w.writerow(s)
f.close()
with open("Student.csv", 'r', newline = '\r\n') as f:
r=csv.reader(f)
for x in r:
print(x)
OUTPUT:

16
# WAP to show implementation of
stack:
CODE:
def isempty(stk):
if stk == []:
return True
else:
return False
def push(stk, item):
stk.append(item)
top=len(stk)-1
def pop(stk):
if isempty(stk):
return "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def peek(stk):
if isempty(stk):
return "underflow"
else:
top=len(stk)-1
return stk[top]
def display(stk):
if isempty(stk):
print("stack empty")
else:
top=len(stk)-1
print(":Elements in stack are:")
for a in range(top,-1,-1):
print(stk[a])

17
stack=[]
top=None
while True:
print("stack operations")
print("1. Push \t 2. pop \t 3. peek \t 4. display stack \t 5. exit")
ch=int(input("enter your choice(1-5)"))
if ch==1:
item=int(input("enter item:"))
push(stack,item)
elif ch==2:
item=pop(stack)
if item=="underflow":
print("stack is empty")
else:
print("popped item is",item)
elif ch==3:
item=peek(stack)
if item=="underflow":
print("stack is empty")
else:
print("topmost item is",item)
elif ch==4:
display(stack)
elif ch==5:
break
else:
print("invalid choice")

18
OUTPUT:

19

You might also like