# Re: Word Loop (#149)

**URL:** <https://www.ruby-forum.com/t/re-word-loop-149/122587>\
**Category:** Ruby\
**Created:** [December 9, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/re-word-loop-149/122587 "2007-12-09T21:49:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jameskelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jameskelly](https://www.ruby-forum.com/u/jameskelly)\
**Post date:** [December 9, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/re-word-loop-149/122587/1 "2007-12-09T21:49:06Z")

</div>

#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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](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](http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ)
