anagram.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Given a word and a list of possible anagrams, select the correct sublist.
class Anagram
  def initialize(subject)
    @subject = subject.downcase.chars
    @subject_sorted = @subject.sort
  end

  def match(candidates)
    candidates.select(&method(:match_word?))
  end

  private

  def match_word?(candidate)
    candidate = candidate.downcase.chars
    candidate != @subject && candidate.sort == @subject_sorted
  end
end

Comments


You're not logged in right now. Please login via GitHub to comment