Forum: Ruby Help - just a few lines of code needed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9cfb365fc955d1babe8b715688a6d172?d=identicon&s=25 unknown (Guest)
on 2006-03-07 02:26
(Received via mailing list)
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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-07 06:25
(Received via mailing list)
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-...
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-07 07:49
(Received via mailing list)
lory88@gmail.com 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 }
  }
}
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-07 07:55
(Received via mailing list)
William James wrote:

> the_list = gets(nil).split

If your strings can contain spaces, change to

the_list = gets(nil).split(/\n+/)
This topic is locked and can not be replied to.