ruby - anagram
use select
This commit is contained in:
parent
0923955efb
commit
da10e318e4
@ -2,13 +2,17 @@
|
|||||||
class Anagram
|
class Anagram
|
||||||
def initialize(word)
|
def initialize(word)
|
||||||
@word = word.downcase
|
@word = word.downcase
|
||||||
@sorted = @word.chars.sort.join
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def match(candidates)
|
def match(candidates)
|
||||||
candidates.map do |candidate|
|
candidates.select do |candidate|
|
||||||
sorted_candidate = candidate.downcase.chars.sort.join
|
sorted(@word) == sorted(candidate) && @word != candidate.downcase
|
||||||
candidate if @sorted == sorted_candidate && @word != candidate.downcase
|
end
|
||||||
end.compact.sort
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def sorted(word)
|
||||||
|
word.downcase.chars.sort.join
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user