Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Word Loop (#149) [SOLUTION]

View parsed - Show only message text

Path: g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!news.stack.nl!talisker.lacave.net!lacave.net!not-for-mail
From: Pawel Radecki <pawel.j.rade...@gmail.com>
Newsgroups: comp.lang.ruby
Subject: Re: Word Loop (#149) [SOLUTION]
Date: Sun, 9 Dec 2007 15:46:04 -0500
Organization: Service de news de lacave.net
Lines: 118
Message-ID: <475C5403.6000108@gmail.com>
References: <20071207204436.OYTN15836.eastrmmtao103.cox.net@eastrmimpo03.cox.net> <55bd3bfd-2014-4ab6-9334-e8a04ba64d48@r1g2000hsg.googlegroups.com> <4ef8bec90712091210k6e1e6a1dw3076dd44ce824623@mail.gmail.com>
NNTP-Posting-Host: bristol.highgroove.com
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: talisker.lacave.net 1197233183 36312 65.111.164.187 (9 Dec 2007 20:46:23 GMT)
X-Complaints-To: abuse@lacave.net
NNTP-Posting-Date: Sun, 9 Dec 2007 20:46:23 +0000 (UTC)
In-Reply-To: <4ef8bec90712091210k6e1e6a1dw3076dd44ce824623@mail.gmail.com>
X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway 
X-Mail-Count: 282813
X-Ml-Name: ruby-talk
X-Rubymirror: Yes
X-Ruby-Talk: <475C5403.6000...@gmail.com>

And here is my solution. It tries to find first suitable letter
repetition and print the whole word on the screen with the very tight
loop. Suitable repetition exists when distance between identical letters
is an even number greater or equal to four.


#!/usr/bin/env ruby

# Solution to Ruby Quiz #149 (see http://www.rubyquiz.com/quiz149.html)
# by Pawel Radecki (pawel.j.rade...@gmail.com).


require 'logger'

$LOG = Logger.new($stderr)

#logging
#$LOG.level = Logger::DEBUG #DEBUG
$LOG.level = Logger::ERROR  #PRODUCTION

NO_LOOP_TEXT = "No loop."

class String
    private
    def compose_word_loop_array (index1, index2)
        a = Array.new(self.length) {|i| Array.new(self.length, " ") }

        i=0
        while (i<index1)
            a[1][i] = self[i].chr
            i+=1
        end

        #repeated letter, first occurrence, loop point
        a[1][index1]=self[index1].chr

        i=index1+1
        boundary = (index2-index1)/2+index1
        while(i<boundary)
            a[1][i] = self[i].chr
            i+=1
        end

        i=index2-1; j=index1
        while(i>boundary-1)
            a[0][j] = self[i].chr
            j+=1; i-=1
        end

        i=index2+1; j=2
        while (i<self.length)
            a[j][index1] = self[i].chr
            i+=1; j+=1
        end

        #cut all empty rows
        a.slice!(j..self.length-1)
        a
    end

    public
    def word_loop
        if (self.length<=4)
            return NO_LOOP_TEXT
        end
        s = self
        index1 = index2 = nil
            #find repeated letter suitable for a loop by
            #taking 1st letter and comparing to 5th, 7th, 9th, 11th, etc.
            #taking 2nd letter and comparing to 6th, 8th, 10th, 12th, etc.
            #taking 3rd letter and comparing to 7th, 9th, 11th etc.
            #etc.
            i = 0
            while i<s.length-1
                j=i+4
                while j<s.length
                    $LOG.debug("i: #{i}")
                    $LOG.debug("j: #{j}")
                    $LOG.debug("s[i]: #{s[i].chr}")
                    $LOG.debug("s[j]: #{s[j].chr}")
                    $LOG.debug("\n")
                    if s[i] == s[j]
                        return compose_word_loop_array(i, j)
                    end
                    j+=2
                end
                i+=1
            end
        return NO_LOOP_TEXT
    end
end

USAGE = <<ENDUSAGE
Usage:
   word_loop <message>
ENDUSAGE

if ARGV.length!=1
    puts USAGE
    exit
end

input_word = ARGV[0]
a = input_word.word_loop
if a.instance_of? Array
    a.each {|x| puts x.join("") }
else
    print a
end

exit

-- 
Paweł Radecki
m: +48 695 34-64-76
e: pawel.j.rade...@gmail.com
w: http://radeckimarch.blogspot.com/


Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google