[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help - just a few lines of code needed

lory88

3/7/2006 1:22:00 AM

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

3 Answers

Daniel Harple

3/7/2006 5:25:00 AM

0

On Mar 7, 2006, at 2:23 AM, lory88@gmail.com 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.
>
> <snip>
>
> 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
instead. ;)

[1] http://mail.python.org/pipermail/python-list/2006-March/3...



William James

3/7/2006 6:47:00 AM

0

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 }
}
}

William James

3/7/2006 6:52:00 AM

0

William James wrote:

> the_list = gets(nil).split

If your strings can contain spaces, change to

the_list = gets(nil).split(/\n+/)