Last active
August 26, 2016 17:47
-
-
Save jeccb-zz/382dd5e83e214913a8e936e4f90a0ce1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hamming | |
def self.compute(type_a, type_b) | |
raise ArgumentError if (type_a.size != type_b.size) | |
count = 0; | |
type_a.each_char.with_index do |x, x_index| | |
type_b.each_char.with_index do |z, z_index| | |
if (x_index != z_index) | |
next; | |
end | |
count += 1 unless x.eql?z | |
end | |
next; | |
end | |
count | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
gem 'minitest', '>= 5.0.0' | |
require 'pry' | |
require 'minitest/autorun' | |
require_relative 'hamming' | |
# Test data version: | |
# deb225e Implement canonical dataset for scrabble-score problem (#255) | |
class HammingTest < Minitest::Test | |
def test_identical_strands | |
assert_equal 0, Hamming.compute('A', 'A') | |
end | |
def test_long_identical_strands | |
assert_equal 0, Hamming.compute('GGACTGA', 'GGACTGA') | |
end | |
def test_complete_distance_in_single_nucleotide_strands | |
assert_equal 1, Hamming.compute('A', 'G') | |
end | |
def test_complete_distance_in_small_strands | |
assert_equal 2, Hamming.compute('AG', 'CT') | |
end | |
def test_small_distance_in_small_strands | |
assert_equal 1, Hamming.compute('AT', 'CT') | |
end | |
def test_small_distance | |
assert_equal 1, Hamming.compute('GGACG', 'GGTCG') | |
end | |
def test_small_distance_in_long_strands | |
assert_equal 2, Hamming.compute('ACCAGGG', 'ACTATGG') | |
end | |
def test_non_unique_character_in_first_strand | |
assert_equal 1, Hamming.compute('AGA', 'AGG') | |
end | |
def test_non_unique_character_in_second_strand | |
assert_equal 1, Hamming.compute('AGG', 'AGA') | |
end | |
def test_same_nucleotides_in_different_positions | |
assert_equal 2, Hamming.compute('TAG', 'GAT') | |
end | |
def test_large_distance | |
assert_equal 4, Hamming.compute('GATACA', 'GCATAA') | |
end | |
def test_large_distance_in_off_by_one_strand | |
assert_equal 9, Hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT') | |
end | |
def test_empty_strands | |
assert_equal 0, Hamming.compute('', '') | |
end | |
def test_disallow_first_strand_longer | |
assert_raises(ArgumentError) { Hamming.compute('AATG', 'AAA') } | |
end | |
def test_disallow_second_strand_longer | |
assert_raises(ArgumentError) { Hamming.compute('ATA', 'AGTG') } | |
end | |
end |
Não entendi porque o if abaixo é necessário:
if (x_index != z_index)
Acho que dá pra re-escrever todo o loop com apenas 1 linha, não?
count += 1 unless x.eql?z
Pode substituir o return count
por count
.
Não entendi porque o if abaixo é necessário:
'if (x_index != z_index)`
Esse if é necessário pois ao finalizar a letra do segundo each_char
no primeiro ele pegava a próxima letra mas no segundo ele repetia a primeira. Ex:
Hamming.compute('AT', 'CT')
No primeiro loop ele compara a letra A e C mas na segunda vez ele estava comparando o T e o C que não é o correto, o certo seria ele comparar o T e T, deu para entender?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Acho que
poderia estar na primeira linha do método.