diff --git a/config.json b/config.json index d512cadc97a93..5168587f59e37 100644 --- a/config.json +++ b/config.json @@ -407,6 +407,18 @@ "exceptions" ] }, + { + "uuid": "3a2facfd-5ec8-49a0-a5aa-2229f07da896", + "slug": "spiral-matrix", + "core": false, + "unlocked_by": null, + "difficulty": 3, + "topics": [ + "matrix", + "mathematics", + "iterators" + ] + }, { "slug": "isbn-verifier", "uuid": "22b64a2b-7f08-444d-be6e-8b42e5c9207c", diff --git a/exercises/spiral-matrix/README.md b/exercises/spiral-matrix/README.md new file mode 100644 index 0000000000000..1ed00ae3568b0 --- /dev/null +++ b/exercises/spiral-matrix/README.md @@ -0,0 +1,31 @@ +# Spiral Matrix + +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 + +```text +1 2 3 +8 9 4 +7 6 5 +``` + +###### Spiral matrix of size 4 + +```text + 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/](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. diff --git a/exercises/spiral-matrix/example.jl b/exercises/spiral-matrix/example.jl new file mode 100644 index 0000000000000..040a5b058f54f --- /dev/null +++ b/exercises/spiral-matrix/example.jl @@ -0,0 +1,9 @@ +#Credit to u/SingularInfinity on Redit: https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/djb8po0 + +function spiral_matrix(n::Int) + n < 0 && throw(DomainError("n should be positive")) + n == 0 && return Matrix{Int}(0,0) + n == 1 && return reshape(Int[1], 1, 1) + m = (2n - 1) .+ spiral_matrix(n-1) + return [ RowVector(1:n); rot180(m) n+1:2n-1 ] +end diff --git a/exercises/spiral-matrix/runtests.jl b/exercises/spiral-matrix/runtests.jl new file mode 100644 index 0000000000000..a84606f8c6008 --- /dev/null +++ b/exercises/spiral-matrix/runtests.jl @@ -0,0 +1,25 @@ +using Base.Test + +include("spiral-matrix.jl") + + +@testset "Different valid values" begin + @testset "Empty spiral" begin + @test spiral_matrix(0) == Matrix{Int}(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 diff --git a/exercises/spiral-matrix/spiral-matrix.jl b/exercises/spiral-matrix/spiral-matrix.jl new file mode 100644 index 0000000000000..23dce9c3ca540 --- /dev/null +++ b/exercises/spiral-matrix/spiral-matrix.jl @@ -0,0 +1,3 @@ +function spiral_matrix(n::Int) + +end