On Fri, 07 Dec 2007 15:45:02 -0500, Ruby Quiz wrote:

> The three rules of Ruby Quiz:
> 
> 1.  Please do not post any solutions or spoiler discussion for this quiz
> until 48 hours have passed from the time on this message.
> 
> 2.  Support Ruby Quiz by submitting ideas as often as you can:
> 
> http://www.rubyquiz.com/
> 
> 3.  Enjoy!
> 
> Suggestion:  A [QUIZ] in the subject of emails about the problem helps
> everyone on Ruby Talk follow the discussion.  Please reply to the
> original quiz message, if you can.
> 
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=
> 
> Here's a fun little challenge from the Educational Computing
> Organization of Ontario.
> 
> Given a single word as input try to find a repeated letter inside of it
> such that you can loop the text around and reuse that letter.  For
> example:
> 
> 	$ ruby word_loop.rb Mississippi
> 	 i
> 	 p
> 	 p
> 	Mis
> 	 ss
> 	 si
> 
> or:
> 
> 	$ ruby word_loop.rb Markham
> 	Ma
> 	ar
> 	hk
> 
> or:
> 
> 	$ ruby word_loop.rb yummy
> 	yu
> 	mm
> 
> If a loop cannot be made, your code can just print an error message:
> 
> 	$ ruby word_loop.rb Dana
> 	No loop.

def loopword word
  matchinfo=
    word.match(/(.*?)(.)(.(?:..)+?)\2(.*)/i)
  if matchinfo
    _,before,letter,looplets,after=matchinfo.to_a
    pad=" "*before.size
    after.reverse.split(//).each{|l| puts pad+l}
    looplets=looplets.split(//)
    puts before+letter+looplets.shift
    until looplets.empty?
      puts pad+looplets.pop+looplets.shift
    end
  else
    puts "No loop."
  end
end

loopword "Mississippi"
puts
loopword "Markham"
puts
loopword "yummy"
puts
loopword "Dana"
puts
loopword "Organization"


outputs:

 i
 p
 p
Mis
 ss
 si

Ma
ar
hk

yu
mm

No loop.

n
Or
ig
ta
an
zi

(The last is a testcase which makes sure that I get the #shifts and #pops 
right)

-- 
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/