[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [QUIZ] Word Loop (#149

James Koppel

12/9/2007 8:48:00 PM

#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 Quiz <james@grayproductions.net>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Friday, December 7, 2007 2:45:02 PM
Subject: [QUIZ] Word Loop (#149)


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.rub...

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.






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