Skip to content

Commit 89c9657

Browse files
Implementation: Spiral Matrix (#803)
* Implementation: Spiral Matrix -- 🤝 https://forum.exercism.org/t/new-exercise-contributions/4077 * Refactor * Relax testing to accept both nested arrays and nested lists
1 parent 08211fb commit 89c9657

10 files changed

Lines changed: 183 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,14 @@
855855
"practices": [],
856856
"prerequisites": [],
857857
"difficulty": 1
858+
},
859+
{
860+
"slug": "spiral-matrix",
861+
"name": "Spiral Matrix",
862+
"uuid": "7319a333-3120-4de2-9e5d-f2e0a7362901",
863+
"practices": [],
864+
"prerequisites": [],
865+
"difficulty": 1
858866
}
859867
]
860868
},
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Instructions
2+
3+
Your task is to return a square matrix of a given size.
4+
5+
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:
6+
7+
## Examples
8+
9+
### Spiral matrix of size 3
10+
11+
```text
12+
1 2 3
13+
8 9 4
14+
7 6 5
15+
```
16+
17+
### Spiral matrix of size 4
18+
19+
```text
20+
1 2 3 4
21+
12 13 14 5
22+
11 16 15 6
23+
10 9 8 7
24+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Introduction
2+
3+
In a small village near an ancient forest, there was a legend of a hidden treasure buried deep within the woods.
4+
Despite numerous attempts, no one had ever succeeded in finding it.
5+
This was about to change, however, thanks to a young explorer named Elara.
6+
She had discovered an old document containing instructions on how to locate the treasure.
7+
Using these instructions, Elara was able to draw a map that revealed the path to the treasure.
8+
9+
To her surprise, the path followed a peculiar clockwise spiral.
10+
It was no wonder no one had been able to find the treasure before!
11+
With the map in hand, Elara embarks on her journey to uncover the hidden treasure.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"habere-et-dispertire"
4+
],
5+
"files": {
6+
"solution": [
7+
"lib/SpiralMatrix.rakumod"
8+
],
9+
"test": [
10+
"t/spiral-matrix.rakutest"
11+
],
12+
"example": [
13+
".meta/solutions/lib/SpiralMatrix.rakumod"
14+
]
15+
},
16+
"blurb": "Given the size, return a square matrix of numbers in spiral order.",
17+
"source": "Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension.",
18+
"source_url": "https://web.archive.org/web/20230607064729/https://old.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/"
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
unit module SpiralMatrix;
2+
3+
sub fill-turn ($row, $step is rw, @m) {
4+
@m[$row;$_] = $step++ unless @m[$row;$_] for ^@m;
5+
@m = reverse map *.Array, [Z] @m;
6+
}
7+
sub spiral-matrix ($n) is export {
8+
return [] unless $n;
9+
my ($row, $step, @m) = 0, 1, |[[Any xx $n] xx $n];
10+
fill-turn $row++ div 4, $step, @m until $step > $n² and @m[0;0] == 1;
11+
return @m;
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../t/spiral-matrix.rakutest
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
unit: module
2+
3+
properties:
4+
spiralMatrix:
5+
test: |-
6+
sprintf(q:to/END/, %case<input><size>.Int.raku, %case<expected>.Array.raku, %case<description>.raku);
7+
cmp-ok(
8+
spiral-matrix(%s),
9+
"eqv",
10+
%s,
11+
%s,
12+
);
13+
END
14+
15+
example: |-
16+
sub fill-turn ($row, $step is rw, @m) {
17+
@m[$row;$_] = $step++ unless @m[$row;$_] for ^@m;
18+
@m = reverse map *.Array, [Z] @m;
19+
}
20+
sub spiral-matrix ($n) is export {
21+
return [] unless $n;
22+
my ($row, $step, @m) = 0, 1, |[[Any xx $n] xx $n];
23+
fill-turn $row++ div 4, $step, @m until $step > $n² and @m[0;0] == 1;
24+
return @m;
25+
}
26+
27+
stub: |-
28+
sub spiral-matrix ($side-length) is export { }
29+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[8f584201-b446-4bc9-b132-811c8edd9040]
13+
description = "empty spiral"
14+
15+
[e40ae5f3-e2c9-4639-8116-8a119d632ab2]
16+
description = "trivial spiral"
17+
18+
[cf05e42d-eb78-4098-a36e-cdaf0991bc48]
19+
description = "spiral of size 2"
20+
21+
[1c475667-c896-4c23-82e2-e033929de939]
22+
description = "spiral of size 3"
23+
24+
[05ccbc48-d891-44f5-9137-f4ce462a759d]
25+
description = "spiral of size 4"
26+
27+
[f4d2165b-1738-4e0c-bed0-c459045ae50d]
28+
description = "spiral of size 5"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
unit module SpiralMatrix;
2+
3+
sub spiral-matrix ($side-length) is export { }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env raku
2+
use Test;
3+
use lib $?FILE.IO.parent(2).add('lib');
4+
use SpiralMatrix;
5+
6+
cmp-ok( # begin: 8f584201-b446-4bc9-b132-811c8edd9040
7+
spiral-matrix(0),
8+
"eqv",
9+
[],
10+
"empty spiral",
11+
); # end: 8f584201-b446-4bc9-b132-811c8edd9040
12+
13+
cmp-ok( # begin: e40ae5f3-e2c9-4639-8116-8a119d632ab2
14+
spiral-matrix(1),
15+
"eqv",
16+
[[1],],
17+
"trivial spiral",
18+
); # end: e40ae5f3-e2c9-4639-8116-8a119d632ab2
19+
20+
cmp-ok( # begin: cf05e42d-eb78-4098-a36e-cdaf0991bc48
21+
spiral-matrix(2),
22+
"eqv",
23+
[[1, 2], [4, 3]],
24+
"spiral of size 2",
25+
); # end: cf05e42d-eb78-4098-a36e-cdaf0991bc48
26+
27+
cmp-ok( # begin: 1c475667-c896-4c23-82e2-e033929de939
28+
spiral-matrix(3),
29+
"eqv",
30+
[[1, 2, 3], [8, 9, 4], [7, 6, 5]],
31+
"spiral of size 3",
32+
); # end: 1c475667-c896-4c23-82e2-e033929de939
33+
34+
cmp-ok( # begin: 05ccbc48-d891-44f5-9137-f4ce462a759d
35+
spiral-matrix(4),
36+
"eqv",
37+
[[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]],
38+
"spiral of size 4",
39+
); # end: 05ccbc48-d891-44f5-9137-f4ce462a759d
40+
41+
cmp-ok( # begin: f4d2165b-1738-4e0c-bed0-c459045ae50d
42+
spiral-matrix(5),
43+
"eqv",
44+
[[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]],
45+
"spiral of size 5",
46+
); # end: f4d2165b-1738-4e0c-bed0-c459045ae50d
47+
48+
done-testing;

0 commit comments

Comments
 (0)