Skip to content

Instantly share code, notes, and snippets.

@cptangry
Last active November 27, 2017 17:34
Show Gist options
  • Save cptangry/4a8b75ecf703b365cbab306649681300 to your computer and use it in GitHub Desktop.
Save cptangry/4a8b75ecf703b365cbab306649681300 to your computer and use it in GitHub Desktop.
require 'minitest/autorun'
module Hamming
def self.compute(str1, str2)
if str1.length == str2.length
distance = str1.chars.zip(str2.chars).count { |l, r| l != r }
print "Hamming Distance for '#{str1}' & '#{str2}' : #{distance}", "\n"
distance
else
puts "ERROR! : The Hamming distance is only defined for sequences of equal length"
raise ArgumentError
end
end
end
class HammingTest < Minitest::Test
def test_empty_strands
# # skip
assert_equal 0, Hamming.compute('', '')
end
def test_identical_strands
# skip
assert_equal 0, Hamming.compute('A', 'A')
end
def test_long_identical_strands
# skip
assert_equal 0, Hamming.compute('GGACTGA', 'GGACTGA')
end
def test_complete_distance_in_single_nucleotide_strands
# skip
assert_equal 1, Hamming.compute('A', 'G')
end
def test_complete_distance_in_small_strands
# skip
assert_equal 2, Hamming.compute('AG', 'CT')
end
def test_small_distance_in_small_strands
# skip
assert_equal 1, Hamming.compute('AT', 'CT')
end
def test_small_distance
# skip
assert_equal 1, Hamming.compute('GGACG', 'GGTCG')
end
def test_small_distance_in_long_strands
# skip
assert_equal 2, Hamming.compute('ACCAGGG', 'ACTATGG')
end
def test_non_unique_character_in_first_strand
# skip
assert_equal 1, Hamming.compute('AAG', 'AAA')
end
def test_non_unique_character_in_second_strand
# skip
assert_equal 1, Hamming.compute('AAA', 'AAG')
end
def test_same_nucleotides_in_different_positions
# skip
assert_equal 2, Hamming.compute('TAG', 'GAT')
end
def test_large_distance
# skip
assert_equal 4, Hamming.compute('GATACA', 'GCATAA')
end
def test_large_distance_in_off_by_one_strand
# skip
assert_equal 9, Hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT')
end
def test_disallow_first_strand_longer
# skip
assert_raises(ArgumentError) { Hamming.compute('AATG', 'AAA') }
end
def test_disallow_second_strand_longer
# skip
assert_raises(ArgumentError) { Hamming.compute('ATA', 'AGTG') }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment