Site icon Console Flare Blog

Hamming Distance 101: Easy Insights for Everyday Understanding”

hamming distance

Before discussing anything further let’s discuss

What is Hamming Distance?

The concept of Hamming distance was first given by an American mathematician named Richard Hamming in the year 1950. It is calculated between two equal-sized strings on behalf of non-similar alphabets. For example, the hamming distance between “RAT” and “SAT” is 1 because there is only 1 pair of non-similar alphabets that is ( R & S).

Before going further let us have a look at exactly in which field hamming distance helps us.

Where do we use hamming distance?

Hamming distance is used majorly in comparing and finding a pattern between two strings. Talking about the field we use hamming distance in fields such as “Error Detection and Correction”, “Genetics and DNA Sequencing”, “Cryptography”, “Network Routing”, “Speech Recognition”, “Pattern Recognition” etc.

Let us understand hamming distance with a simple program of comparing two words.

def hamming_distance(str1, str2):
    if len(str1) == len(str2):
        distance = 0
        for char1, char2 in zip(str1, str2):
            if char1 != char2:
                distance += 1
    return distance
    
    else:
        print("Error: Input strings must have the same length")
        return None
  1. So we have declared and defined a function by the name of hamming_distance with str1 and str2 as parameters.
  2. Hamming distance is used to find dissimilarities between two equal-length strings therefore we have used a conditional statement that will make sure that the length(number of characters) of both strings are equal.
  3. if the length of both strings is equal that means the condition is true, and the body of if will start performing tasks such as assigning 0 to a variable distance.
  4. Now to compare the characters from both strings we have used the for loop along with the zip() function which will take every character one by one from both strings and store the character from str1 into char1 and the character from str2 into char2.
  5. Now we have to use conditional statements again to check if the characters of both strings are not equal. So if the characters are not equal we will update the distance variable by 1 for every dissimilar character from both strings.
  6. After checking all the characters from both strings, we will return the variable distance which will hold the final value or the exact distance between two strings.

Want to read more such interesting blogs, visit: blog.consoleflare.com

string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")

distance = hamming_distance(string1, string2)
  1. Receive two inputs from the user and store them in variables string1 and string2 respectively.
  2. Call the function and pass string1 and string2 as arguments. The function will perform the task and return a value which will be stored in the distance variable.
if distance is not None:
    print(f"The Hamming distance between '{string1}' and '{string2}' is:  {distance}")

Our final step is to check the data (int/none) stored at a distance. The output will be displayed only if there is data(int) present in distance otherwise in case of none no output will be displayed.

If you want to explore more in this field, you can also look for Levenshtein Distance.

For more such content and regular updates, follow us on FacebookInstagramLinkedIn

If you wish to learn more about data science or want to curve your career in the data science field feel free to join our free workshop on Masters in Data Science with PowerBI, where you will get to know how exactly the data science field works and why companies are ready to pay handsome salaries in this field.

In this workshop, you will get to know each tool and technology from scratch that will make you skillfully eligible for any data science profile.

To join this workshop, register yourself on consoleflare and we will call you back.

Download our brochure:

Exit mobile version