【发布时间】:2015-12-06 09:08:26
【问题描述】:
我被分配写一个 Anagram 程序 以下是我想出的
class Anagram
attr_accessor :anagram_value
def initialize(value)
@anagram_value = value
end
def matches(*collection)
matches = []
matches = collection.select do |word|
(word.length == @anagram_value.length) ? is_an_anagram?(word) : false
end
return matches
end
def is_an_anagram?(word)
return get_word_ord_sum(word) == get_word_ord_sum(@anagram_value)
end
def get_word_ord_sum(word)
sum = 0
word.split("").each { |c| sum += c.ord }
Areturn sum
end
end
虽然上述情况使用以下情况,但令人惊讶的是。
it "detects multiple Anagrams" do
subject = Anagram.new("allergy")
matches = subject.matches('gallery', 'ballerina', 'regally', 'clergy', 'largely', 'leading');
expect(matches).to eq ['gallery', 'regally', 'largely']
end
它实际上失败了以下
it "no matches" do
subject = Anagram.new("abc")
matches = subject.matches("bbb")
expect(matches).to eq []
end
【问题讨论】: