0% found this document useful (0 votes)
21 views

PROGRAMS

The document discusses various Python functions related to searching algorithms, sorting algorithms, string operations, and dictionaries. It provides examples of linear search, binary search, bubble sort, insertion sort, counting characters in strings, checking palindromes, and using dictionaries.

Uploaded by

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

PROGRAMS

The document discusses various Python functions related to searching algorithms, sorting algorithms, string operations, and dictionaries. It provides examples of linear search, binary search, bubble sort, insertion sort, counting characters in strings, checking palindromes, and using dictionaries.

Uploaded by

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

Linear and Binary Search

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

lst=eval(input("Enter the list:"))


def sprime():
sum=0
for i in lst:
num=1
for j in range(2,i):
if i%j==0:
num=0
break
if num==1:
print("The numbers are:",i,end=" ")
sum=sum+i
print()
print("Sum of Prime Number is:",sum)

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():

str1=input("Enter the string")

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 upper case=",u)

print("Number of lower case=",l)

print("Number of digits=",n)

print("Number of vowels=",v)

print("Number of consonants=",c)

print("Number of spaces=",sp)

print("Number of special characters=",sc)

def convert():

str2=input("Enter the string")

str3=" "
for i in str2:

if i.isalnum():

if i.isupper():

str3+=i.lower()

elif i.islower():

str3+=i.upper()

else:

str3+=i

print("The converted string is",str3)

ch="y"

while ch=="y":

print("1- COUNT THE CHARACTERS")

print("2- cONVERT THE CASES")

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

2- cONVERT THE CASES

3- EXIT

enter choice1

Enter the stringHello this is @kola123

Number of upper case= 1

Number of lower case= 14

Number of digits= 3

Number of vowels= 6

Number of consonants= 9

Number of spaces= 3

Number of special characters= 1

Continue ? y/n y

1- COUNT THE CHARACTERS

2- cONVERT THE CASES

3- EXIT

enter choice2

Enter the stringCoNvErSiOn@case

The converted string is cOnVeRsIoN@CASE

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

print("max in tuple is :",max2)

print("min in tuple is:",min2)

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:

print("the number ,",j,"is a perfect number")

else:

print("it is not a perfect no")

ch='y'

while ch=='y':

print("1.max and min of tuple")

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:

1.max and min of tuple

2.perfect no

3.exit

enter no:1

enter a tuple(12,35,45,67)

max in tuple is : 67

min in tuple is: 12

1.max and min of tuple

2.perfect no

3.exit

enter no:2

enter a tuple(6,28,34)

the number , 6 is a perfect number

the number , 28 is a perfect number

it is not a perfect no

1.max and min of tuple

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:

print("it is not a palindrome")

def replacecharacter():

str=input("enter a string")

k=len(str)

s=""

l=int(input("enter the index need to be replaced"))

for i in range(0,k):

if i==l:

ch=input("enter the character:")

s=s+ch

else:

s=s+str[i]

print(s)

ch="y"

while ch=="y":

print("1- STRING PALINDROME")

print("2- REPLACE CHARACTER IN A STRING")

print("3- EXIT")

choice=int(input("enter choice"))

if choice==1:
pos=stringpalindrome()

elif choice==2:

pos=replacecharacter()

elif choice==3:

break

ch=input("Continue ? y/n ")

Output:
1- STRING PALINDROME

2- REPLACE CHARACTER IN A STRING

3- EXIT

enter choice1

enter a stringsanjeyrama

it is not a palindrome

Continue ? y/n y

1- STRING PALINDROME

2- REPLACE CHARACTER IN A STRING

3- EXIT

enter choice2

enter a stringsanrey

enter the index need to be replaced3

enter the character:j

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:

if (i[0] not in d.keys()):

d[i[0]]=[]

d[i[0]].append(i)

else:

if (i not in d[i[0]]):

d[i[0]].append(i)

for key,value in d.items():

print(key,":",value)

ch="y"

while ch=="y":

print("1- COUNT CHARACTER FREQUENCY")

print("2- MAKE FIRST LETTER AS THE KEY")


print("3- EXIT")

choice=int(input("Enter choice"))

if choice==1:

chf()

elif choice==2:

key()

elif choice==3:

break

ch=input("Continue ? y/n ")

Output:
1- COUNT CHARACTER FREQUENCY

2- MAKE FIRST LETTER AS THE KEY

3- EXIT

Enter choice1

enter stringapple apple

{'apple': 2}

Continue ? y/n y

1- COUNT CHARACTER FREQUENCY

2- MAKE FIRST LETTER AS THE KEY

3- EXIT

Enter choice2

Enter stringmelon melon man

m : ['melon', 'man']

Continue ? y/n n
TEXT FILE
def create():

f1=open("file1.txt","w")

n=int(input("Enter number of lines"))

for i in range(n):

str1=input("enter the string")

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:

if i[0]in "AEIOUaeiou" and i[-2] in "AEIOUaeiou":

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("1- create a file")

print("2- copy contents to other file")

print("3- display")

choice=int(input("enter choice"))

if choice==1:
create()

elif choice==2:

copy()

elif choice==3:

display()

ch=input("do u want to continue:y/n")

Output:
1- create a file

2- copy contents to other file

3- display

enter choice1

Enter number of lines3

enter the stringindia ise

enter the stringa country

enter the stringin asia

do u want to continue:y/ny

1- create a file

2- copy contents to other file

3- display

enter choice2

copied

do u want to continue:y/ny

1- create a file

2- copy contents to other file

3- display

enter choice3

['india ise \n']

do u want to continue:y/nn

You might also like