Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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