Avatar of artemkorsakov

artemkorsakov's solution

to Spiral Matrix in the Java Track

Published at Apr 06 2019 · 0 comments
Instructions
Test suite
Solution

Given the size, return a square matrix of numbers in spiral order.

The matrix should be filled with natural numbers, starting from 1 in the top-left corner, increasing in an inward, clockwise spiral order, like these examples:

Spiral matrix of size 3
1 2 3
8 9 4
7 6 5
Spiral matrix of size 4
 1  2  3 4
12 13 14 5
11 16 15 6
10  9  8 7

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension. https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

SpiralMatrixBuilderTest.java

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

public class SpiralMatrixBuilderTest {

    private SpiralMatrixBuilder spiralMatrixBuilder;

    @Before
    public void setUp() {
        spiralMatrixBuilder = new SpiralMatrixBuilder();
    }

    @Test
    public void testEmptySpiral() {
        int[][] expected = {};

        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(0));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTrivialSpiral() {
        int[][] expected = {
                {1}
        };

        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(1));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSpiralOfSize2() {
        int[][] expected = {
                {1, 2},
                {4, 3}
        };

        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(2));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSpiralOfSize3() {
        int[][] expected = {
                {1, 2, 3},
                {8, 9, 4},
                {7, 6, 5}
        };

        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(3));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSpiralOfSize4() {
        int[][] expected = {
                { 1,  2,  3,  4},
                {12, 13, 14,  5},
                {11, 16, 15,  6},
                {10,  9,  8,  7}
        };

        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(4));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSpiralOfSize5() {
        int[][] expected = {
                { 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}
        };

        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(5));
    }

}
class SpiralMatrixBuilder {
    private int currentRow = 0;
    private int currentColumn = 0;
    private int currentValue = 1;

    int[][] buildMatrixOfSize(int size) {
        if (size < 1) {
            return new int[][]{};
        }

        int[][] result = new int[size][];
        for (int i = 0; i < size; i++) {
            result[i] = new int[size];
        }

        result[0][0] = currentValue;
        for (int i = 0; i < size / 2 + 1; i++) {
            setLine(result);
        }

        return result;
    }

    private void setLine(int[][] result) {
        while (currentColumn + 1 < result[currentRow].length && result[currentRow][currentColumn + 1] == 0) {
            currentColumn++;
            currentValue++;
            result[currentRow][currentColumn] = currentValue;
        }

        while (currentRow + 1 < result.length && result[currentRow + 1][currentColumn] == 0) {
            currentRow++;
            currentValue++;
            result[currentRow][currentColumn] = currentValue;
        }

        while (currentColumn - 1 >= 0 && result[currentRow][currentColumn - 1] == 0) {
            currentColumn--;
            currentValue++;
            result[currentRow][currentColumn] = currentValue;
        }

        while (currentRow - 1 >= 0 && result[currentRow - 1][currentColumn] == 0) {
            currentRow--;
            currentValue++;
            result[currentRow][currentColumn] = currentValue;
        }
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?