Re: Word Loop (#149)

#A loop is possible whenever there are two of the same lettter an even
distance
#greater than 2 from each other
def first_loop(letters)
0.upto(letters.length-1) do |idx1|
(idx1+4).step(letters.length-1,2) do |idx2|
return [idx1,idx2] if letters[idx1].casecmp(letters[idx2])==0
end
end
nil
end

letters = ARGV.first.split(//)

first,last = first_loop(letters)

if first==nil
puts “No loop”
exit
end

letters[(last+1)…-1].reverse_each {|l| puts ’ '*first + l}
puts letters[0…(first+1)].join
1.upto((last-first-1)/2) do |n|
puts ’ '*first + letters[last-n] + letters[first+1+n]
end

----- Original Message ----
From: Ruby Q. [email protected]
To: ruby-talk ML [email protected]
Sent: Friday, December 7, 2007 2:45:02 PM
Subject: [QUIZ] Word Loop (#149)

The three rules of Ruby Q.:

  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 Q. by submitting ideas as often as you can:

http://www.rubyquiz.com/

  1. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby T. 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.






  ____________________________________________________________________________________

Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now.
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs