Text Munger (#76)

On 4/23/06, Albert Vernon S. [email protected] wrote:
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

From rubyquiz.com:

Where should I send my solutions?
Ideally, solutions should be sent to the Ruby T. mailing list for
all to see and learn from. All solutions sent to Ruby T. are
archived with the quiz. If you do not subscribe to Ruby T., 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 Apr 23, 2006, at 12:52 PM, Daniel H. wrote:

now there is:

  • Original ruby quiz thread
  • [QUIZ][SOLUTION] …
  • [QUIZ] … A solution
  • [QUIZ] … A simplistic solution
  • [SOLUTION] …

Done.

James Edward G. II

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

From rubyquiz.com:

Where should I send my solutions?
Ideally, solutions should be sent to the Ruby T. mailing list
for all to see and learn from. All solutions sent to Ruby T. are
archived with the quiz. If you do not subscribe to Ruby T., 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.

Thank you. I have updated the FAQ.

James Edward G. II

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.

My solution:

=== snip ===

Ruby Q. 76

Ruby Quiz - Text Munger (#76)

Solution of Tom M.

http://blog.moertel.com/

2006-04-21

Usage: munge.rb [inputs…]

class String
def munge!
(length - 2).downto(2) do |i|
j = rand(i) + 1
self[i], self[j] = self[j], self[i]
end
self
end
end

while line = gets
puts line.gsub(/\w+/) { |s| s.munge! }
end

=== end ===

A few notes:

I took the term “scramble” in the task definition to mean randomly
permute because some occurrences of words in the example text were
apparently unchanged by the scrambling transformation (e.g., “keep” and
“being” in the tenth line) and some words that had multiple
occurrences were scrambled differently for each occurrence (e.g.,
“remvpidtee” in the second line vs. “retpmevide” in the fourth from the
last line).

str.munge! (fairly) permutes the inner characters of +str+ and has no
effect on strings of three or fewer characters.

I used +gets+ in the main I/O loop in order to get sensible command-line
input handling for free.

Cheers,
Tom

Hello,

Here is my solution to the quiz.
It’s not a one-liner anymore - i’ve left the first version in the
comments, for historical purposes.

1st try:

does not scramble abcd123, which may or not be a good thing

no support for accented characters

_ is considered a letter

#puts ARGF.read.gsub(/\b(?=\D+\b)(\w)(\w+)(?=\w\b)/) { $1 +
$2.split(’’).sort_by{rand}.join }

class String
# returns the string with characters randomly placed
def randomize
split(’’).sort_by{rand}.join
end

# character class to identify a word's letter
# arbitrarily ripped from iso-8859-1
WordChars = '[a-zA-Z\xc0-\xd6\xd8-\xf6\xf8-\xfd\xff]'

# randomizes each word (defined by +chars+), leaving alone the
# first and last letters
# uses a default argument to fit in 80 cols :)
def scramble_words(chars = WordChars)
	gsub(/(#{chars})(#{chars}+)(?=#{chars})/) { $1 + $2.randomize }
end

end

puts ARGF.read.scramble_words if FILE == $0

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

James G. wrote:

Is it not the second link on this page?

Sorry, I shouldn’t have said “add.” I meant could you remove the first
listing and keep the seconds.

On Apr 24, 2006, at 9:42 AM, Alex Barrett wrote:

James G. wrote:

Is it not the second link on this page?

Sorry, I shouldn’t have said “add.” I meant could you remove the
first
listing and keep the seconds.

Nothing wrong with showing progress, is there?

I always handle the quiz solutions that way. See the past problems
for examples.

James Edward G. II

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

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

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

On Apr 24, 2006, at 10:53 AM, Alex Barrett wrote:

Aye, usually I’d agree with you. But in this case the two examples are
almost exactly the same. Just changing it to work with input
instead of
a string.
The first one I posted was in reply to a post about one-liners. Not
intended as a seperate submission.

I have removed the first link.

James Edward G. II

James G. wrote:

Nothing wrong with showing progress, is there?

I always handle the quiz solutions that way. See the past problems
for examples.

James Edward G. II

Aye, usually I’d agree with you. But in this case the two examples are
almost exactly the same. Just changing it to work with input instead of
a string.
The first one I posted was in reply to a post about one-liners. Not
intended as a seperate submission.

And what’s with the “3D’s” in the mailing list archives?

On 4/23/06, Gregory B. [email protected] wrote:

On 4/23/06, James Edward G. II [email protected] 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:

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


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

This is my first try:
puts $<.read.split(/\W/).map{|x|x==“”||nil
?“”:“#{x[0…0]}#{x[1…a=x.size-1].split(//).sort{rand}}#{x[a…a+1]}
“}*””

but this doesn’t work on single letter words & I wanted to use inject.
My final version:

puts
$<.inject([]){|a,w|a<<w.gsub(/\B(\w+)\B/){$1.split(‘’).sort_by{rand}}}

j`ey
http://www.eachmapinject.com