Created
August 31, 2016 23:44
-
-
Save mooreniemi/0df992ab45b187757069cc3aa1199757 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
require 'spec_helper' | |
def perms(str) | |
return [str] if str.length == 1 | |
results = [] | |
str.length.times do |i| | |
substring = str.dup | |
c = substring.slice!(i - 1) | |
perms(substring).map do |s| | |
results << c + s | |
end | |
end | |
results | |
end | |
RSpec.describe '#perms' do | |
it 'returns permutations' do | |
expect(perms('abc')).to match_array( | |
%w(abc bac cba cab bca acb) | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment