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

leadersheir's solution

to Spiral Matrix in the Julia Track

Published at Feb 13 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(n::Int)
    n == 0 && return Matrix{Int}(undef,0,0)
    
    lin_map = [] 
    ens = sort(cat([(4*k - 3, n - (k-1)) for k ∈ 1:n], [(4*k - 2, n - (k-1)) for k ∈ 1:n], dims=(1, 1)))
    nums = sort(cat([(4*k - 1, k) for k ∈ 1:n], [(4*k, k+1) for k ∈ 1:n], dims=(1, 1)))
    
    i = j = 1
    
    for e ∈ sort(cat(ens, nums, dims=(1, 1)))[1:2*n - 1]
        if e ∈ ens
            if e[1] % 2 == 1
                while j != e[2]
                    push!(lin_map, (i, j))
                    j += 1
                end
            elseif e[1] % 2 == 0
                while i != e[2]
                    push!(lin_map, (i, j))
                    i += 1
                end
            end

        elseif e ∈ nums
            if e[1] % 2 == 1
                while j != e[2]
                    push!(lin_map, (i, j))
                    j -= 1
                end
            elseif e[1] % 2 == 0
                while i != e[2]
                    push!(lin_map, (i, j))
                    i -= 1
                end
            end        
        end
    end
    
    push!(lin_map, (i, j))
    
    spiral_mat = zeros(Int64, n, n)
    
    for k ∈ 1:length(lin_map)
        spiral_mat[lin_map[k][1], lin_map[k][2]] = k
    end
    
    return spiral_mat 
end

Community comments

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

leadersheir's Reflection

Had lots of fun figuring this one out completely on my own for 3 days