Re: Number Spiral (#109)

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

I think the standard idiom is to use the min/max functions of an array:

a = 5
b = 10
[a, b].max #=> 10

You can also give max a block, similar to sort:

a = “Hello”
b = “Hi”
[a, b].max {|x, y| x.length <=> y.length}

Or you can write a method so it works more like sort_by (the interface,
not the implementation):

class Array
def max_by &blk
max {|a, b| blk.call(a) <=> blk.call(b)}
end
end

a = “Hello”
b = “Hi”
[a, b].max {|x| x.length}

And then, in Ruby 1.9, you should be able to do this (using max_by from
above):

a = “Hello”
b = “Hi”
[a, b].max(&:length) # Not sure if the syntax is 100%

And if you still want your max() function:

def min(*args)
args.min
end

Everything also applies to minimums using the min function.

Dan

This one works for even and odd spirals (obvious really)

class SpiralMaker
def make_spiral(square_size)
# allow for even numbered squares by missing off the last row and
column
size = square_size
if (square_size.modulo(2) == 0)
square_size = square_size+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 <= square_size)
      # top left - calculate value
      sn = square_size - (2 * (min(x,y) - 1))
      val = (sn*sn) - (3*sn) + 2 - y + x
    else
      # bottom right - calculate value
      sn = square_size - (2 * (square_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 3

On Jan 14, 2007, at 1:46 PM, Tom A. wrote:

My answers assumes an odd numbered spirals (I inferred it from “The
number zero represents the center of the spiral”).

But the quiz example is an even spiral. :wink:

James Edward G. II

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs