Skip to content

Instantly share code, notes, and snippets.

@buren
Created May 24, 2015 11:59
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 buren/80ef34ef30c4f88c6c9b to your computer and use it in GitHub Desktop.
Save buren/80ef34ef30c4f88c6c9b to your computer and use it in GitHub Desktop.
Permute name
require 'set'
last = (ARGV.shift || fail('<int> Permute of last n chars must be set')).to_i
seed = ARGV
fail 'At least one seed must be provided' if seed.empty?
def say(name)
puts name
`say #{name}`
name
end
def permutations(name, n)
head = name[0..-(n - 1)]
tails = name[-n..-1].chars.permutation.map(&:join)
tails.each { |tail| yield(head + tail) }
end
names = seed
found = Set.new
names.each do |name|
found << say(name)
permutations(name, last) do |permutation|
found << say(permutation) unless found.include?(permutation)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment