From a9acfc110c2a4f11daa34a252824308a342d535c Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 9 May 2016 15:37:29 -0700 Subject: [PATCH 1/2] created hamming rb file --- hamming.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 hamming.rb diff --git a/hamming.rb b/hamming.rb new file mode 100644 index 0000000..c8eed1b --- /dev/null +++ b/hamming.rb @@ -0,0 +1,22 @@ +class Hamming + def self.compute(dna,sdna) + c = [] + d = dna.split('') + s = sdna.split('') + if s.length != d.length + raise ArgumentError.new("First or second strand to long") #could have used unless + end + not_matching_element = d.dup + f = 0 + until f == s.length + s.each_index do |i| + if not_matching_element[i] != s[i] + c << s[i] + + end + f += 1 + end + end + return c.length + end +end From b50fce4bc560e71a79b06c40e35c298338e5abf8 Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 9 May 2016 23:09:19 -0700 Subject: [PATCH 2/2] finished --- hamming.rb | 54 ++++++++++++++++++++++++++++++++++--------------- hamming_test.rb | 26 +++++++++++------------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/hamming.rb b/hamming.rb index c8eed1b..4ab3166 100644 --- a/hamming.rb +++ b/hamming.rb @@ -1,22 +1,44 @@ +#class Hamming +# def self.compute(dna,sdna) +# c = [] +# d = dna.split('') +# s = sdna.split('') +# if s.length != d.length +# raise ArgumentError.new("First or second strand to long") #could have used unless +# end +# not_matching_element = d.dup +# f = 0 +# until f == s.length +# s.each_index do |i| +# if not_matching_element[i] != s[i] +# c << s[i] +# +# end +# f += 1 +# end +# end +# return c.length +# end +# end +# +# +# + + class Hamming - def self.compute(dna,sdna) - c = [] - d = dna.split('') - s = sdna.split('') - if s.length != d.length - raise ArgumentError.new("First or second strand to long") #could have used unless + def self.compute(dna,sdna, counter = 0) + if dna.length != sdna.length + raise ArgumentError end - not_matching_element = d.dup - f = 0 - until f == s.length - s.each_index do |i| - if not_matching_element[i] != s[i] - c << s[i] - end - f += 1 - end + if dna.length == 0 && sdna.length == 0 + return counter + + elsif + dna[0] != sdna[0] + counter += 1 end - return c.length + return self.compute(dna[1..-1], sdna[1..-1], counter) + end end diff --git a/hamming_test.rb b/hamming_test.rb index 2d1f9d7..1ab7602 100644 --- a/hamming_test.rb +++ b/hamming_test.rb @@ -4,72 +4,70 @@ class HammingTest < Minitest::Test 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('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