Avatar of artemkorsakov

artemkorsakov's solution

to Spiral Matrix in the C# Track

Published at May 17 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

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit SpiralMatrix.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

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

SpiralMatrixTest.cs

// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;

public class SpiralMatrixTest
{
    [Fact]
    public void Empty_spiral()
    {
        Assert.Empty(SpiralMatrix.GetMatrix(0));
    }

    [Fact(Skip = "Remove to run test")]
    public void Trivial_spiral()
    {
        var expected = new[,]
        {
             { 1 }
        };
        Assert.Equal(expected, SpiralMatrix.GetMatrix(1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Spiral_of_size_2()
    {
        var expected = new[,]
        {
             { 1, 2 },
             { 4, 3 }
        };
        Assert.Equal(expected, SpiralMatrix.GetMatrix(2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Spiral_of_size_3()
    {
        var expected = new[,]
        {
             { 1, 2, 3 },
             { 8, 9, 4 },
             { 7, 6, 5 }
        };
        Assert.Equal(expected, SpiralMatrix.GetMatrix(3));
    }

    [Fact(Skip = "Remove to run test")]
    public void Spiral_of_size_4()
    {
        var expected = new[,]
        {
             { 1, 2, 3, 4 },
             { 12, 13, 14, 5 },
             { 11, 16, 15, 6 },
             { 10, 9, 8, 7 }
        };
        Assert.Equal(expected, SpiralMatrix.GetMatrix(4));
    }

    [Fact(Skip = "Remove to run test")]
    public void Spiral_of_size_5()
    {
        var expected = new[,]
        {
             { 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 }
        };
        Assert.Equal(expected, SpiralMatrix.GetMatrix(5));
    }
}
public class SpiralMatrix
{
    private static int currentRow;
    private static int currentColumn;
    private static int currentValue;

    public static int[,] GetMatrix(int size)
    {
        if (size < 1)
        {
            return new int[,] { };
        }

        var result = new int[size, size];
        currentRow = currentColumn = 0;
        result[0, 0] = currentValue = 1;
        for (int i = 0; i < size / 2 + 1; i++)
        {
            SetSpiralLine(result);
        }

        return result;
    }

    private static void SetSpiralLine(int[,] result)
    {
        while (currentColumn + 1 < result.GetLength(1)
               && result[currentRow, currentColumn + 1] == 0)
        {
            currentColumn++;
            currentValue++;
            result[currentRow, currentColumn] = currentValue;
        }

        while (currentRow + 1 < result.GetLength(1)
               && 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?