session3/challenge/5_blocks.rb
# Write a method, spiral_access, that takes a 2d square array (an array of arrays)
# and a block, and calls the block for each of the elements, in spiral order.
#
# HINT: This method is probably best sovled recursively
#
# Example:
# two_d = [
#   [ 1,  2,  3,  4, 5],
#   [16, 17, 18, 19, 6],
#   [15, 24, 25, 20, 7],
#   [14, 23, 22, 21, 8],
#   [13, 12, 11, 10, 9],
# ]
# order = []
# spiral_access two_d do |i|
#   order << i
# end
# order # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
Write a method, spiral_access, that takes a 2d square array (an array of arrays)
and a block, and calls the block for each of the elements, in spiral order.

This method is probably best sovled recursively


Write a method spiral_access that takes a 2d square array ( an array of arrays ) and a block and calls the block for each of the elements in spiral order . This method is probably best sovled recursively
def spiral_access(square, i = 0, &block)
  col_min = i;   col_max = (square.first.length - 1)
  row_min = i;   row_max = (square.length - 1)
  return if col_max / 2 < i || row_max / 2 < i
  col_min.upto(col_max-i){ |col| block.call square[row_min][col] }
  (row_min+1).upto(row_max-i){ |row| block.call square[row][col_max-i] }
  (col_max-1-i).downto(col_min){ |col| block.call square[row_max-i][col]}
  (row_max-1-i).downto(row_min+1){ |row| block.call square[row][col_min]}
  spiral_access(square, i+=1, &block)
end
def spiral_access ( square , i = 0 , & block ) col_min = i ; col_max = ( square . first . length - 1 ) row_min = i ; row_max = ( square . length - 1 ) return if col_max / 2 < i || row_max / 2 < i col_min . upto ( col_max - i ) { | col | block . call square [ row_min ] [ col ] } ( row_min + 1 ) . upto ( row_max - i ) { | row | block . call square [ row ] [ col_max - i ] } ( col_max - 1 - i ) . downto ( col_min ) { | col | block . call square [ row_max - i ] [ col ] } ( row_max - 1 - i ) . downto ( row_min + 1 ) { | row | block . call square [ row ] [ col_min ] } spiral_access ( square , i += 1 , & block ) end
Write a method, spiral_access, that takes a 2d square array (an array of arrays)
and a block, and calls the block for each of the elements, in spiral order.
Write a method spiral_access that takes a 2d square array ( an array of arrays ) and a block and calls the block for each of the elements in spiral order .
#check when loop should end #push top row #push right column #push last row #push left column #recursion with i+= 1
square
<not_specific>
i
<not_specific>
block
<not_specific>