Forum: Ruby Text Munger (#76) simplistic solution.

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.
05e48e632fdd0b2c25d27042f52c11d5?d=identicon&s=25 A LeDonne (Guest)
on 2006-04-23 16:47
(Received via mailing list)
This solution makes no attempt to deal with non-ascii, AND counts
underscore as a word character. This is the first time I read a quiz,
took a crack at a solution, and found my brain spitting out "inject"
on the first attempt.

The \w{4,} is not just an optimization to skip words of 3 letters or
less; it also ensures that runs of digits or of punctuation do not get
scrambled.

I was somewhat surprised that even with a u modifier on the regex,
POSIX regex classes like [:alpha:] don't pick up the Unicode notion of
letter. Perhaps Oniguruma will do such magic.

-A

class String
    def scramble
        self.split(/\b/).inject(""){ |result, wordbit| result << (
          wordbit.match(/\w{4,}/)?
            (wordbit[0,1] << wordbit[1..-2].split(//).sort_by{ rand
}.join("") << wordbit[-1,1]) :
            wordbit ) }
    end

    def scramble!
        self.replace self.scramble
    end
end
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-04-23 17:31
(Received via mailing list)
"A LeDonne" <aledonne.listmail@gmail.com> writes:

> This solution makes no attempt to deal with non-ascii, AND counts
> underscore as a word character.

... and niheter deos mine.

# Now, taht was a nice, sorht quiz good for a qucik barek... I knew the
# poerblm came up brfeoe, but I wrote my sotouiln berfoe loknoig at
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/....

STDIN.each { |line| puts line.gsub(/\B\w+\B/) {
$&.split('').sort_by{rand} } }

# Eojny.
This topic is locked and can not be replied to.