From: Martin DeMello on 14 Jan 2007 14:20 On 1/15/07, Martin DeMello wrote:> n = ARGV[0].to_i > square = Array.new(n+2) { Array.new(n+2) } oops - didn't read the question carefully enough. ignore. m. From: Matthew Moss on 14 Jan 2007 14:24 My first attempt... A recursive solution recognizing than a spiral of even dimension can be formed by a top row, a left column, and an odd spiral. Likewise, an odd spiral is a smaller even spiral with a right column and bottom row. The functions erow and orow reflect the even/odd-ness of the spiral, not the row. DIM = ARGV[0].to_i FLD = (DIM ** 2 - 1).to_s.size + 2 def fmt(x) " " * (FLD - x.to_s.size) + x.to_s end def orow(n, i) m = n ** 2 x = m - n if i == n - 1 (1..n).inject("") { |o, v| o + fmt(m - v) } else erow(n - 1, i) + fmt(x - n + i + 1) end end def erow(n, i) m = n ** 2 x = m - n if i == 0 (0...n).inject("") { |o, v| o + fmt(x + v) } else fmt(x - i) + orow(n - 1, i - 1) end end def spiral(n) if (n % 2).zero? n.times { |i| puts erow(n, i) } else n.times { |i| puts orow(n, i) } end end spiral(ARGV[0].to_i) From: Tom Ayerst on 14 Jan 2007 14:46 My answers assumes an odd numbered spirals (I inferred it from "The number zero represents the center of the spiral"). Sorry for my beginners ruby (are there some standard min(x,y)/max(x,y,) functions?) The approach is to work out a standard equation for the value in any cell (I ended up with two, for the top left and bottom right) and then to iterate through each cell and calculate the value. The algorithm is stateless. class SpiralMaker def make_spiral(size) # only allow odd numbered squares (as zero is centre) if (size.modulo(2) == 0) exit(1) end #step along row (1..size).each do |y| # step down columns (1..size).each do |x| # are we in top left or bottom right half of spiral? if (y+x <= size) # top left - calculate value sn = size - (2 * (min(x,y) - 1)) val = (sn*sn) - (3*sn) + 2 - y + x else # bottom right - calculate value sn = size - (2 * (size - max(x,y))) val = (sn*sn) - sn + y - x end # Print value STDOUT.printf "%03d ", val end # Next line STDOUT.print "\n" end end def min(a,b) (a <= b) ? a : b end def max(a,b) (a >= b) ? a : b end end maker = SpiralMaker.new maker.make_spiral 21 From: Simon Kröger on 14 Jan 2007 14:49 Dear Ruby Quiz, this isn't really a solution to the quiz 109 because it violates some (if not all) of the rules. But as James noted there was a code golf problem very similar to this quiz and here is my solution to that. (see http://codegolf.com/oblongular-number-spirals for detailed description of the code golf problem) ---------------------------------------------------------------- s,f=1,proc{|x,y|y<1?[]:[[*s...s+=x]]+f[y-1,x].reverse.transpose} puts f[w=gets(' ').to_i,gets.to_i].map{|i|['%3i']*w*' '%i} ---------------------------------------------------------------- It draws a number spiral, starting with '1' in the upper left corner and the highest number in the middle, it also features spirals that are not quadratic. Yes, you will get some score at the codegolf site if you repost this solution there - but nowadays you will only get to Rank 9 with this solution and of course you will start to feel ill and you won't be able to sleep for days and other nasty things might happen if you do so. If someone can derive an even shorter solution from this i would be very interested to see it (the best ruby solution today has 7 bytes less) cheers Simon From: William James on 14 Jan 2007 17:33 Simon Kröger wrote:> Dear Ruby Quiz, > > this isn't really a solution to the quiz 109 because it violates > some (if not all) of the rules. But as James noted there was a > code golf problem very similar to this quiz and here is my > solution to that. > (see http://codegolf.com/oblongular-number-spirals for detailed > description of the code golf problem) > > ---------------------------------------------------------------- > s,f=1,proc{|x,y|y<1?[]:[[*s...s+=x]]+f[y-1,x].reverse.transpose} > puts f[w=gets(' ').to_i,gets.to_i].map{|i|['%3i']*w*' '%i} > ---------------------------------------------------------------- > > It draws a number spiral, starting with '1' in the upper left > corner and the highest number in the middle, it also features > spirals that are not quadratic. > > Yes, you will get some score at the codegolf site if you repost > this solution there - but nowadays you will only get to Rank 9 > with this solution and of course you will start to feel ill and > you won't be able to sleep for days and other nasty things might > happen if you do so. > > If someone can derive an even shorter solution from this i would > be very interested to see it (the best ruby solution today has 7 > bytes less) > > cheers > > Simon I can't get this to work. E:\Ruby>ruby try.rb 4 4 try.rb:2:in `%': too few arguments. (ArgumentError) from try.rb:2 from try.rb:2:in `map' from try.rb:2 First  |  Prev  |  Next  |  Last Pages: 1 2 3 4 5 6 Prev: Send over raw socket?Next: Web service client help needed