From: Nasir Khan Date: 2007-01-15T01:52:18+09:00 Subject: Re: [QUIZ] Number Spiral (#109) ------=_Part_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 += 1 @p = 0 if @p == self.length self[@p] end end class ClockState def initialize @seq = [:left, :up, :right, :down] @count = 1 @count_state = 0 @times = 0 @val = @seq.cnext end def next if @count == @count_state @val = @seq.cnext @count_state = 0 @times += 1 if @times == 2 @count += 1 @times = 0 end end @count_state += 1 @val end end class Spiral def initialize(dim) @m = [] dim.times do @m << Array.new(dim, 0) end @x = dim/2 @y = dim/2 @val = 0 @sz = dim end def left @x -= 1 end def up @y -= 1 end def right @x += 1 end def down @y +=1 end def make_spiral dir_hash={:dir=>:clock} c = ClockState.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 += 1 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 = Spiral.new(20) s.make_spiral :dir=>:clock s.print_spiral #--- On 1/12/07, Ruby Quiz 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 > > ------=_Part_64782_15673789.1168793534228--