0
我試圖找到兩個字符串之間的字母差異。 例如,如果我把單詞ATTGCC和GTTGAC,差異將是2,因爲A和G以及C和G不是相同的字符。如何計算Ruby中字符串之間不同字母的數量?
class DNA
def initialize (nucleotide)
@nucleotide = nucleotide
end
def length
@nucleotide.length
end
def hamming_distance(other)
self.nucleotide.chars.zip(other.nucleotide) { |a,b| a == b }.count
end
protected
attr_reader :nucleotide
end
dna1 = DNA.new("ATTGCC")
dna2 = DNA.new("GTTGAC")
puts dna1.hamming_distance(dna2)
方法hamming_distance並未真正發揮作用,因爲它給出了一個錯誤的參數類型爲String(必須迴應:每個)(類型錯誤)
查找https://en.wikipedia.org/wiki/Levenshtein_distance – Mircea
鑑於你[前僱主發現不匹配的對數問題](https://stackoverflow.com/questions/39711526/how-to-compare-two-instance-variables-from-the-same-class-in-ruby)我假設輸入字符串是(for例如)'「ATTGCC」'和'「GTTGAC」'並且長度將是相等的。值得在這個問題中明確這些條件。 – meagar
是的,我繼續並更新了問題。謝謝。 – Codes316