Created
September 9, 2013 18:12
-
-
Save rjmcdonald83/6499379 to your computer and use it in GitHub Desktop.
Ruby Solution to Anagram solved with @seeflanigan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Anagram | |
attr_reader :source | |
attr_reader :match_chars | |
def initialize(source) | |
@source = source.downcase | |
@match_chars = self.source.split('').sort | |
end | |
def match(candidates) | |
candidates.reject {|word| duplicate?(word) }.select {|word| same_chars?(word) } | |
end | |
private | |
def duplicate?(word) | |
word.downcase.eql? source | |
end | |
def same_chars?(word) | |
word.downcase.split('').sort.eql? match_chars | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment