[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

scrambler one-liner

Xavier Noria

9/16/2003 2:28:00 PM

I just came across this interesting article at Slashdot that explains that
according to some English university the order of letters in words are not
really relevant for reading as long as the first and the last are at the
right place. (It seems that do not hold in Spanish).

There is a link to a Perl filter[*] that munges a given text that way and I
wanted to write a mock-up as a one-liner, but in Ruby this time. This is
what I have for now:

-pe 'gsub!(/\w+/){|w|r=1..w.size-1;w[r]=w[r].split(//).sort_by{rand}.to_s;w}'

The cool Array.sort_by { rand } trick is a shuffle I saw in Freenode#ruby-lang
last week, I would like very much to remember the nick of who showed it but I
can't remember.

It seems one-liners are not at the heart of the Ruby community, but hey,
that's funny in the end. Can you shorten it?

-- fxn

[*] http://www.jwz.org/hacks/s...


17 Answers

Jason Williams

9/16/2003 2:31:00 PM

0

In article <200309161631.48824.fxn@hashref.com>, Xavier Noria wrote:
> -pe ''gsub!(/\w+/){|w|r=1..w.size-1;w[r]=w[r].split(//).sort_by{rand}.to_s;w}''
>
> The cool Array.sort_by { rand } trick is a shuffle I saw in Freenode#ruby-lang
> last week, I would like very much to remember the nick of who showed it but I
> can''t remember.
>
> It seems one-liners are not at the heart of the Ruby community, but hey,
> that''s funny in the end. Can you shorten it?

Alternatively, you could write a version that accepts a "Maximum
distance" for letters to be moved from their original position. Most
people notice after playing for a short while that it doesn''t work
so well for longer words; I''m curious whether it''s actually a limit
of "total shuffle distance".

James F. Hranicky

9/16/2003 2:57:00 PM

0

On Tue, 16 Sep 2003 23:28:01 +0900
Xavier Noria <fxn@hashref.com> wrote:

> It seems one-liners are not at the heart of the Ruby community, but hey,
> that''s funny in the end. Can you shorten it?

*shrug* I use ruby for my one liners all the time...

% ruby -e ''p File.stat("/etc/passwd").mtime''
Fri May 30 15:17:19 EDT 2003

or more horribly

% ruby -rdbi -e ''rows = []; DBI.connect("dbi:Pg:somedb:localhost", "postgres", "XXXXX")
{ |dbi| dbi.prepare("select cisco, cisco_slot, cisco_port, liu_number, liu_port, floor
from new_fiberx where cisco is not null and cisco_slot is not null and cisco_port is
not null and liu_number is not null and liu_port is not null order by cisco, cisco_slot,
cisco_port") { |sth| sth.execute; sth.each {|row| rows << row.clone } ; rows.sort_by
{ |row| [ row[0], row[1].to_i, row[2].to_i ] }.each { |row| next if row[0] == "" ;
printf "%s.%s/%d : %d.%s/%-5s\n", row[0], row[1], row[2], row[5], row[3], row[4] }}}''

etc...

Jim

Kurt M. Dresner

9/16/2003 6:07:00 PM

0

This is hilarious, because a friend and I just had (over lunch) a race
to see who could code it up first. I used Ruby and he used Perl. Mine
worked first, but then I reworked it a bit to make it neater. His was
impossible to read, but I totally have to send him this one-liner. Man,
sort_by{rand} is so awesome! I can''t believe I didn''t think of that!

-Kurt

On Tue, Sep 16, 2003 at 11:28:01PM +0900, Xavier Noria wrote:
> I just came across this interesting article at Slashdot that explains that
> according to some English university the order of letters in words are not
> really relevant for reading as long as the first and the last are at the
> right place. (It seems that do not hold in Spanish).
>
> There is a link to a Perl filter[*] that munges a given text that way and I
> wanted to write a mock-up as a one-liner, but in Ruby this time. This is
> what I have for now:
>
> -pe ''gsub!(/\w+/){|w|r=1..w.size-1;w[r]=w[r].split(//).sort_by{rand}.to_s;w}''
>
> The cool Array.sort_by { rand } trick is a shuffle I saw in Freenode#ruby-lang
> last week, I would like very much to remember the nick of who showed it but I
> can''t remember.
>
> It seems one-liners are not at the heart of the Ruby community, but hey,
> that''s funny in the end. Can you shorten it?
>
> -- fxn
>
> [*] http://www.jwz.org/hacks/s...
>
>
>======= End of Original Message =======<

Xavier Noria

9/16/2003 6:21:00 PM

0

On Tuesday 16 September 2003 20:07, Kurt M. Dresner wrote:

> This is hilarious, because a friend and I just had (over lunch) a
> race to see who could code it up first. I used Ruby and he used
> Perl. Mine worked first, but then I reworked it a bit to make it
> neater. His was impossible to read, but I totally have to send him
> this one-liner. Man, sort_by{rand} is so awesome! I can''t believe I
> didn''t think of that!

Cool! :-) I have just written this version even shorter:

-pe ''gsub!(/(\w)(\w+)(?=\w)/){$1+$2.split(//).sort_by{rand}.to_s}''

-- fxn


Kurt M. Dresner

9/16/2003 6:33:00 PM

0

Try

-pe ''gsub!(/\B\w+\B/){$&.split(//).sort_by{rand}.join}

Shorter (and even cleaner too!)

-Kurt



On Wed, Sep 17, 2003 at 03:20:53AM +0900, Xavier Noria wrote:
> On Tuesday 16 September 2003 20:07, Kurt M. Dresner wrote:
>
> > This is hilarious, because a friend and I just had (over lunch) a
> > race to see who could code it up first. I used Ruby and he used
> > Perl. Mine worked first, but then I reworked it a bit to make it
> > neater. His was impossible to read, but I totally have to send him
> > this one-liner. Man, sort_by{rand} is so awesome! I can''t believe I
> > didn''t think of that!
>
> Cool! :-) I have just written this version even shorter:
>
> -pe ''gsub!(/(\w)(\w+)(?=\w)/){$1+$2.split(//).sort_by{rand}.to_s}''
>
> -- fxn
>
>
>======= End of Original Message =======<

Mark J. Reed

9/16/2003 7:05:00 PM

0


On Wed, Sep 17, 2003 at 03:33:23AM +0900, Kurt M. Dresner wrote:
> Try
>
> -pe ''gsub!(/\B\w+\B/){$&.split(//).sort_by{rand}.join}
>
> Shorter (and even cleaner too!)

It''ll work with just sort instead of sort_by, won''t it?
Save three characters! :)

-Mark

Kurt M. Dresner

9/16/2003 7:26:00 PM

0

I don''t /think/ it works with sort. Someone correct me if I''m wrong,
but the block passed to sort has to return either a -1, 0, or 1, and I
think it needs to be consistent between two items (at least it should
be).

-Kurt

On Wed, Sep 17, 2003 at 04:12:07AM +0900, Mark J. Reed wrote:
>
> On Wed, Sep 17, 2003 at 03:33:23AM +0900, Kurt M. Dresner wrote:
> > Try
> >
> > -pe ''gsub!(/\B\w+\B/){$&.split(//).sort_by{rand}.join}
> >
> > Shorter (and even cleaner too!)
>
> It''ll work with just sort instead of sort_by, won''t it?
> Save three characters! :)
>
> -Mark
>
>======= End of Original Message =======<

Jason Creighton

9/16/2003 8:30:00 PM

0

On Tue, 16 Sep 2003 19:04:53 GMT
"Mark J. Reed" <markjreed@mail.com> wrote:

>
> On Wed, Sep 17, 2003 at 03:33:23AM +0900, Kurt M. Dresner wrote:
> > Try
> >
> > -pe ''gsub!(/\B\w+\B/){$&.split(//).sort_by{rand}.join}
> >
> > Shorter (and even cleaner too!)
>
> It''ll work with just sort instead of sort_by, won''t it?
> Save three characters! :)

No, not quite. Sort will pass in two args and expect you to return -1, 0
or 1. So you could say

enum.sort { rand(3)-1 }

...or..

enum.sort { rand() <=> 0.5 }

....but I don''t know what evil effects having the comparison be random
will have on a sorting algorithm. Best to stick with sort_by.

Jason Creighton

Tom Felker

9/16/2003 8:56:00 PM

0

On Tue, 16 Sep 2003 23:28:01 +0900, Xavier Noria wrote:

> I just came across this interesting article at Slashdot that explains
> that according to some English university the order of letters in words
> are not really relevant for reading as long as the first and the last
> are at the right place. (It seems that do not hold in Spanish).

Cool. I was going to post something about that, but I hadn''t bothered to
write the code.

But for the really interesting part, who can make either the shortest or
most efficient descrambler? Here''s a Perl version from Slashdot:

#!/usr/bin/perl
open A,"/usr/share/dict/words"||die;sub
a{$b=b(split//,$_[0]);map{uc;($b->{$_}!=$_[1]->{$_ }?return
0:1)}keys%{$_[1]}}sub b{undef$z;map{$z->{uc$_}++}@_;$z}$d=$ARGV[0];$c=b(
split//,$ARGV[0]);map{print"$_\n"}grep{chomp;/^[$d ]+$/i&&a$_,$c}<A>;close
A;

I haven''t even tried to understand this, but it is very slow. Seems to
work, though. I''ll post if I get anything.

--
Tom Felker, <tcfelker@mtco.com>
<http://vlevel.sourcefor... - Stop fiddling with the volume knob.

IBM invented FUD, and they''re not about to be outFUDed by SCO.

Mark J. Reed

9/16/2003 9:18:00 PM

0

On Wed, Sep 17, 2003 at 04:26:01AM +0900, Kurt M. Dresner wrote:
> I don''t /think/ it works with sort. Someone correct me if I''m wrong,
> but the block passed to sort has to return either a -1, 0, or 1, and I
> think it needs to be consistent between two items (at least it should
> be).

Right, right. That occurred to me after I sent that message. For
some reason I was thinking that sort and sort_by were the same method
with two aliases, the longer one intended for use when passing a block.
Obviously the blocks really do different things - the sort_by block
converts each item into a key to be compared, while the sort block
performs the actual comparisons.

Just ignore me. :)

-Mark