Created
May 24, 2015 11:59
-
-
Save buren/80ef34ef30c4f88c6c9b to your computer and use it in GitHub Desktop.
Permute name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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