------art_4678_25881217.1145785503250
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Here's my solution.

Usage : scramble.rb <text_file>

I made 3 attempts.

1)
print ARGF.read.gsub!(/\B[a-z]+\B/) {|x| x.split('').sort_by{rand}.join}

Here I use gsub to find all the words. Use split to convert strings into
arrays. And then use the sort_by{rand} to scramble the arrays. And finally
use join to convert the array back to a string.
I'm assuming that words don't have upper case letters in the middle, so that
I can get away with [a-z].

2)
print ARGF.read.gsub!(/\B[a-z]+\B/) {|x| x.unpack
('c*').sort_by{rand}.pack('c*')}

I found this method of converting strings to and from arrays to be faster.
I'm not sure what the standard idiom for doing this is. But, I'm sure I'll
learn after seeing other people's solutions ;)

3 If sort_by{rand} does what I think it does, it probably has a bias when
the rand function returns the same value. So, this is my third
implementation:

print ARGF.read.gsub!(/\B[a-z]+\B/) {|x|
    x.length.times {|i|
        j = rand(i+1)
        x[j], x[i] = x[i] , x[j]
    }
    x
}

Basically, this is an implementation of scrambling that uses swaps. I
remember this method for scrambling from way back, but I can't seem to find
a good reference for it at the moment.
I also figured that this method would be faster since it is linear, while
the sorts are n log(n) (n = length of the word)

To by surprise, I found this method to actually be slower for any normal
text. One possible explanation is that when words are relatively short you
don't gain much from the n vs. nlogn difference, and you lose because while
this method always has n swaps, sorting may have less.

In order to see any performance benefit from the 3rd method I had to make up
some horrifically long words which aren't terribly likely in  the English
language (maybe I should have tried German :)).

Himadri

On 4/21/06, Ruby Quiz <james / grayproductions.net> wrote:
>
> The three rules of Ruby Quiz:
>
> 1.  Please do not post any solutions or spoiler discussion for this quiz
> until
> 48 hours have passed from the time on this message.
>
> 2.  Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rubyquiz.com/
>
> 3.  Enjoy!
>
> Suggestion:  A [QUIZ] in the subject of emails about the problem helps
> everyone
> on Ruby Talk follow the discussion.
>
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
> by Matthew Moss
>
>         Now terhe is a fnial rseaon I thnik that Jsues syas, "Lvoe your
>         emneies." It is tihs: taht love has wtiihn it a remvpidtee pewor.
> And
>         three is a pwoer three taht eellvtanuy tfranrmsos idvlinaidus.
> Taht's
>         why Juess syas, "Love your emeeins."  Bsceaue if you hate your
>         enmeies, you have no way to reedem and to tarfnrsom your eenmeis.
> But
>         if you love yuor emienes, you wlil decsiovr that at the vrey root
> of
>         love is the pwoer of rdoemptein. You just keep loinvg pepole and
> keep
>         lnivog tehm, even tgouhh they're mteitnsiarg you. Hree's the
> porsen
>         who is a nhoeigbr, and tihs psoren is dnoig simhoetng wrong to you
> and
>         all of that. Just keep being fnrdliey to that preosn. Keep liovng
>         them. Don't do atnynhig to earsmrbas tehm. Just keep lvonig them,
> and
>         they can't stand it too long. Oh, they raect in mnay ways in the
>         bineningg. They react wtih brnetitess beucase they're mad bauesce
> you
>         lvoe them like that. Tehy raect wtih gluit flegines, and setioemms
>
>         they'll hate you a lltite more at that tinoiasrtn piroed, but just
>         keep lvniog them. And by the poewr of your love tehy will beark
> down
>         uendr the load. That's lvoe, you see. It is retpmevide, and tihs
> is
>         why Juess says love. Trehe's shimeotng aubot love that blidus up
> and
>         is cavrtiee. Trehe is stmeonihg aubot hate that tares dwon and is
>         disettvrcue. So lvoe your eenmeis.
>
> On first glance, the above may appear to be gibberish, but you may find
> that you
> can actually read this portion of a speech from Dr Martin Luther King
> Jr.  The
> brain has an amazing capacity to compensate for things that aren't quite
> right,
> and one study has shown that when the first and last letters of words are
> left
> alone but those in the middle are scrambled, the text is often still quite
> comprehensible.
>
> Your task for this quiz, then, is to take a text as input and output the
> text in
> this fashion. Scramble each word's center (leaving the first and last
> letters of
> each word intact). Whitespace, punctuation, numbers -- anything that isn't
> a
> word -- should also remain unchanged.
>
>

------art_4678_25881217.1145785503250--