Thank you for your help yesterday and for teaching me something new as well. :)
I have another question based on permutation... the algorithm I have works however I have the开发者_StackOverflow中文版 issue that identical adjacent characters are missing from the list generated.
For example, if I have the character list a-z,0-9,- and let's say that the maximum length is 2, then I should see aa, bb, cc, dd, ee, ff, etc. ad nauseum.
length = 1
alphabet = [('a'..'z').to_a, ('0'..'9').to_a, ('-').to_a].flatten
prefix = 'file-'
suffix = '.txt'
while length < 3
alphabet.permutation(length).each do |x|
@name = prefix+x.join('').to_s+suffix
puts @name
end
length += 1
end
However, I am only seeing the following:
file-ba.txt
file-bc.txt
note the missing "bb" and this continues on until the program is finished.
I am sure I am missing something, just not sure what?
I think you want to use repeated_permutation instead of permutation.
http://www.ruby-doc.org/core/classes/Array.html#M000289
It will generate all permutations including "file-bb.txt".
That's what a permutation is. The only 6 permutations of [1,2,3] are
123
132
213
231
312
321
精彩评论