How to Make a Spiral in Java
The challenge
Your task, is to create a NxN spiral with a given size
.
For example, spiral with size 5 should look like this:
00000 ....0 000.0 0...0 00000
and with the size 10:
0000000000 .........0 00000000.0 0......0.0 0.0000.0.0 0.0..0.0.0 0.0....0.0 0.000000.0 0........0 0000000000
Return value should contain array of arrays, of `` and 1
, for example for given size 5
result should be:
|
|
Because of the edge-cases for tiny spirals, the size will be at least 5.
General rule-of-a-thumb is, that the snake made with ‘1’ cannot touch to itself.
The solution in Java code
Option 1:
|
|
Option 2:
|
|
Test cases to validate our solution
|
|