Skip to content

Instantly share code, notes, and snippets.

@collinvandyck
Created March 30, 2012 23:37
Show Gist options
  • Select an option

  • Save collinvandyck/2257920 to your computer and use it in GitHub Desktop.

Select an option

Save collinvandyck/2257920 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'benchmark'
def permute(chars)
if chars.length <= 1
[chars]
else
result = []
chars.each_with_index do |char, idx|
remainder = chars[0...idx] + chars[idx+1..chars.length]
permute(remainder).each do |permutation|
result << [char] + permutation
end
end
result
end
end
puts Benchmark.measure { puts permute("abcdefghijk".chars.to_a).length }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment