lory88 / gmail.com wrote: > Hi > > I hope someone can help me out with a very SIMPLE program > about whole-string permutations. That is: given a list of strings, > the required outcome is a complete set of all their possible > permutations. > It's like character permutations of a string, but this time it is > whole strings instead of single characters that have to be permuted. > > I need this because I don't remember exactly the password to open > my zipped archives, but i do remember the bits of strings > that made up the long passphrase. > > Could someone kindly write a simple program that, after reading a set > of > strings contained in a .txt file (one string on each line), > produces as output another .txt file containing all the possible > permutations/combinations of those strings. > > For example, the text file with the set of strings may contain: > > HOUSE > jolly > --- > 0& > 99 > > > and the output file contains: > > HOUSE > HOUSEjolly > HOUSE--- > HOUSE0& > > and so on... > ...with the word combinations growing extensively, > so as to exhaust all the possibilities: > > e.g. > > ---99jolly0& > jolly0&---99HOUSE > > etc. etc. > > Unfortunately I am not able to program it myself, so > I would appreciate if someone could write this piece of > software, compile it (for DOS or Windows) and send the .exe file to: > > lory88 at gmail . com > > > I thank you all in advance. > > Lory =begin A problem in combinations and permutations. Let's lift some code from this newsgroup. =end class Array def permute(prefixed=[]) if (length < 2) yield(prefixed + self) else each_with_index { |e, i| (self[0,i]+self[(i+1)..-1]).permute(prefixed+[e]) {|a| yield a } } end end end module Combine def Combine.pick(pick, items, &block) combine([], 0, pick, items, &block) end private def Combine.combine(set, index, pick, items, &block) if pick == 0 or index == items.length yield set else set.push(items[index]) combine(set, index + 1, pick - 1, items, &block) set.pop combine(set, index + 1, pick, items, &block) if pick < items.length - index end end end # You're right. We only have to write a few lines of code. the_list = gets(nil).split 1.upto(the_list.size) {|n| Combine.pick( n, the_list ) {|ary| ary.permute {|ary_p| puts ary_p.join } } }