In: Computer Science
In python.
Write a program that takes 2 string inputs and calculates the Hamming Distance. Hamming distance between two strings is the number of positions at which the corresponding symbols are different.
The program should output an integer representing this distance.
For example
a = XXWWZZ
b = ZZWWXX
answer = 4
More examples:
"Phone" and "PHOONE" = 3
"God" and "Dog" = 2
"Dog" and "House" = 4
I have written the code using PYTHON PROGRAMMING LANGUAGE.
OUTPUT :
CODE :
#Function definition for stringdifference
def stringdifference (string1,string2):
diff = 0 #initialized diff with zero
#to get the minimum length string
minimum = min(len(string1),len(string2))
#For loop for getting the diff count
for i in range(minimum):
if(string1[i].lower() != string2[i].lower()):
diff += 1
#condition for adding the remaining character of bigger string
if(len(string1) > len(string2)):
diff += len(string1) - len(string2)
else:
diff += len(string2) - len(string1)
return diff#returned result
if(__name__ == "__main__"):
str1 = input("Enter String 1: ")
str2 = input("Enter String 2: ")
print("Hamming Distance = {:d}".format(stringdifference(str1,str2)))
Thanks..