Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Created August 31, 2016 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooreniemi/0df992ab45b187757069cc3aa1199757 to your computer and use it in GitHub Desktop.
Save mooreniemi/0df992ab45b187757069cc3aa1199757 to your computer and use it in GitHub Desktop.
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