Text Munger (#76)


#1

The three rules of Ruby Q.:

  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 Q. by submitting ideas as often as you can:

http://www.rubyquiz.com/

  1. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby T. follow the discussion.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Matthew M.

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.


#2

Hi –

On Fri, 21 Apr 2006, Ruby Q. wrote:

who is a nhoeigbr, and tihs psoren is dnoig simhoetng wrong to you and
disettvrcue. So lvoe your eenmeis.
each word intact). Whitespace, punctuation, numbers – anything that isn’t a
word – should also remain unchanged.

Question:

Given a word like “there’s” or “that’s”, does the letter before the
apostrophe count as a “last” letter? In other words, could “that’s”
become “ttha’s”?

In the example above, there’s no case where that letter gets
scrambled. It’s possible that that’s coincidence, but it doesn’t look
like it.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!


#3

On Apr 21, 2006, at 8:10 AM, Florian Groß wrote:

What about writing an unscrambler? Could that also be done for this
quiz or might that be next week’s task? :slight_smile:

It’s not part of the challenge this week or next, but you know I’m
always for setting your own goals. :slight_smile:

James Edward G. II


#4

Ruby Q. 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.

What about writing an unscrambler? Could that also be done for this quiz
or might that be next week’s task? :slight_smile:


#5

this quiz is probably easier than usually, as, for the first time
ever, i felt up to it, and created a solution in not so much time.
i’ll be posting it in 48 hrs :wink:
greetings, Dirk.

2006/4/21, James Edward G. II removed_email_address@domain.invalid:


#6

Given a word like “there’s” or “that’s”, does the letter before the
apostrophe count as a “last” letter? In other words, could “that’s”
become “ttha’s”?

In the example above, there’s no case where that letter gets
scrambled. It’s possible that that’s coincidence, but it doesn’t look
like it.

Do it whichever way you like it…

I don’t know what the study said about contractions, if anything.
Personally, I think I would consider the parts before and after as
separate words, which would be slightly less scrambled, but my
intuition (which could be wrong) says that counting it as one whole
word might throw off legibility more than expected.


#7

On Fri, Apr 21, 2006 at 11:16:38PM +0900, Dirk M. wrote:
} this quiz is probably easier than usually, as, for the first time
} ever, i felt up to it, and created a solution in not so much time.
} i’ll be posting it in 48 hrs :wink:

Yes, I sent my solution directly to James. He or I will repost it on
Sunday.

} greetings, Dirk.
–Greg

} 2006/4/21, James Edward G. II removed_email_address@domain.invalid:
} > On Apr 21, 2006, at 8:10 AM, Florian Gro? wrote:
} >
} > > Ruby Q. 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.
} > >
} > > What about writing an unscrambler? Could that also be done for
this
} > > quiz or might that be next week’s task? :slight_smile:
} >
} > It’s not part of the challenge this week or next, but you know I’m
} > always for setting your own goals. :slight_smile:
} >
} > James Edward G. II
} >
}
}


#8

On Fri, Apr 21, 2006 at 11:27:08PM +0900, Gregory S. wrote:

On Fri, Apr 21, 2006 at 11:16:38PM +0900, Dirk M. wrote:
} this quiz is probably easier than usually, as, for the first time
} ever, i felt up to it, and created a solution in not so much time.
} i’ll be posting it in 48 hrs :wink:

Mmh, for my first participation, i get a quizz solved by a one-liner.
And still i discover things :slight_smile:

Yoann


#9

On Sat, Apr 22, 2006 at 12:24:06AM +0900, Matthew M. wrote:
} One line?
} Hmmm… Methinks I need to go back and reexamine my own solution.
:slight_smile:

Heh. I decided against making it a single line for readability purposes.
Yeah, it can be done in a single line, but it will be less readable and
less efficient.

–Greg

} On 4/21/06, Yoann G. removed_email_address@domain.invalid wrote:
} > On Fri, Apr 21, 2006 at 11:27:08PM +0900, Gregory S. wrote:
} > > On Fri, Apr 21, 2006 at 11:16:38PM +0900, Dirk M. wrote:
} > > } this quiz is probably easier than usually, as, for the first
time
} > > } ever, i felt up to it, and created a solution in not so much
time.
} > > } i’ll be posting it in 48 hrs :wink:
} > >
} >
} > Mmh, for my first participation, i get a quizz solved by a
one-liner.
} > And still i discover things :slight_smile:
} >
} > Yoann
} >
} >
}
}


#10

my attempts to one-line it failed, and since it was now three lines
anyway, i decided to expand the entire thing for readability.
greetings, Dirk.

2006/4/21, Gregory S. removed_email_address@domain.invalid:


#11

One line?
Hmmm… Methinks I need to go back and reexamine my own solution. :slight_smile:


#12

On Apr 21, 2006, at 9:27 AM, Gregory S. wrote:

On Fri, Apr 21, 2006 at 11:16:38PM +0900, Dirk M. wrote:
} this quiz is probably easier than usually, as, for the first time
} ever, i felt up to it, and created a solution in not so much time.
} i’ll be posting it in 48 hrs :wink:

Yes, I sent my solution directly to James.

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é.
James Edward G. II


#13

On Sat, Apr 22, 2006 at 02:28:48AM +0900, James Edward G. II wrote:
} On Apr 21, 2006, at 9:27 AM, Gregory S. wrote:
}
} >On Fri, Apr 21, 2006 at 11:16:38PM +0900, Dirk M. wrote:
} >} this quiz is probably easier than usually, as, for the first time
} >} ever, i felt up to it, and created a solution in not so much time.
} >} i’ll be posting it in 48 hrs :wink:
} >
} >Yes, I sent my solution directly to James.
}
} 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?.

Yeah, it treats accented characters as punctuation. The two regexes
could
be changed to handled accented characters, but I leave that as an
exercise
for the reader .

} James Edward G. II
–Greg


#14

Hi –

On Fri, 21 Apr 2006, Matthew M. wrote:

I don’t know what the study said about contractions, if anything.
Personally, I think I would consider the parts before and after as
separate words, which would be slightly less scrambled, but my
intuition (which could be wrong) says that counting it as one whole
word might throw off legibility more than expected.

I guess if the part after the ’ isn’t going to be mixed in with the
part before (which I definitely don’t think it should be), then the
part before does really count as a word, so its first and last letters
would be preserved.

there’s => terhe’s but not theer’s

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!


#15

Looks like this Quiz is too easy.
May I suggest smthng to make it more challenging?

  1. Vowel can be exchanged with vowel only,
    consonant can be exchanged with consonant only;
  2. Parameterize the solution, so that set of exchangeable characters
    classes
    can be specified (optionally);
    Sorry, if I’m breaking the Quiz rules.

Happy Rubying,
Sergey

----- Original Message -----
From: “Matthew M.” removed_email_address@domain.invalid
Sent: Friday, April 21, 2006 11:24 AM

One line?
Hmmm… Methinks I need to go back and reexamine my own solution. :slight_smile:


#16

On Apr 21, 2006, at 12:26 PM, Sergey V. wrote:

Looks like this Quiz is too easy.

I’ll tell you what I told David Black off-list:

One of the easiest ones we have ever done, yes, and I think that’s a
good thing.

Programmers of all skill levels follow the quiz and it’s a common
complaint that all the problems are “advanced” material. I’m trying
to get better about that.

May I suggest smthng to make it more challenging?

  1. Vowel can be exchanged with vowel only,
    consonant can be exchanged with consonant only;
  2. Parameterize the solution, so that set of exchangeable
    characters classes
    can be specified (optionally);

The Ruby Q.zes are ideas. If you need to add this to challenge
yourself, go for it. We won’t come and take your keyboard away, I
promise. :wink:

James Edward G. II


#17

On 4/21/06, Sergey V. removed_email_address@domain.invalid wrote:

Looks like this Quiz is too easy.
May I suggest smthng to make it more challenging?

  1. Vowel can be exchanged with vowel only,
    consonant can be exchanged with consonant only;
  2. Parameterize the solution, so that set of exchangeable characters classes
    can be specified (optionally);
    Sorry, if I’m breaking the Quiz rules.

James (being the host) can put forth his own comments on the goals of
the quiz. But I think there’s plenty of room for both simple and
mildly complex problems; the simpler ones especially give newbies some
fun.

Feel free to add more to your own code if you like. Since I’ll be
writing the summary, I’ll be looking at all the solutions and will
comment on extras as I find them interesting, appropriate, etc.


#18

On 4/21/06, James Edward G. II removed_email_address@domain.invalid wrote:

Random playing around…

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

My solution also did not munge résumé. Seems that \w does not include
accented characters, so résumé becomes the words r and sum, which are
too small to be munged. Is there something one can require to make \w
include non-Latin characters?

Ryan


#19

On Apr 21, 2006, at 12:33 PM, Gregory S. wrote:

}

Yeah, it treats accented characters as punctuation. The two regexes
could
be changed to handled accented characters, but I leave that as an
exercise
for the reader .

Really? With everyone bragging about how easy this quiz is? :smiley:

James Edward G. II


#20

you know, it’s great that this quiz is easy! when i had read the other
ones, i’d feel hopeless and most of the times, i wouldn’t even try,
but with this quiz, i knew i could come up with a solution, which made
me study the MatchData class, which’ll come in handy for sure some
day!
greetings, Dirk.

2006/4/21, Mike removed_email_address@domain.invalid: