Text Munger (#76)

My first participation in Ruby Q., and it has to be easy. That
said, I must really be missing something because some of you guys are
mentioning one-liners, and mine is 26 lines. Maybe it’s because I
made mine highly abstracted, but I still don’t really see how to do
this in one line.

  • Jake McArthur

Strictly speaking, any Ruby code can be made into one line with
liberal use of the semi-colon (;). It would just be an extremely long
line!

My full, nicely abstracted solution for this quiz is 36 lines
(including empty lines), but I also wrote a somewhat obfuscated
one-line version which is 104 characters. But it is missing some of
the features of the full one. But overall it solves the quiz. It is
probably possible to make an even shorter version.

Ryan

Random playing around…

Neo:~/Desktop$ ruby gregory_seidmans_solution.rb
test_document.txt Atchtaed is my résumé.
Neo:~/Desktop$ ruby gregory_seidmans_solution.rb
test_document.txt Acaehttd is my résumé.
Neo:~/Desktop$ ruby gregory_seidmans_solution.rb
test_document.txt Atceahtd is my résumé.
Neo:~/Desktop$ ruby gregory_seidmans_solution.rb
test_document.txt Ahaecttd is my résumé.

Keep in mind that longer words with a purely randomly munged inside will
not
be “readable”.

“Ahaecttd” takes effort to figure out that it was “Attached” before the
munging.

The physical process of munging is very easy (as we’ve seen from people
reporting one line results), but modeling something that’ll also produce
“readable” results for all lengths of words is a bit more challenging.
:slight_smile:

-M

On Sat, 22 Apr 2006, Ryan L. wrote:

Ryan
my fully oo version is 12 lines and only 40 words.

i went golfing and got one line : 96 chars

let the games begin :wink:

-a

When I say “one-liner,” I’m excluding cases of using semicolons. In
code, I consider semicolons to be line breaks.
:wink:

  • Jake McArthur

On 4/21/06, [email protected] [email protected] wrote:

probably possible to make an even shorter version.

Ryan

my fully oo version is 12 lines and only 40 words.

i went golfing and got one line : 96 chars

let the games begin :wink:

Oh great… Now I have to read these solutions when writing up a
summary.

(Suddenly, I have recollections of my time as a teaching assistant,
having to read mountains of pages of Pascal code written by freshman
newbie programmers… .)

unknown wrote:

my fully oo version is 12 lines and only 40 words.

i went golfing and got one line : 96 chars

let the games begin :wink:

As a script, my one-liner is down to 70 chars including
the newline. It can be shortened a bit as a command-liner.
:slight_smile:

andrew

From: “Andrew J.” [email protected]

As a script, my one-liner is down to 70 chars including
the newline. It can be shortened a bit as a command-liner.
:slight_smile:

Wow, nice. Does that include the restriction in the quiz rules that
numbers have to be left alone? Or does your solution rearrange
both numbers and letters?

Regards,

Bill

On 4/21/06, Phil H. [email protected] wrote:

I have to say I am in favor of making quizzes of varying
difficulty. Like some others here, I’ve been a bit intimidated by all
the heavy meta stuff in the past, but with varying difficulty I can
work my way up to the tough stuff.

I agree with you, and I’m very experienced with Ruby. In most cases
the toughness is just in the problem itself, not even the Ruby aspects
of it. I don’t usually have time to solve those, but problems like
this which can be done in an hour or so are fun and worthwhile. Plus
all the various solutions usually provide some nice insights (even
from newbies!)

If a problem is so tough that it is even hard to read a solution, let
alone code one, I think the value of the quiz is diminished some. So
I’d suggest we try to keep most quizzes on the easy side and throw in
a few complicated ones now and then.

Actually in hindsight this seems to be the pattern, so keep it up! :wink:

Ryan

[email protected] writes:

i went golfing and got one line : 96 chars

86 chars after removing all the extraneous spaces. Of course, now it
looks like the Camping source.

Not bad for my first Ruby Q…

I have to say I am in favor of making quizzes of varying
difficulty. Like some others here, I’ve been a bit intimidated by all
the heavy meta stuff in the past, but with varying difficulty I can
work my way up to the tough stuff.

-Phil H.

i went golfing and got one line : 96 chars

For reference:

perl -pe
's/(?<=\w)\w+(?=\w)/join"",sort{int(rand(3)-2)}split"",$&/eg'

(65 characters)

Well, I can make one in ~60 chars that rearranges both. Still working
on getting <70 for not rearranging numbers…

  • Jake McArthur

Working on my short version. 62 chars long and works for your
example, but still trying to get <70 with it working correctly with
digits.

-Jake McArthur

On Apr 21, 2006, at 3:34 PM, Bill K. wrote:

From: “Andrew J.” [email protected]

As a script, my one-liner is down to 70 chars including
the newline. It can be shortened a bit as a command-liner.
:slight_smile:

Wow, nice. Does that include the restriction in the quiz rules that
numbers have to be left alone? Or does your solution rearrange
both numbers and letters?

I’m still waiting for someone to show off their solution properly
handling the trivial (multi-byte) example I showed earlier… :slight_smile:

James Edward G. II

On 4/21/06, Ruby Q. [email protected] wrote:

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.

I know everyone here is Nice™, so I’m sure this is not the
intent… but between this quiz and the Markov chain one, it seems we
are building a set of utilities perfect for generating those ‘Re:
PHARmudMACY’
spam emails selling ‘vigara’ and such that have been sneaking through
my spam filter at work recently…

-Adam

Hi –

On Sat, 22 Apr 2006, PerlyGates wrote:

i went golfing and got one line : 96 chars

For reference:

perl -pe
's/(?<=\w)\w+(?=\w)/join"",sort{int(rand(3)-2)}split"",$&/eg'

(65 characters)

But \w includes underscore. I think punctuation is supposed to remain
unscrambled, isn’t it? And numbers likewise.

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!

I’ve been able to condense mine to about 15 lines - I’m always impressed
that you guys can shrink stuff down so far… I just wish I could
understand
them after they’re posted to the group! :slight_smile:

-M

On Apr 21, 2006, at 8:55 PM, Adam S. wrote:

I know everyone here is Nice™, so I’m sure this is not the
intent… but between this quiz and the Markov chain one, it seems we
are building a set of utilities perfect for generating those ‘Re:
PHARmudMACY’
spam emails selling ‘vigara’ and such that have been sneaking through
my spam filter at work recently…

I vote we assume the best instead of the worst.

James Edward G. II

On Sat, 2006-04-22 at 06:09 +0900, James Edward G. II wrote:

both numbers and letters?

I’m still waiting for someone to show off their solution properly
handling the trivial (multi-byte) example I showed earlier… :slight_smile:

$ ./munger.rb test.txt
Attehcaed is my résmué.
$ ./munger.rb test.txt
Atthaceed is my réumsé.
$ ./munger.rb test.txt
Attacheed is my rémsué.
$ ./munger.rb test.txt
Attcaehed is my rémusé.
$ ./munger.rb test.txt
Attecahed is my rémsué.

It’s four lines though.

Hi –

On Sat, 22 Apr 2006, Adam S. wrote:

are building a set of utilities perfect for generating those ‘Re:
PHARmudMACY’
spam emails selling ‘vigara’ and such that have been sneaking through
my spam filter at work recently…

Maybe we can use the techniques to filter those messages out :slight_smile:

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!