Menu

gitpiper

hamming_distance python Code Snippet in 2024

mathintermediate

Last Updated: 23 April 2024

Calculates the Hamming distance between two values.

  • Use the XOR operator (^) to find the bit difference between the two numbers.
  • Use bin() to convert the result to a binary string.
  • Convert the string to a list and use count() of str class to count and return the number of 1s in it.
def hamming_distance(a, b): return bin(a ^ b).count('1')
hamming_distance(2, 3) # 1

python snippet similar to hamming_distance For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️