Created
May 1, 2012 04:13
-
-
Save tehviking/2564943 to your computer and use it in GitHub Desktop.
Kata six: Anagrams (pairing with Dan Dorman)
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 AnagramFinder | |
def find_anagrams(words) | |
output = [] | |
while (target_word = words.shift) | |
matches = [target_word] | |
words.reject! do |word| | |
matches << word if check_for_anagram(word, target_word) | |
end | |
output << matches | |
end | |
output | |
end | |
def check_for_anagram(word, target_word) | |
word.split(//).sort == target_word.split(//).sort | |
end | |
end | |
describe AnagramFinder do | |
describe "#find_anagrams" do | |
it "returns a list of lists of anagrams" do | |
input = %w[kinship pinkish elves] | |
output = [%w[kinship pinkish], %w[elves]] | |
subject.find_anagrams(input).should eq output | |
end | |
end | |
describe "#check_for_anagram" do | |
it "checks for identical letters in word" do | |
subject.check_for_anagram("kinship", "pinkish").should be_true | |
end | |
it "returns false for a non-anagram pair" do | |
subject.check_for_anagram("kinship", "monkeys").should be_false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment