From: James Edward Gray II Date: 2006-04-24T04:48:36+09:00 Subject: Fwd: [SOLUTION] Text Munger (#76) --Apple-Mail-1-10778482 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Begin forwarded message: > From: "Sergey Volkov" > Date: April 22, 2006 5:38:27 AM CDT > To: > Cc: > Subject: [SOLUTION] Text Munger (#76) > > James, > please find subj attached; > > let me guess - this Quiz will beat solutions number record! > > thanks > regards > Sergey Volkov > > ----- Original Message ----- From: "James Edward Gray II" > > To: "ruby-talk ML" > Sent: Friday, April 21, 2006 1:34 PM > Subject: Re: [QUIZ] Text Munger (#76) > > >> On Apr 21, 2006, at 12:26 PM, Sergey Volkov 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 Quizzes 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. ;) >> James Edward Gray II --Apple-Mail-1-10778482 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0666; name="SV.rb" Content-Disposition: attachment; filename=SV.rb #!/bin/ruby -w # ## Sergey Volkov ## Ruby Quiz #76 - Text Munger (by Matthew Moss) ## Thanks Matthew - it was fun! # =begin On Apr 21, 2006, at 12:26 PM, Sergey Volkov wrote: > 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); On Apr 21, 2006, at 1:34 PM, James Edward Gray II replied: >.. > The Ruby Quizzes 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. ;) Thanks James, your promise is very encouraging! =end class String # intermix characters matching rx (default /./) def intermix! rx=/./ pos, pp = -1, [] # process every matching char index while pos = self.index( rx, pos+1 ) if pp[0] # select randomly previously saved pos rp = pp[rand(pp.size)] # swap self[pos], self[rp] = self[rp], self[pos] end # save current pos pp << pos end # return modified string self end end # character classes # change them for localization (I didn't tested) VOWEL = /[aeyuio]/i CONSONANT = /[#{('b'..'z').to_a.join.gsub!(VOWEL, '')}]/i WORDCHAR = /[#{('a'..'z').to_a.join}]/i # specify -d to process text from the end of this file text = (ARGV[0]=='-d' ? DATA : ARGF).read print text.gsub!( /#{WORDCHAR}{3,}'?/ ){ |word| epos = word.size - (word[-1]==?\' ? 1 : 2) # I was lazy to implement command line option for mix mode.. # mix VOWELs word[1..epos]=word[1..epos].intermix!(VOWEL) # mix CONSONANTs # word[1..epos]=word[1..epos].intermix!(CONSONANT) # we can mix all letters #word[1..epos]=word[1..epos].intermix!(WORDCHAR) word } || text __END__ Execute 'SV.rb -d' and compare output with_the_text below. If you can understand easyly, try to uncomment line 59 :) Assumptions: processing words as sequence of characters matching regular expression in WORDCHAR constant possibly terminated with apostrophe (single quote) Constraints: consonant-consonant, vowel-vowel exchange only, that's my challenge; localization is out of scope, that's my decision; Enough testing phrases, better have pure fun playing with: $ fortune | ruby SV.rb Dametitconoun is like sex: when it is good, it is very, very good; and when it is bad, it is better than nintohg. -- Dick Bdornan --Apple-Mail-1-10778482 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-1-10778482--