All possible letter combinations?

Discussion in 'Ruby' started by Teme Rosi, Dec 11, 2008.

  1. Teme Rosi

    Teme Rosi Guest

    Hi, im new to ruby. It is smiliar to perl. Can anyone convert this
    script to ruby?



    #!/usr/bin/perl

    my $data = '0123456789abcdefghijklmnopqrstuvwxyz';
    my @list = split //, $data;
    for my $i (@list) {
    for my $j (@list) {
    for my $k (@list) {
    for my $l (@list) {
    print "$i $j $k $l\n";
    }
    }
    }
    }
    --
    Posted via http://www.ruby-forum.com/.
     
    Teme Rosi, Dec 11, 2008
    #1
    1. Advertising

  2. Teme Rosi

    Teme Rosi Guest

    Teme Rosi, Dec 11, 2008
    #2
    1. Advertising

  3. Teme Rosi

    Todd Benson Guest

    On Thu, Dec 11, 2008 at 7:22 AM, DanDiebolt.exe <> wrote:
    >>... convert this script to ruby ...

    >
    > data='0123456789abcdefghijklmnopqrstuvwxyz'
    > list=data.split("")
    > list.each do |i|
    > list.each do |j|
    > list.each do |k|
    > list.each do |l|
    > puts "#{i} #{j} #{k} #{l} "
    > end
    > end
    > end
    > end


    Logically equivalent on 1.8.7, but _don't_do_it_ (far too slow)...

    s = "0123456789abcdefghijklmnopqrstuvwxyz"
    b = ""
    (s * 4).split(//).combination(4) {|group| b << group.join << "\n"}

    ...If just printing you could just use "p group.join", or "puts
    group". I find it interesting that it is about 5x slower than the
    code quoted above on my machine. I suppose it has to do with extra
    method calls inside the block, since it is almost on par if you don't
    do anything inside the block :) Having b as an array without the
    join or "\n" basically halts my machine (10 seconds to recognize the
    ctrl-c interrupt).

    Todd
     
    Todd Benson, Dec 12, 2008
    #3
  4. On Thu, 11 Dec 2008 21:44:31 -0500, Todd Benson wrote:

    >> data='0123456789abcdefghijklmnopqrstuvwxyz'
    >> list=data.split("")
    >> list.each do |i|
    >> list.each do |j|
    >> list.each do |k|
    >> list.each do |l|
    >> puts "#{i} #{j} #{k} #{l} "
    >> end
    >> end
    >> end
    >> end

    >
    > Logically equivalent on 1.8.7, but _don't_do_it_ (far too slow)...
    >
    > s = "0123456789abcdefghijklmnopqrstuvwxyz"
    > b = ""
    > (s * 4).split(//).combination(4) {|group| b << group.join << "\n"}



    Alternatively:

    STRING_LEN = 4 # length of each string to output
    BASE = 36 # number of symbols in data string

    (0..BASE ** STRING_LEN - 1).each do |i|
    puts i.to_s(BASE).rjust(STRING_LEN, '0')
    end

    Here this runs ~the same speed as the nested loops solution. This won't
    work for BASE > 36 however.
     
    Martin Carpenter, Dec 16, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D r .  P r o z a c

    Identify letter combinations where order is not important?

    D r . P r o z a c, Oct 19, 2003, in forum: C Programming
    Replies:
    3
    Views:
    429
    Micah Cowan
    Oct 20, 2003
  2. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    759
    Reinhold Birkenfeld
    Jul 6, 2004
  3. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    507
    Tony Meyer
    Jul 6, 2004
  4. Andrew McNamara

    Re: big letter -> small letter

    Andrew McNamara, Jul 6, 2004, in forum: Python
    Replies:
    2
    Views:
    753
    Scott David Daniels
    Jul 6, 2004
  5. Rich
    Replies:
    5
    Views:
    441
Loading...

Share This Page