From: Ross Bamford Date: 2006-04-23T21:53:10+09:00 Subject: Re: [QUIZ][SOLUTION] Text Munger (#76) Seems like forty-eight hours are up now, so here are my solutions for this quiz, it was good to get a quick one :) I wrote a simple random munging solution, and a slightly longer one that munges only part of the words. I went for a different way on the latter one, just to play with regexps a bit, but I expect its performance isn't great... Both support unicode properly, as long as the -Ku stays on the ruby command line ;) I could have used the u modifier instead but wanted to save on the repetition. # ========= random munging #!/usr/local/bin/ruby -Ku $stdout << ARGF.read.gsub(/\B((?![\d_])\w{2,})\B/) do |w| $&.split(//).sort_by { rand } end # (easily compresses to:) #!/usr/local/bin/ruby -npKu gsub(/\B((?![\d_])\w){2,}\B/){$&.split(//).sort_by{rand}} # ========= slightly-less-random munging #!/usr/local/bin/ruby -Ku RX = Hash.new{|h,k|h[k]=/(.{#{(k/4.0).round}})#{'(.)'*(k/2.0).round}(.*)/} $stdout << ARGF.read.gsub(/((?![\d_])\w){4,}/) do |w| (caps = RX[w.split(//u).length].match(w).captures).first + caps[1..-2].sort_by { rand }.to_s + caps.last end -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk