PROGRAMS
PROGRAMS
def linear_search():
lst=eval(input("enter list"))
ele=int(input("enter element to be searched"))
n=len(lst)
for i in range(n):
if lst[i]==ele:
return i
else:
return None
def binary_search():
lst=eval(input("enter list"))
ele=int(input("enter element to be searched"))
n=len(lst)
f=0
l=n-1
while f<=l:
mid=(f+l)//2
if lst[mid]==ele:
return mid+1
elif lst[mid]<ele:
f=mid+1
elif lst[mid]>ele:
l=mid-1
else:
return None
ch="y"
while ch=="y":
print("1- LINEAR SEARCH")
print("2- BINARY SEARCH")
print("3- EXIT")
choice=int(input("enter choice"))
if choice==1:
pos=linear_search()
if pos!=None:
print("element found in position",pos+1)
else:
print("element not found")
elif choice==2:
pos=binary_search()
if pos!=None:
print("element found in position",pos)
else:
print("element not found")
elif choice==3:
break
ch=input("Continue ? y/n ")
OUTPUT:
1- LINEAR SEARCH
2- BINARY SEARCH
3- EXIT
enter choice1
enter list[1,12,15,20]
enter element to be searched12
element found in position 2
Continue ? y/n y
1- LINEAR SEARCH
2- BINARY SEARCH
3- EXIT
enter choice2
enter list[1,12,15,20]
enter element to be searched15
element found in position 3
Continue ? y/n y
1- LINEAR SEARCH
2- BINARY SEARCH
3- EXIT
enter choice3
Bubble and Insertion
def bubble_sort():
lst= eval(input("enter the list"))
n=len(lst)
print(lst)
for i in range(0,n):
for j in range(0,n-i-1):
if lst[j]>lst[j+1]:
lst[j],lst[j+1]=lst[j+1],lst[j]
print(lst,"after pass",i+1)
print("after sorting",lst)
def insertion_sort():
lst=eval(input("enter the list"))
n=len(lst)
print(lst)
for i in range(1,n):
key=lst[i]
j=i-1
while j>=0 and lst[j]>key:
lst[j+1]=lst[j]
j-=1
else:
lst[j+1]=key
print(lst,"after pass",i+1)
print("After sorting",lst)
ch="y"
while ch=="y":
print("1- BUBBLE SORT")
print("2- INSERTION SORT")
print("3- EXIT")
choice=int(input("enter choice"))
if choice==1:
pos=bubble_sort()
if pos!=None:
print("element found")
elif choice==2:
pos=insertion_sort()
if pos!=None:
print("element found")
elif choice==3:
break
ch=input("Continue ? y/n ")
OUTPUT:
1- BUBBLE SORT
2- INSERTION SORT
3- EXIT
enter choice1
enter the list[1,30,15,12]
[1, 30, 15, 12]
[1, 15, 12, 30] after pass 1
[1, 12, 15, 30] after pass 2
[1, 12, 15, 30] after pass 3
[1, 12, 15, 30] after pass 4
after sorting [1, 12, 15, 30]
Continue ? y/n y
1- BUBBLE SORT
2- INSERTION SORT
3- EXIT
enter choice2
enter the list[12,15,2,3]
[12, 15, 2, 3]
[12, 15, 2, 3] after pass 2
[2, 12, 15, 3] after pass 3
[2, 3, 12, 15] after pass 4
After sorting [2, 3, 12, 15]
Continue ? y/n y
1- BUBBLE SORT
2- INSERTION SORT
3- EXIT
enter choice3
Sum of even/odd and Digit palindrome
l=eval(input("Enter list"))
def seo():
e=0
o=0
for i in l:
if i%2==0:
e=e+i
else:
o=o+i
print("Sum of odd numbers is",o)
print("sum of even numbers is",e)
def dpal():
palin=[]
count=0
for i in l:
temp=i
rev=0
while i:
dig=i%10
rev=(rev*10)+dig
i//=10
if rev==temp:
palin.append(temp)
count+=1
print("The palindrome number is ",palin)
print('The count is ',count)
ch="y"
while ch=="y":
print("1- SUM OF EVEN AND ODD")
print("2- DIGIT PALINDROME")
print("3- EXIT")
choice=int(input("Enter choice"))
if choice==1:
seo()
elif choice==2:
dpal()
elif choice==3:
break
ch=input("Continue ? y/n ")
OUTPUT:
Enter list[11,14,10,21]
1- SUM OF EVEN AND ODD
2- DIGIT PALINDROME
3- EXIT
Enter choice1
Sum of odd numbers is 32
sum of even numbers is 24
Continue ? y/n y
1- SUM OF EVEN AND ODD
2- DIGIT PALINDROME
3- EXIT
Enter choice2
The palindrome number is [11]
The count is 1
Continue ? y/n y
1- SUM OF EVEN AND ODD
2- DIGIT PALINDROME
3- EXIT
Enter choice3
Sum of prime and 2nd largest
def slarge():
n=len(lst)
l=s=min(lst)
for i in range(0,n):
if lst[i]>l:
s=l
l=lst[i]
elif lst[i]>s:
s=lst[i]
print("Second largest number is:",s)
ans="y"
while ans=="y":
print("1.Sum of prime numbers")
print("2.Second largest number")
print("3.Exit")
choice=int(input("Enter the choice:"))
if choice==1:
sprime()
elif choice==2:
slarge()
else:
break
ans=input("Continue ? y/n ")
OUTPUT:
Enter the list:[12,13,17,44]
1.Sum of prime numbers
2.Second largest number
3.Exit
Enter the choice:1
The numbers are: 13
The numbers are: 17
Sum of Prime Number is: 30
Continue ? y/n y
1.Sum of prime numbers
2.Second largest number
3.Exit
Enter the choice:2
Second largest number is: 17
Continue ? y/n y
1.Sum of prime numbers
2.Second largest number
3.Exit
Enter the choice:3
Counting characters and conversion
def countch():
u,l,n,sc,sp,v,c=0,0,0,0,0,0,0
for i in str1:
if i.isalpha():
if i.isupper():
u+=1
else:
l+=1
if i in "AEIOUaeiou":
v+=1
else:
c+=1
elif i.isdigit():
n+=1
elif i.isspace():
sp+=1
else:
sc+=1
print("Number of digits=",n)
print("Number of vowels=",v)
print("Number of consonants=",c)
print("Number of spaces=",sp)
def convert():
str3=" "
for i in str2:
if i.isalnum():
if i.isupper():
str3+=i.lower()
elif i.islower():
str3+=i.upper()
else:
str3+=i
ch="y"
while ch=="y":
print("3- EXIT")
choice=int(input("enter choice"))
if choice==1:
countch()
elif choice==2:
convert()
elif choice==3:
break
OUTPUT:
1- COUNT THE CHARACTERS
3- EXIT
enter choice1
Number of digits= 3
Number of vowels= 6
Number of consonants= 9
Number of spaces= 3
Continue ? y/n y
3- EXIT
enter choice2
Continue ? y/nn
Maximum and minimum of tuple and perfect no
def maxmin():
t=eval(input("enter a tuple"))
max2=t[0]
min2=t[0]
for i in t:
if i>max2:
max2=i
if i<min2:
min2=i
def perfectno():
t=eval(input("enter a tuple"))
for i in t:
s=0
for j in range(1,i):
if i%j==0:
s=s+j
if s==i:
else:
ch='y'
while ch=='y':
print("2.perfect no")
print("3.exit")
cho=int(input("enter no:"))
if cho==1:
maxmin()
elif cho==2:
perfectno()
elif cho==3:
break
Output:
2.perfect no
3.exit
enter no:1
enter a tuple(12,35,45,67)
max in tuple is : 67
2.perfect no
3.exit
enter no:2
enter a tuple(6,28,34)
it is not a perfect no
2.perfect no
3.exit
enter no:3
string palindrome and replace character
def stringpalindrome():
str=input("enter a string")
k=len(str)
if str[::-1]==str:
print("it is a palindrome")
else:
def replacecharacter():
str=input("enter a string")
k=len(str)
s=""
for i in range(0,k):
if i==l:
s=s+ch
else:
s=s+str[i]
print(s)
ch="y"
while ch=="y":
print("3- EXIT")
choice=int(input("enter choice"))
if choice==1:
pos=stringpalindrome()
elif choice==2:
pos=replacecharacter()
elif choice==3:
break
Output:
1- STRING PALINDROME
3- EXIT
enter choice1
enter a stringsanjeyrama
it is not a palindrome
Continue ? y/n y
1- STRING PALINDROME
3- EXIT
enter choice2
enter a stringsanrey
sanjey
Continue ? y/n n
DICTIONARY
def chf():
str1=input("enter string")
s=str1.split()
l=list(s)
d={}
for i in l:
if i not in d:
c=l.count(i)
d[i]=c
print(d)
def key():
st=input("Enter string")
l=st.split()
d={}
for i in l:
d[i[0]]=[]
d[i[0]].append(i)
else:
if (i not in d[i[0]]):
d[i[0]].append(i)
print(key,":",value)
ch="y"
while ch=="y":
choice=int(input("Enter choice"))
if choice==1:
chf()
elif choice==2:
key()
elif choice==3:
break
Output:
1- COUNT CHARACTER FREQUENCY
3- EXIT
Enter choice1
{'apple': 2}
Continue ? y/n y
3- EXIT
Enter choice2
m : ['melon', 'man']
Continue ? y/n n
TEXT FILE
def create():
f1=open("file1.txt","w")
for i in range(n):
f1.write(str1+"\n")
f1.flush()
f1.close()
def copy():
f2=open("file1.txt","r")
f3=open("file2-copied.txt","w")
for i in f2:
f3.write(i)
print("copied")
f2.close()
f3.close()
def display():
f1=open("file2-copied.txt","r")
a=f1.readlines()
print(a)
ch="y"
while ch=="y":
print("3- display")
choice=int(input("enter choice"))
if choice==1:
create()
elif choice==2:
copy()
elif choice==3:
display()
Output:
1- create a file
3- display
enter choice1
do u want to continue:y/ny
1- create a file
3- display
enter choice2
copied
do u want to continue:y/ny
1- create a file
3- display
enter choice3
do u want to continue:y/nn