[SOLUTION] Quiz #76


#1

I sincerely hope I am doing this correctly, as…

(1) I have never posted to a mailing list.
(2) I have never participated in Ruby Q.
(3) I really hope I am posting at the right time, place, etc

I apologise if I did this incorrectly.

Here was my solution to the Ruby Q.:

#By Jesse H-K
#On the date of Sunday April 23, 2006

class String
#Take a string, and scramble the center of each word.
def scramble
#find each word and replace it with…
gsub(/\w+/) do |word|
if (1…3) === word.length
#…the word if it’s length is 1, 2, or 3. These words cannot be
scrambled.
word
else
#…the first character, plus the scrambled middle, and ending with
the last character.
word[0].chr + word[1…(-2)].scan(/./).sort_by { rand }.join +
word[-1].chr
end
end
end
end

while str = gets
puts str.scramble
end

I don’t have any formal education with programming, but this was an
easier challenge, so I thought I’d try my hand at it.

Since nothing in the String could be affected besides words, I figured I
would use the built in String#gsub . I simply find each word using the
regex /\w+/ (which will find 1 or more letters), and return it if its
length is from 1 to 3. This is because these words cannot be altered in
any way. For all the other words, I take the first character, and
seperate the middle of the string into and Array of letters using
String#scan. This Array is randomely shuffled with Enumerable#sort_by,
and join back tpogether into a String. Lastly, the final character is
appended.

To use my example, either run with a file as a command line argument, or
enter text as an input-result loop.

This was a great way to get involved in Ruby Q… Thanks to all those
that organize it.


#2

On Apr 23, 2006, at 8:49 AM, Jesse H-K wrote:

I sincerely hope I am doing this correctly, as…

(1) I have never posted to a mailing list.
(2) I have never participated in Ruby Q.
(3) I really hope I am posting at the right time, place, etc

I apologise if I did this incorrectly.

Looks perfect to me.

I don’t have any formal education with programming, but this was an
easier challenge, so I thought I’d try my hand at it.

You did a nice job. Thanks for sending it in!

Since nothing in the String could be affected besides words, I
figured I
would use the built in String#gsub . I simply find each word using the
regex /\w+/ (which will find 1 or more letters)

And numbers and s. :wink: \w is equivalent to [A-Za-z0-9], just FYI.

James Edward G. II