From 5cfaa7e3b36895999db262b0c0cbeb1bf73fa84e Mon Sep 17 00:00:00 2001 From: glaxxie <86179463+glaxxie@users.noreply.github.com> Date: Fri, 22 Sep 2023 21:30:20 -0500 Subject: [PATCH] [New Exercise]: Spiral Matrix --- config.json | 8 +++ .../spiral-matrix/.docs/instructions.md | 24 +++++++ .../.meta/SpiralMatrix.example.ps1 | 64 +++++++++++++++++ .../practice/spiral-matrix/.meta/config.json | 19 ++++++ .../practice/spiral-matrix/.meta/tests.toml | 28 ++++++++ .../practice/spiral-matrix/SpiralMatrix.ps1 | 25 +++++++ .../spiral-matrix/SpiralMatrix.tests.ps1 | 68 +++++++++++++++++++ 7 files changed, 236 insertions(+) create mode 100644 exercises/practice/spiral-matrix/.docs/instructions.md create mode 100644 exercises/practice/spiral-matrix/.meta/SpiralMatrix.example.ps1 create mode 100644 exercises/practice/spiral-matrix/.meta/config.json create mode 100644 exercises/practice/spiral-matrix/.meta/tests.toml create mode 100644 exercises/practice/spiral-matrix/SpiralMatrix.ps1 create mode 100644 exercises/practice/spiral-matrix/SpiralMatrix.tests.ps1 diff --git a/config.json b/config.json index 9f6e7cfb..6e7192d7 100644 --- a/config.json +++ b/config.json @@ -713,6 +713,14 @@ "prerequisites": [], "difficulty": 4 }, + { + "slug": "spiral-matrix", + "name": "Spiral Matrix", + "uuid": "37534530-58c2-4993-ae27-9e743058dc8f", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "complex-numbers", "name": "Complex Numbers", diff --git a/exercises/practice/spiral-matrix/.docs/instructions.md b/exercises/practice/spiral-matrix/.docs/instructions.md new file mode 100644 index 00000000..ba99e12c --- /dev/null +++ b/exercises/practice/spiral-matrix/.docs/instructions.md @@ -0,0 +1,24 @@ +# Instructions + +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: + +## 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 +``` diff --git a/exercises/practice/spiral-matrix/.meta/SpiralMatrix.example.ps1 b/exercises/practice/spiral-matrix/.meta/SpiralMatrix.example.ps1 new file mode 100644 index 00000000..687e2663 --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/SpiralMatrix.example.ps1 @@ -0,0 +1,64 @@ +Function Get-SpiralMatrix() { + <# + .SYNOPSIS + Implement a function to generate a spiral matrix. + + .DESCRIPTION + 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 + + .PARAMETER Size + Size of the matrix. + + .EXAMPLE + Get-SpiralMatrix -Size 2 + Return: @( + @(1,2), + @(4,3) + ) + #> + [CmdletBinding()] + Param( + [int]$Size + ) + + enum Dir { + RIGHT + DOWN + LEFT + UP + } + + $matrix = @() + for ($i = 0; $i -lt $Size; $i++) { + $matrix += ,(@($null) * $Size) + } + + $top = $left = 0 + $bottom = $right = $Size - 1 + $dir = [Dir]::RIGHT + $value = 1 + + while (($bottom -ge $top) -and ($right -ge $left)) { + switch ($dir) { + RIGHT { + $left..$right | ForEach-Object { $matrix[$top][$_] = $value; $value++ } + $top++ + } + DOWN { + $top..$bottom | ForEach-Object { $matrix[$_][$right] = $value; $value++ } + $right-- + } + LEFT { + $right..$left | ForEach-Object { $matrix[$bottom][$_] = $value; $value++ } + $bottom-- + } + default { + $bottom..$top | ForEach-Object { $matrix[$_][$left] = $value; $value++ } + $left++ + } + } + $dir = ($dir + 1) % 4 + } + $matrix +} \ No newline at end of file diff --git a/exercises/practice/spiral-matrix/.meta/config.json b/exercises/practice/spiral-matrix/.meta/config.json new file mode 100644 index 00000000..cacfb221 --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glaxxie" + ], + "files": { + "solution": [ + "SpiralMatrix.ps1" + ], + "test": [ + "SpiralMatrix.tests.ps1" + ], + "example": [ + ".meta/SpiralMatrix.example.ps1" + ] + }, + "blurb": "Given the size, return a square matrix of numbers in spiral order.", + "source": "Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension.", + "source_url": "https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/" +} diff --git a/exercises/practice/spiral-matrix/.meta/tests.toml b/exercises/practice/spiral-matrix/.meta/tests.toml new file mode 100644 index 00000000..9ac5baca --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/tests.toml @@ -0,0 +1,28 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[8f584201-b446-4bc9-b132-811c8edd9040] +description = "empty spiral" + +[e40ae5f3-e2c9-4639-8116-8a119d632ab2] +description = "trivial spiral" + +[cf05e42d-eb78-4098-a36e-cdaf0991bc48] +description = "spiral of size 2" + +[1c475667-c896-4c23-82e2-e033929de939] +description = "spiral of size 3" + +[05ccbc48-d891-44f5-9137-f4ce462a759d] +description = "spiral of size 4" + +[f4d2165b-1738-4e0c-bed0-c459045ae50d] +description = "spiral of size 5" diff --git a/exercises/practice/spiral-matrix/SpiralMatrix.ps1 b/exercises/practice/spiral-matrix/SpiralMatrix.ps1 new file mode 100644 index 00000000..59716e14 --- /dev/null +++ b/exercises/practice/spiral-matrix/SpiralMatrix.ps1 @@ -0,0 +1,25 @@ +Function Get-SpiralMatrix() { + <# + .SYNOPSIS + Implement a function to generate a spiral matrix. + + .DESCRIPTION + 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 + + .PARAMETER Size + Size of the matrix. + + .EXAMPLE + Get-SpiralMatrix -Size 2 + Return: @( + @(1,2), + @(4,3) + ) + #> + [CmdletBinding()] + Param( + [int]$Size + ) + Throw "Please implement this function" +} \ No newline at end of file diff --git a/exercises/practice/spiral-matrix/SpiralMatrix.tests.ps1 b/exercises/practice/spiral-matrix/SpiralMatrix.tests.ps1 new file mode 100644 index 00000000..4a1264b2 --- /dev/null +++ b/exercises/practice/spiral-matrix/SpiralMatrix.tests.ps1 @@ -0,0 +1,68 @@ +BeforeAll { + . "./SpiralMatrix.ps1" +} + +Describe "SpiralMatrix test cases" { + It "empty spiral" { + $got = Get-SpiralMatrix -Size 0 + $want = @() + + $got | Should -BeExactly $want + } + + It "trivial spiral" { + $got = Get-SpiralMatrix -Size 1 + $want = @( + ,@(1) + ) + + $got | Should -BeExactly $want + } + + It "spiral of size 2" { + $got = Get-SpiralMatrix -Size 2 + $want = @( + @(1,2), + @(4,3) + ) + + $got | Should -BeExactly $want + } + + It "spiral of size 3" { + $got = Get-SpiralMatrix -Size 3 + $want = @( + @(1,2,3), + @(8,9,4), + @(7,6,5) + ) + + $got | Should -BeExactly $want + } + + It "spiral of size 4" { + $got = Get-SpiralMatrix -Size 4 + $want = @( + @( 1, 2, 3,4), + @(12,13,14,5), + @(11,16,15,6), + @(10, 9, 8,7) + ) + + $got | Should -BeExactly $want + } + + It "spiral of size 5" { + $got = Get-SpiralMatrix -Size 5 + $want = @( + @( 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) + ) + + $got | Should -BeExactly $want + } + +}