Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I'm totally new to python, I want to calculate the Hamming distance between the two strings

chaine1 = 6fb17381822a6ca9b02153d031d5d3da

chaine2 = a242eace2c57f7a16e8e872ed2f2287d

The XOR work didn't work, and my hunt on the web was not effective. 

I attempted to adjust something I found on the web, however, there's some invalid syntax...:

assert len (chaine1) == len(chaine2)

return sum(chaine1 != chaine2 for chaine1, chaine2 in zip(chaine1, chaine2))

if __name__=="__main__":    

chaine1 = hashlib.md5("chaine1".encode()).hexdigest()

chaine2 = hashlib.md5("chaine2".encode()).hexdigest()

print hamming_distance(chaine1, chaine2)

Can anyone help me with how to proceed with it? Thanks

closed

4 Answers

0 votes
by (15.4k points)
selected by
 
Best answer
Here is the correct code for the above question:

import hashlib

def hamming_distance(string1, string2):

    assert len(string1) == len(string2)

    return sum(bit1 != bit2 for bit1, bit2 in zip(string1, string2))

if __name__ == "__main__":

    chaine1 = "6fb17381822a6ca9b02153d031d5d3da"

    chaine2 = "a242eace2c57f7a16e8e872ed2f2287d"

    distance = hamming_distance(chaine1, chaine2)

    print("The Hamming distance between the two strings is:", distance)

In this revised version of the code, the hashlib module is imported at the beginning. The hamming_distance function is defined to calculate the Hamming distance between two strings. The strings chaine1 and chaine2 are corrected by enclosing them in quotes. The assert statement ensures that the strings have the same length. The zip() function is used to iterate over corresponding bits in the two strings, and the Hamming distance is computed. Finally, the result is printed.

Please note that the Hamming distance compares corresponding bits in the strings, so it is important for the strings to have the same length.
0 votes
by (26.4k points)

Look at the below program to find the hamming distance using two different ways.

import hashlib

def hamming_distance(chaine1, chaine2):

    return sum(c1 != c2 for c1, c2 in zip(chaine1, chaine2))

def hamming_distance2(chaine1, chaine2):

    return len(list(filter(lambda x : ord(x[0])^ord(x[1]), zip(chaine1, chaine2))))

if __name__=="__main__":    

    chaine1 = hashlib.md5("chaine1".encode()).hexdigest()

    chaine2 = hashlib.md5("chaine2".encode()).hexdigest()

    #chaine1 = "6fb17381822a6ca9b02153d031d5d3da"

    #chaine2 = "a242eace2c57f7a16e8e872ed2f2287d"

    assert len(chaine1) == len(chaine2)

    print(hamming_distance(chaine1, chaine2))

    print(hamming_distance2(chaine1, chaine2))

Since, you didn't have any intendation in your program, that's why you got Invalid syntax error.

Join the python online course fast, to learn python concepts in detail and get certified.

0 votes
by (25.7k points)
I can help you with calculating the Hamming distance between two strings in Python. However, there are a few issues with the code you provided. Let's go through them and make the necessary corrections:

The strings chaine1 and chaine2 need to be enclosed in quotes since they represent text.

The hashlib module is not imported in the code, so we need to add the import statement import hashlib at the beginning of the code.

The hamming_distance function is not defined in the code, so we need to include its definition.

Here's the corrected code:

import hashlib

def hamming_distance(chaine1, chaine2):

    assert len(chaine1) == len(chaine2)

    return sum(bit1 != bit2 for bit1, bit2 in zip(chaine1, chaine2))

if __name__ == "__main__":

    chaine1 = "6fb17381822a6ca9b02153d031d5d3da"

    chaine2 = "a242eace2c57f7a16e8e872ed2f2287d"

    distance = hamming_distance(chaine1, chaine2)

    print("The Hamming distance between the two strings is:", distance)

In this updated code, the hashlib module is imported at the beginning. The hamming_distance function is defined to calculate the Hamming distance between the two strings. The strings chaine1 and chaine2 are corrected by enclosing them in quotes. The assert statement checks the length of the strings. The zip() function is used to iterate over corresponding bits of the two strings, and the Hamming distance is calculated. Finally, the result is printed.

Please note that the Hamming distance compares corresponding bits in the strings, so the strings should have the same length.
0 votes
by (19k points)
import hashlib

def hamming_distance(string1, string2):

    assert len(string1) == len(string2)

    return sum(bit1 != bit2 for bit1, bit2 in zip(string1, string2))

if __name__ == "__main__":

    chaine1 = "6fb17381822a6ca9b02153d031d5d3da"

    chaine2 = "a242eace2c57f7a16e8e872ed2f2287d"

    distance = hamming_distance(chaine1, chaine2)

    print("The Hamming distance between the two strings is:", distance)

In this improved version of the code, the hashlib module is imported at the beginning. The function hamming_distance is defined to calculate the Hamming distance between two strings. The variables chaine1 and chaine2 are used to store the input strings. The assert statement verifies that the strings have the same length. The zip() function is employed to iterate over corresponding characters in the two strings, and the Hamming distance is computed. Finally, the result is printed.

Note that the Hamming distance is determined by comparing corresponding characters in the strings, so it is crucial for the strings to have the same length.

Related questions

Browse Categories

...