Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ERROR CORRECTION

TECHNIQUE

Submitted By:- Submitted To:-


Name :-- Ayush Jain Name :--Prof. Tanushree Dholpuria
Enroll. No. :- 0103AL213D01
Branch :- CSE-AIML
Sem. : - 5th
Min. Edit Distance
Definition:- Minimum Edit Distance gives you to the minimum number of
operations required to change one string into another string. The
operations involved are:-
• Insert
• Update
• Delete

All the operations involve the same cost

Example:-
Let’s say,
Input:
String 1 = ‘Cat’
String 2 = ‘Car’
Output: 1

The minimum number of operations required to change string 1 to string 2


is only one. That means to change the string ‘Cat’ into string ‘Car’ is to only
update the letter ‘t’ to ‘r’.
IMPLEMENTATION OF MIN EDIT DIS. IN
PYTHON

def edit_distance(str1, str2, a, b):


string_matrix = [[0 for i in range(b+1)] for i in range(a+1)]
for i in range(a+1):
for j in range(b+1):
if i == 0:
string_matrix[i][j] = j # If first string is empty, insert all
characters of second string into first.
elif j == 0:
string_matrix[i][j] = i # If second string is empty, remove all
characters of first string.
elif str1[i-1] == str2[j-1]:
string_matrix[i][j] = string_matrix[i-1][j-1] # If last
characters of two strings are same, nothing much to do. Ignore the last
two characters and get the count of remaining strings.
else:
string_matrix[i][j] = 1 + min(string_matrix[i][j-1], # insert operation
string_matrix[i-1][j], # remove operation
string_matrix[i-1][j-1]) # replace operation
return string_matrix[a][b]

if __name__ == '__main__':
str1 = 'Cats'
str2 = 'Rats'
print('No. of Operations required :',edit_distance(str1, str2, len(str1), len(str2)))
str3 = 'Saturday'
str4 = 'Sunday'
print('No. of Operations required :',edit_distance(str3, str4, len(str3), len(str4)))
------------------------------------------------------------------------------------------------------------
Case 1-:-

Input :
str1 = ‘cat’
Str2 = ‘rat’
No. of operation required :1
WAP FOR COUNTDOWN USING
RECURSION
def count(n):
if n<1:
return
print(n,end=‚ ‚)
return count(n-1)
count(10)

OUTPUT:
10 9 8 7 6 5 4 3 2 1
Thank You!

You might also like