🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of n0mn0m

n0mn0m's solution

to Spiral Matrix in the C# Track

Published at Feb 26 2021 · 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/

SpiralMatrixTests.cs

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

using Xunit;

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

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

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

    [Fact(Skip = "Remove this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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
{
    public static int[,] GetMatrix(int size)
    {
        var result = new int[size, size];
        FillMatrix(result, 0, size, 1);
        return result;
    }

    private static void FillMatrix(int[,] result, int offset, int size, int index)
    {
        while (true)
        {
            if (size <= 0)
            {
                return;
            }

            //Left to right
            for (var y = 0; y < size - 2; y++)
            {
                result[offset + y + 1, offset + size - 1] = index + size + y;
                result[offset + size - y - 2, offset] = index + size * 3 - 2 + y;
            }

            //Top to bottom
            for (var x = 0; x < size; x++)
            {
                result[offset + 0, offset + x] = index + x;
                result[offset + size - 1, offset + size - x - 1] = index + (size * 2 + x - 2);
            }

            var rsize = size;
            offset += 1;
            size -= 2;
            index = index + rsize * 3 + rsize - 4;
        }
    }
}

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?