Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions hamming.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hamming Test Project
class Hamming

def self.compute(str1, str2)
puts "str1 = #{str1}, str2 = #{str2}"
# Base case: no more characters to process / empty string
return 0 if str1.length <= 0 # str1 == "" , str1.empty?

# Recursive case: peel off one letter, recurse, and add 1 if different
first = str1[0]
second = str2[0]
if first != second
puts "First letters are different"
return 1 + compute(str1[1..-1],str2[1..-1])
else
return 0 + compute(str1[1..-1],str2[1..-1])
end

end

end

#str1('AT') = 1
#str2('CT') = 1
puts Hamming.compute("ATGAC","ACTAC")
20 changes: 20 additions & 0 deletions hamming1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Hamming Test Project
class Hamming

def self.compute(str1, str2)
# hamming distance assumes strings of equal length
# .chars splits a string into an array
str1 = str1.chars
str2 = str2.chars

raise ArgumentError unless str1.length == str2.length #error when user makes an arror, not an error with syntax

differences = 0
str1.each_index do |ind|
if str1[ind] != str2[ind]
differences += 1
end
end
return differences
end
end
1 change: 0 additions & 1 deletion hamming_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class HammingTest < Minitest::Test
def test_identical_strands
skip
assert_equal 0, Hamming.compute('A', 'A')
end

Expand Down