Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > All possible letter combinations?

Reply
Thread Tools

All possible letter combinations?

 
 
Teme Rosi
Guest
Posts: n/a
 
      12-11-2008
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/.

 
Reply With Quote
 
 
 
 
Teme Rosi
Guest
Posts: n/a
 
      12-11-2008
Thanks!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      12-12-2008
On Thu, Dec 11, 2008 at 7:22 AM, DanDiebolt.exe <(E-Mail Removed)> 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

 
Reply With Quote
 
Martin Carpenter
Guest
Posts: n/a
 
      12-16-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is That A Letter...A Letter For Me? peterg@uti.com Computer Support 6 12-10-2006 12:32 PM
XML + XSD: Is it possible to get all possible Values for an Element? Markus Java 1 11-22-2005 02:51 PM
Re: big letter -> small letter Andrew McNamara Python 2 07-06-2004 02:09 PM
big letter -> small letter vertigo Python 4 07-06-2004 07:23 AM
RE: big letter -> small letter Tony Meyer Python 0 07-06-2004 07:11 AM



Advertisments