From 0b90c1489828653bdec38db8dc824df560c4a0e5 Mon Sep 17 00:00:00 2001 From: Anna Wilson Date: Sat, 7 May 2016 12:01:01 -0700 Subject: [PATCH 1/2] iterative solution --- hamming.rb | 22 ++++++++++++++++++++++ hamming_test.rb | 1 - 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 hamming.rb diff --git a/hamming.rb b/hamming.rb new file mode 100644 index 0000000..a4365c2 --- /dev/null +++ b/hamming.rb @@ -0,0 +1,22 @@ +class Hamming + + def self.compute(seq1,seq2) + unless seq1.length == seq2.length + raise ArgumentError.new("The lengths of the strands must be equal.") + end + + array1 = seq1.split(//) + array2 = seq2.split(//) + count = 0 + + array1.each_with_index do |item,index| + if item == array2[index] + count = count + 0 + else + count = count + 1 + end + end + return count + end + +end diff --git a/hamming_test.rb b/hamming_test.rb index 2d1f9d7..f3feb79 100644 --- a/hamming_test.rb +++ b/hamming_test.rb @@ -4,7 +4,6 @@ class HammingTest < Minitest::Test def test_identical_strands - skip assert_equal 0, Hamming.compute('A', 'A') end From 94586d078e5c5ad2aa6cd33226f58b7f30e0c7aa Mon Sep 17 00:00:00 2001 From: Anna Wilson Date: Sat, 7 May 2016 19:28:33 -0700 Subject: [PATCH 2/2] now with answer --- hamming.rb | 36 +++++++++++++++++++++++++----------- hamming_test.rb | 13 ------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/hamming.rb b/hamming.rb index a4365c2..5e1cf05 100644 --- a/hamming.rb +++ b/hamming.rb @@ -5,18 +5,32 @@ def self.compute(seq1,seq2) raise ArgumentError.new("The lengths of the strands must be equal.") end - array1 = seq1.split(//) - array2 = seq2.split(//) - count = 0 + @array1 = seq1.split(//) + @array2 = seq2.split(//) + @count = 0 + next_elements + count_differences(@element1,@element2) + end + + def self.count_differences(element1,element2) + if @element1 == nil + return @count + end - array1.each_with_index do |item,index| - if item == array2[index] - count = count + 0 - else - count = count + 1 - end + if element1 != element2 + @count += 1 + next_elements + count_differences(@element1,@element2) + else + @count += 0 + next_elements + count_differences(@element1,@element2) end - return count end - + + def self.next_elements + @element1 = @array1.shift + @element2 = @array2.shift + end + end diff --git a/hamming_test.rb b/hamming_test.rb index f3feb79..0ddf0a1 100644 --- a/hamming_test.rb +++ b/hamming_test.rb @@ -8,67 +8,54 @@ def test_identical_strands 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('AGA', 'AGG') end def test_non_unique_character_in_second_strand - skip assert_equal 1, Hamming.compute('AGG', 'AGA') 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_empty_strands - skip assert_equal 0, Hamming.compute('', '') 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