# Help - just a few lines of code needed

**URL:** <https://www.ruby-forum.com/t/help-just-a-few-lines-of-code-needed/77614>\
**Category:** Ruby\
**Created:** [March 7, 2006, 2:26am UTC](https://www.ruby-forum.com/t/help-just-a-few-lines-of-code-needed/77614 "2006-03-07T02:26:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 7, 2006, 2:26am UTC](https://www.ruby-forum.com/t/help-just-a-few-lines-of-code-needed/77614/1 "2006-03-07T02:26:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 7, 2006, 6:25am UTC](https://www.ruby-forum.com/t/help-just-a-few-lines-of-code-needed/77614/2 "2006-03-07T06:25:04Z")

</div>

On Mar 7, 2006, at 2:23 AM, removed\_email\_address@domain.invalid wrote:

> 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.
> 
> 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:

Follow the Python folks’ suggestion[1], but go to [ruby-lang.org](http://ruby-lang.org)  
instead. 😉

[1] [http://mail.python.org/pipermail/python-list/2006-March/329541.html](http://mail.python.org/pipermail/python-list/2006-March/329541.html)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 7, 2006, 7:55am UTC](https://www.ruby-forum.com/t/help-just-a-few-lines-of-code-needed/77614/3 "2006-03-07T07:55:37Z")

</div>

William J. wrote:

> the\_list = gets(nil).split

If your strings can contain spaces, change to

the\_list = gets(nil).split(/\n+/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 7, 2006, 7:49am UTC](https://www.ruby-forum.com/t/help-just-a-few-lines-of-code-needed/77614/4 "2006-03-07T07:49:35Z")

</div>

removed\_email\_address@domain.invalid wrote:

> my zipped archives, but i do remember the bits of strings  
> HOUSE  
> HOUSE—
> 
> 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 }  
}  
}
