Sha256: 7fd322b69a08cdd2158f5bb79eb3fc019bccf59c951286598467e07f9723af33

Contents?: true

Size: 807 Bytes

Versions: 2

Compression:

Stored size: 807 Bytes

Contents

# frozen_string_literal: true

module Edits
  # Implements Hamming distance algorithm
  #
  # @see https://en.wikipedia.org/wiki/Hamming_distance
  module Hamming
    # Calculate the Hamming distance between two sequences.
    #
    # @note A true distance metric, satisfies triangle inequality.
    #
    # @param seq1 [String, Array]
    # @param seq2 [String, Array]
    # @return [Integer] Hamming distance
    def self.distance(seq1, seq2)
      # if seq1.is_a?(Integer) && seq2.is_a?(Integer)
      #   return (seq1 ^ seq2).to_s(2).count("1")
      # end

      length = seq1.length < seq2.length ? seq1.length : seq2.length
      diff = (seq1.length - seq2.length).abs

      length.times.reduce(diff) do |distance, i|
        seq1[i] == seq2[i] ? distance : distance + 1
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
edits-0.2.1 lib/edits/hamming.rb
edits-0.2.0 lib/edits/hamming.rb