Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
39 lines (27 sloc) 637 Bytes

Spiral matrix generator

Instructions

Given integer n implement a function which generates a spiral numbers matrix (size n x n). Numbers in matrix are placed according to spiral - starting top left corner and then go right, down, left, up, right...

Puzzle | Solution

Examples

Example 1

generateSpiralMatrix(2)
   [[1, 2],
   [4, 3]]

Example 2

generateSpiralMatrix(3)
   [[1, 2, 3],
   [8, 9, 4],
   [7, 6, 5]]

Example 3

generateSpiralMatrix(4)
   [[1,   2,  3, 4],
   [12, 13, 14, 5],
   [11, 16, 15, 6],
   [10,  9,  8, 7]]
You can’t perform that action at this time.