Created
December 11, 2013 15:57
-
-
Save sjwats/7912948 to your computer and use it in GitHub Desktop.
Campaign Charlie - Anagram TDD
This file contains hidden or 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 AnagramGenerator | |
| attr_reader :word | |
| def initialize(word) | |
| @word = word.downcase | |
| end | |
| def separate_word | |
| @separate_words = @word.split('') | |
| end | |
| def anagram_maker | |
| @anagrams = @separate_words.permutation.map(&:join) | |
| end | |
| def anagrams | |
| anagram_list = anagram_maker | |
| anagram_list.delete(@word) | |
| anagram_list | |
| end | |
| end |
This file contains hidden or 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
| require 'rspec' | |
| require_relative 'anagrams' | |
| describe AnagramGenerator do | |
| it 'should separate words into separate elements in an array' do | |
| anagrams = AnagramGenerator.new('dog') | |
| expect(anagrams.separate_word).to eql(['d', 'o', 'g']) | |
| end | |
| it 'should determine all possible combinations of word characters in the array' do | |
| anagrams = AnagramGenerator.new('dog') | |
| anagrams.separate_word | |
| expect(anagrams.anagram_maker).to eql(["dog","dgo", "odg", "ogd", "gdo", "god"]) | |
| end | |
| it 'should return an empty array if the string is blank' do | |
| blank_anagram = AnagramGenerator.new('') | |
| blank_anagram.separate_word | |
| blank_anagram.anagram_maker | |
| expect(blank_anagram.anagrams).to eql([]) | |
| end | |
| it 'should return an empty array if the string has one letter' do | |
| single_letter = AnagramGenerator.new('t') | |
| single_letter.separate_word | |
| single_letter.anagram_maker | |
| expect(single_letter.anagrams).to eql([]) | |
| end | |
| it 'should return one anagram for a two letter word' do | |
| single_anagram = AnagramGenerator.new('to') | |
| single_anagram.separate_word | |
| single_anagram.anagram_maker | |
| expect(single_anagram.anagrams).to eql(['ot']) | |
| end | |
| it 'should cleave the root word from the list of anagrams' do | |
| anagrams = AnagramGenerator.new('dog') | |
| anagrams.separate_word | |
| anagrams.anagram_maker | |
| expect(anagrams.anagrams).to eql(["dgo", "odg", "ogd", "gdo", "god"]) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment