Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.