From: Juraj Plavcan Date: 2007-12-11T00:06:46+09:00 Subject: Re: [QUIZ] Word Loop (#149) [SOLUTION] My solution... def find_knot letters #returns first and last index which become a "knot" letters.each_with_index do |letter1, ind1| (ind1+4).upto(letters.length - 1) do |ind2| if (ind2 - ind1) % 2 == 0 if letters[ind2].casecmp(letter1) == 0 return [ind1, ind2] end end end end return nil end def loop word letters = word.split(//) first, last = find_knot letters if first.nil? puts "No loop" else (letters.length-1).downto(last+1) { |i| print " "*first puts letters[i] } print letters[0..first+(last-first)/2-1] puts " "*first first.times {print " "} (last-1).downto(first+(last-first)/2) { |i| print letters[i] } puts end end loop "Mississippi" puts loop "Markham" puts loop "Yummy" puts loop "Dana" 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. > > >