Class: Hamming
- Inherits:
-
Object
- Object
- Hamming
- Defined in:
- lib/hamming.rb
Class Method Summary collapse
Class Method Details
.distance(a, b) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hamming.rb', line 13 def distance(a, b) a = a.kind_of?(Array) ? vector_to_hash(a) : a b = b.kind_of?(Array) ? vector_to_hash(b) : b n = a.hex ^ b.hex ones = 0 while n > 0 n &= n - 1 ones += 1 end ones end |
.hash_to_vector(hash) ⇒ Object
9 10 11 |
# File 'lib/hamming.rb', line 9 def hash_to_vector(hash) hash.hex.to_s(2).split("").map(&:to_i) end |
.vector_to_hash(vector) ⇒ Object
5 6 7 |
# File 'lib/hamming.rb', line 5 def vector_to_hash(vector) vector.join.to_i(2).to_s(16) end |