term1 CS 12th project
term1 CS 12th project
Roll No:-
CERTIFICATE
Name:-PALAK AGROYA
Class:-12TH A
Roll No:-
INDEX
S.No Practical Questions Remark/Sign
1 Write a python program to print a square multiplication
table.
2 Write a python program to find a string is a palindrome or
not.
3 Write a python program to calculate simple interest using a
function interest() that can receive principal amount, time
and rate and returns calculated simple interest. Do specify
default value for rate and time as 10% and 2 years
respectively.
4 Write a python program to get student data (roll no.. name
and marks) from user and write onto a binary file. The
program should be able to get data from the user and write
onto the file as long as the user wants.
def interest(principal,time=2,rate=0.10):
return principal*rate*time
prin=float(input('enter principal amount :'))
print('simple interest with default ROI and time value is :')
si1=interest(prin)
print('Rs.',si1)
roi=float(input("enter rates of interest(ROI):"))
time=int(input("enter time in years :"))
print('simple interest with your provided ROI and time value is :')
si2=interest(prin,time,roi/100)
print("rs.",si2)
Q4. Write a python program to get student data (roll no.. name and marks) from user and
write onto a binary file. The program should be able to get data from the user and write
onto the file as long as the user wants.
import pickle
stu={}
stufile = open( 'Stu.dat' , 'wb' )
ans= 'y'
while ans == 'y' :
rno = int(input("Enter roll number :"))
name = input ("Enter name :")
marks = float(input("Enter marks :"))
stu[ 'Rollno'] = rno
stu['Name'] = name
stu['Marks'] = marks
pickle.dump (stu, stufile)
ans = input ("Want to enter more records? (y/n)...")
stufile.close()
Q5. Write a python program to read a text file line by line and display each word separated
by a “#’.
Myfile=open("Answer.txt" , "r")
line=" "
while line:
line=Myfile.readline( )
for word in line.spilt( ) :
print(word , end= '#')
print()
myfile.close()
Q6. Write a python program to read a text file and display tha count of vowels and
consonants in the file.
myfile = open("Answer.txt" , "r")
ch = " "
vcount = 0
ccount= 0
while ch:
ch = myfile.read(1)
if ch in [ 'a' ,'A' , 'e' , 'E' , 'i' , 'I' , 'o' , 'O' , 'u' , 'U' ] :
vcount = vcount+1
else :
ccount= ccount+1
print("Vowels in the file :", vcount)
print("Consonants in the file :" , ccount)
myfile.close()
Q7. Write a python program that copies a text file “source.txt” onto “target.txt” barring
the lines starting with a “@” sign.
def filter(Answer, newfile) :
fin = open(Answer, "r")
fout = open(newfile, "w")
while True :
text = fin.readline( )
if len(text) == 0:
break
if text[0] == "@":
continue
fout.write(text)
fin.close( )
fout.close( )
filter("Answer.txt", "newfile.txt")
Q8. Write a python program to print string backward using recursion function.
def bp(sg,n):
if n>0:
print(sg[n],end='')
bp(sg,n-1)
elif n==0:
print(sg[0])
#__main__
s=input("Enter a string:")
bp(s,len(s)-1)
Q9.Wrie a python Program using a recursive function to print Fibonacci series upto nth
term.
def fib(n) :
if n == 1 :
return 0
elif n == 2 :
return 1
else :
return fib( n-1 ) + fib( n-2 )
#_main_
n = int(input("Enter last term required :"))
for i in range(1, n+1):
print(fib(i), end = ',')
print("...")
Q10. Write a python program recursive function to implement binary search algorithm
using python.
def binsearch(ar,key,low,high):
if low > high:
return -999
mid=int((low+high)/2)
if key==ar[mid]:
return mid
elif key <ar[mid]:
high=mid-1
return binsearch(ar,key,low,high)
else:
low=mid+1
return binsearch(ar,key,low,high)
#__main__
ary=[12,15,21,25,28,32,33,36,43,45]
item=int(input("Enter search item:"))
res=binsearch(ary,item,0,len(ary)-1)
if res>=0:
print(item,"FOUND at index",res)
else:print("Sorry!",item,"NOT FOUND in array")
Q11. Write a python program in python for linear Searching in an array(linear list).
def Lsearch(AR, ITEM) :
i=0
while i<len(AR) and AR[i] != ITEM :
i+=1
if i<len(AR):
return i
else :
return False
#-----main-----
N = int(input("Enter desired linear-list size(max.50)..."))
print("\nEnter elements for Linear Li\n")
AR=[0]*N
for i in range(N) :
AR[i] = int(input ("Elements" + str(i)+":"))
ITEM=int(input("\nEnter Element to be searched for..."))
index=Lsearch(AR,ITEM)
if index:
print("\nElement found at index:",index,", position:", (index+1))
else :
print("\nSorry!! Given elements couldn't not be found.\n")
Q12. Write a python program to implement stack operations.
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(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
#__main__
Stack=[]
top=None
while True:
print("STACK OPERATIONS")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display stack")
print("5. Exit")
ch=int(input("Enter your choice(1-5):"))
if ch==1:
item=int(input("Enter item:"))
Push(Stack, item)
elifch==2:
item=Pop(Stack)
if item=="Underflow":
print("Underflow! Stack is empty!")
else:
print("Popped item is",item)
elifch==3:
item=Peek(Stack)
if item=="Underflow":
print("Underflow! Stack is item!")
else:
print("Topmost item is",item)
elif ch==4:
Display(Stack)
elif ch==5:
break
else:
print("Invalid choice!")
Q13. Program to implement Queue Operations.
def cls():
print("\n"*100)
def isEmpty(Qu):
if Qu==[]:
return True
else:
return False
def Enqueue(Qu, item):
Qu.append(item)
if len(Qu)==1:
front=rear=0
else:
rear=len(Qu)-1
def Dequeue(Qu):
if isEmpty(Qu):
return "Underflow"
else:
item=Qu.pop(0)
if len(Qu)==0:
front=rear=None
return item
def Peek(Qu):
if isEmpty(Qu):
return "Underflow"
else:
front=0
return Qu[front]
def Display(Qu):
if isEmpty(Qu):
print("Queue Empty!")
elif len(Qu)==1:
print(Qu[0],"<==front,rear")
else:
front=0
rear=len(Qu)-1
print(Qu[front],"<-front")
for a in range(1,rear):
print(Qu[a])
print(Qu[rear],"<-rear")
#__mian__program
queue=[]
front=None
while True:
cls()
print("QUEUE OPERATIONS")
print("1. Enqueue")
print("2. Dequeue")
print("3. Peek")
print("4. display queue")
print("5.Exit")
ch=int(input("Enter your choice(1-5):"))
if ch==1:
item=int(input("Enter item:"))
Enqueue(queue, item)
input("Press Enter to continue...")
elif ch==2:
item = Dequeue(queue)
if item=="Underflow":
print("Under flow! Queue is empty!")
else:
print("Dequeeue-ed item is ",item)
input("press Enter to control..")
elif ch==3:
item=Peek(queue)
if item=="Underflow":
print("Queue is empty!")
else:
print("Frontmost item is",item)
input("press Enter to continue...")
elif ch==4:
Display(queue)
input("Press Enter to continue...")
elif ch==5:
break
else:
print("Invalid choice!")
input("Press Enter to continue...")
Q14. Write a python program to reverse a string using stack.
def push (stack,item) :
stack.append(item)
def pop(stack):
if stack==[]:
return
return stack.pop()
def reverse(string):
n = len(string)
stack = [ ]
for i in range(n):
push(stack, string[i])
string = " "
for i in range(n):
string += pop(stack)
return string
string= "This is it"
print("String:" , string)
reversedStr = reverse(string)
print("Reversed String:",reversedStr)