ruby - anagram
use select
This commit is contained in:
parent
0923955efb
commit
da10e318e4
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user