On Mon, Mar 7, 2011 at 8:47 AM, Martin Hansen <> wrote:
> What is the fastest way to calculate the Hamming Distance between two
> equally long strings in Ruby? A have looked at the amatch gem, but I was
> wondering what can be done with Ruby only?
>
> Here is a very interesting discussion on the subject:
>
> http://www.perlmonks.org/?node_id=3D500235
>
>
> Note the elegant and extremely fast Perl solution:
>
> sub hd {
> =A0 =A0return ($_[0] ^ $_[1]) =3D~ tr/\001-\255//;
> }
There are a bunch of ways. There isn't a built in xor of strings in
Ruby, but it's easy to define one. Once you have that, you can do it
pretty similarly to the Perl example:
def hd(l, r)
l.xor(r).tr("\x00",'').length
end
You can implement xor of two strings using pure ruby, but to get it
fast, you'll probably want to utilize an extension. Here's one
approach using a technique that, I believe, was in the mailing list
several years ago:
require "narray"
class String
def xor(other)
if other.empty?
self
else
left =3D self
right =3D other
if left.length < right.length
n,r =3D right.length.divmod(left.length)
left =3D left * n + left[0, r]
elsif right.length < left.length
n,r =3D left.length.divmod(right.length)
right =3D right * n + right[0, r]
end
left_na =3D NArray.to_na(left, "byte")
right_na =3D NArray.to_na(right, "byte")
(left_na ^ right_na).to_s
end
end
def hamming_distance(other)
self.xor(other).tr("\x00",'').length
end
end
Kirk Haines
Software Engineer
Engine Yard