Question

In: Computer Science

In python. Write a program that takes 2 string inputs and calculates the Hamming Distance. Hamming...

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

Solutions

Expert Solution

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..


Related Solutions

python program You are going to write a program that takes two inputs: A string representing...
python program You are going to write a program that takes two inputs: A string representing a list of names of the following form: first name 1, last name 1, (nick name 1); first name 2, last name 2, (nick name 2); ... A string representing part of or the complete family name. You are going to output all nick names for those names where there is a partial or complete match of the family name. If no match was...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
Python Program 1: Write a program that takes user input in the form of a string...
Python Program 1: Write a program that takes user input in the form of a string Break up the string into a list of characters Store the characters in a dictionary The key of the dictionary is the character The value of the dictionary is the count of times the letter appears Hint: remember to initialize each key before using Output of program is the count of each character/letter Sort the output by key (which is the character) Python Program...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone number, e.g., 800-MYPYTHON, and prints the all numeric equivalent, 800-69798466. You should implement this using a loop construct to process each character from left to right. Build a new string that is the all numeric equivalent and then print the string.
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits strings, you can assume each strings only contains 0's and 1's. (Note: the two strings might have the same length or not!) for example: hamming('010001001111', '01010100') should return 5(1 bit changed plus 4 bits "lost" from the end). 2. write a main function that ask user for two file names, open files and read the 1st line of each, and compares them using Hamming...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT