------art_64782_15673789.1168793534228
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Here is one that uses an empirical algorithm but with an array (Saw the
constraint a little late) . Uses :clock or :counter parameter to print
either spiral.


class Array
   def cnext
    @p || 1
    @p + 
    @p   if @p self.length
    self[@p]
   end
 end


class ClockState
  def initialize
    @seq  :left, :up, :right, :down]
    @count  
    @count_state  
    @times  
    @val  seq.cnext
  end

  def next
    if @count @count_state
      @val  seq.cnext
      @count_state  
      @times + 
      if @times 2
        @count + 
        @times  
      end
    end
    @count_state + 
    @val
  end
end




class Spiral
  def initialize(dim)
    @m  ]
    dim.times do
      @m << Array.new(dim, 0)
    end
    @x  im/2
    @y  im/2
    @val  
    @sz  im
  end

  def left
    @x - 
  end

  def up
    @y - 
  end

  def right
    @x + 
  end

  def down
    @y +  end

  def make_spiral dir_hashdirclock}
    c  lockState.new
    while ((@x < @sz) && (@y < @sz))
      if dir_hash[:dir]counter
        @m[@x][@y]  val
      elsif dir_hash[:dir]clock
        @m[@y][@x]  val
      else
        raise "Legal values are :clock and :counter"
      end
      self.send(c.next)
      @val + 
    end
  end

  def print_spiral
    fmt_sz  @sz*@sz).to_s.length + 2
    for i in 0...@sz do
      print "\n"
      for j in 0...@sz do
        printf("%#{fmt_sz}d", @m[i][j])
      end
    end
  end
end

s  piral.new(20)
s.make_spiral :dirclock
s.print_spiral

#---



On 1/12/07, Ruby Quiz <james / grayproductions.net> 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.
>
>
> --------------------
>
> by Bob Showalter
>
> (Taken from the puzzle by William Wu at
> http://www.ocf.berkeley.edu/~wwu/riddles/cs.shtml)
>
> [Editor's Note:  This was also a code golf problem a few months back:
> http://codegolf.com/oblongular-number-spirals  --JEG2]
>
> Write a Ruby program to print out a "spiral" of numbers that fill a NxN
> square.
> Your program will take a single argument to specify the dimensions of the
> square
> (1 or higher). The number zero represents the center of the spiral, and
> the
> succeeding integers spiral out in a clockwise (or counterclockwise; your
> choice)
> direction from the center until the square is filled.
>
> Your program should write the output line by line, without using an array
> to
> build up the data first.
>
> Here's the output for an 8x8 spiral:
>
>         56   57   58   59   60   61   62   63
>
>         55   30   31   32   33   34   35   36
>
>         54   29   12   13   14   15   16   37
>
>         53   28   11    2    3    4   17   38
>
>         52   27   10    1    0    5   18   39
>
>         51   26    9    8    7    6   19   40
>
>         50   25   24   23   22   21   20   41
>
>         49   48   47   46   45   44   43   42
>
>

------art_64782_15673789.1168793534228--