[QUIZ] Text Munger (#76)

James Edward Gray II

···

On Apr 23, 2006, at 4:45 AM, Himadri Choudhury wrote:

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.

From rubyquiz.com:

Where should I send my solutions?
Ideally, solutions should be sent to the Ruby Talk mailing list for all to see and learn from. All solutions sent to Ruby Talk are archived with the quiz. If you do not subscribe to Ruby Talk, you may send your messages to me and I will forward them to the list for you. Solutions are easy to find if your message subject includes a [SOLUTION], so that's probably the best tactic to make sure your work is recognized.

-a

···

On 23.4.2006, at 17:52, Daniel Harple wrote:

On Apr 21, 2006, at 2:34 PM, Ruby Quiz wrote:

The three rules of Ruby Quiz:
[...]
Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion.

Can you also suggest that people reply to the original thread instead of making new ones when they send their solutions? Right now there is:

- Original ruby quiz thread
- [QUIZ][SOLUTION] ...
- [QUIZ] ... A solution
- [QUIZ] ... A simplistic solution
- [SOLUTION] ...

-- Daniel

Done.

James Edward Gray II

···

On Apr 23, 2006, at 12:52 PM, Daniel Harple wrote:

On Apr 21, 2006, at 2:34 PM, Ruby Quiz wrote:

The three rules of Ruby Quiz:
[...]
Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion.

Can you also suggest that people reply to the original thread instead of making new ones when they send their solutions? Right now there is:

- Original ruby quiz thread
- [QUIZ][SOLUTION] ...
- [QUIZ] ... A solution
- [QUIZ] ... A simplistic solution
- [SOLUTION] ...

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 Gray II <james@grayproductions.net>:

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

> Ruby Quiz 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 Gray II

Hi --

···

On Fri, 21 Apr 2006, Matthew Moss wrote:

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.

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 (dblack@wobblini.net)
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!

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 Apr 21, 2006, at 2:09 PM, Ryan Leavengood wrote:

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 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

···

On Sat, 22 Apr 2006, Ryan Leavengood wrote:

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

--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama

How about COBOL or FORTRAN? Or is that a spoiler for a different reason? :wink:

···

On 4/23/06, James Edward Gray II <james@grayproductions.net> wrote:

For the record, I do consider posting solutions in other languages
(like Perl) a spoiler.

Also, for a swap method to give random results doesn't one need to
swap from a random position in the array which has not been passed
through yet? (see Wikipedia, the free encyclopedia

/Shuffle noting Fisher-
Yates shuffling.)

Yes. You are right. My memory didn't serve me well in this case.
Instead of j = rand(i+1), it should have been: j = i + rand(x.length-i)

Thanks for the reference.

Himadri

···

On 4/23/06, Albert Vernon Smith <smithav@cshl.edu> wrote:

On 4/23/06, Albert Vernon Smith <smithav@cshl.edu> wrote:

Is the performance better if you skip swaps when i == j ?

Also, for a swap method to give random results doesn't one need to
swap from a random position in the array which has not been passed
through yet? (see Shuffling - Wikipedia noting Fisher-
Yates shuffling.)

-a

On 23.4.2006, at 09:45, Himadri Choudhury wrote:

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

Thank you. I have updated the FAQ.

James Edward Gray II

···

On Apr 23, 2006, at 1:02 PM, Albert Vernon Smith wrote:

From rubyquiz.com:

Where should I send my solutions?
Ideally, solutions should be sent to the Ruby Talk mailing list for all to see and learn from. All solutions sent to Ruby Talk are archived with the quiz. If you do not subscribe to Ruby Talk, you may send your messages to me and I will forward them to the list for you. Solutions are easy to find if your message subject includes a [SOLUTION], so that's probably the best tactic to make sure your work is recognized.

@James Gray: Can you please only add my second listing on rubyquiz.com.
They are both effectively the same; only one has less characters.
Thanks.

···

--
Posted via http://www.ruby-forum.com/.

} 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

···

On Fri, Apr 21, 2006 at 11:16:38PM +0900, Dirk Meijer wrote:

} 2006/4/21, James Edward Gray II <james@grayproductions.net>:
} > On Apr 21, 2006, at 8:10 AM, Florian Gro? wrote:
} >
} > > Ruby Quiz 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 Gray II
} >
}
}

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

···

--
Posted via http://www.ruby-forum.com/\.

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.... <shudder>.)

···

On 4/21/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Sat, 22 Apr 2006, Ryan Leavengood wrote:

> 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

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:

ara.t.howard@noaa.gov 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 Quiz.

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 Hagelberg

i went golfing and got one line : 96 chars

For reference:

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

(65 characters)

···

--
Posted via http://www.ruby-forum.com/\.

Is it not the second link on this page?

http://www.rubyquiz.com/quiz76.html

If not, please send me a link to the message.

James Edward Gray II

···

On Apr 24, 2006, at 6:55 AM, Alex Barrett wrote:

@James Gray: Can you please only add my second listing on rubyquiz.com.
They are both effectively the same; only one has less characters.
Thanks.

James was talking about languages, wasn't he :))

···

On 4/23/06, Gregory Brown <gregory.t.brown@gmail.com> wrote:

On 4/23/06, James Edward Gray II <james@grayproductions.net> wrote:

> For the record, I do consider posting solutions in other languages
> (like Perl) a spoiler.

How about COBOL or FORTRAN? Or is that a spoiler for a different reason?
:wink:

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

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

Yoann

···

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

On Fri, Apr 21, 2006 at 11:16:38PM +0900, Dirk Meijer 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:

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 Gray II

···

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

On Fri, Apr 21, 2006 at 11:16:38PM +0900, Dirk Meijer 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.