ruby - anagram

use select
This commit is contained in:
James Walker 2018-10-24 10:57:41 -04:00
parent 0923955efb
commit da10e318e4
Signed by: walkah
GPG Key ID: 3C127179D6086E93

View File

@ -2,13 +2,17 @@
class Anagram
def initialize(word)
@word = word.downcase
@sorted = @word.chars.sort.join
end
def match(candidates)
candidates.map do |candidate|
sorted_candidate = candidate.downcase.chars.sort.join
candidate if @sorted == sorted_candidate && @word != candidate.downcase
end.compact.sort
candidates.select do |candidate|
sorted(@word) == sorted(candidate) && @word != candidate.downcase
end
end
private
def sorted(word)
word.downcase.chars.sort.join
end
end