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

SpookyB's solution

to Spiral Matrix in the Julia Track

Published at Apr 01 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

Source

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

Version compatibility

This exercise has been tested on Julia versions >=1.0.

Submitting Incomplete Solutions

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

runtests.jl

using Test

include("spiral-matrix.jl")


@testset "Different valid values" begin
    @testset "Empty spiral" begin
        @test spiral_matrix(0) == Matrix{Int}(undef,0,0)
    end
    @testset "Trivial spiral" begin
        @test spiral_matrix(1) == reshape([1],(1,1))
    end
    @testset "Spiral of size 2" begin
        @test spiral_matrix(2) == [1 2; 4 3]
    end
    @testset "Spiral of size 3" begin
        @test spiral_matrix(3) == [1 2 3; 8 9 4; 7 6 5]
    end
    @testset "Spiral of size 4" begin
        @test spiral_matrix(4) == [1 2 3 4; 12 13 14 5; 11 16 15 6; 10 9 8 7]
    end
    @testset "Spiral of size 5" begin
        @test spiral_matrix(5) == [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]
    end
end
function spiral_matrix(n)
    spiral = Array{Int}(undef, n, n)  #initialize a square matrix of length n
    dir = 1                           #direction of traversal, initialized positive
    dim = 2                           #dimension of traversal, initialized horizontal
    cur = [1, 1]                      #current index being modified, initialized at the top left corner
    edge = n                          #number of values to modify before turning while traversing
    step = 1                          #current steps taken since turning
    for i in 1:n^2
        spiral[cur[1], cur[2]] = i    #set the next value of the spiral at current position
        if step == edge
            if dim == 1
                dir = -dir            #if traversing vertically flip the sign of our direction
            else
                edge -= 1             #if traversing horizontally deincrement the ammount of times we step before turning
            end
            dim = dim == 1 ? 2 : 1    #change the dimension we are we are traversing
            step = 0
        end
        step += 1
        cur[dim] += dir               #take either a positive or negative step in the dimension being traversed
    end
    return spiral 
end

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?