Created
September 5, 2013 16:53
-
-
Save dmehrotra/6452917 to your computer and use it in GitHub Desktop.
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 Anagram | |
def initialize(word) | |
@characters =[] | |
word.each_char{|x| @characters<<x} | |
end | |
def convert | |
anagrams = [] | |
@characters.permutation.each {|x| anagrams << x.join} | |
p anagrams | |
end | |
end | |
dog = Anagram.new('dog') | |
dog.convert |
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 'anagram' | |
describe Anagram do | |
describe 'Anagram = to' do | |
it 'expects :to: to return ot' do | |
expect(Anagram.new('to').convert).to eql('ot') | |
end | |
end | |
describe 'Anagram: dog' do | |
it 'expects :dog: to return an array of anagrams' do | |
expect(Anagram.new('dog').convert).to eql(["dog", "dgo", "odg", "ogd", "gdo", "god"]) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment