-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Instructions | ||
|
||
Your task is to return a square matrix of a given size. | ||
|
||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Introduction | ||
|
||
In a small village near an ancient forest, there was a legend of a hidden treasure buried deep within the woods. | ||
Despite numerous attempts, no one had ever succeeded in finding it. | ||
This was about to change, however, thanks to a young explorer named Elara. | ||
She had discovered an old document containing instructions on how to locate the treasure. | ||
Using these instructions, Elara was able to draw a map that revealed the path to the treasure. | ||
|
||
To her surprise, the path followed a peculiar clockwise spiral. | ||
It was no wonder no one had been able to find the treasure before! | ||
With the map in hand, Elara embarks on her journey to uncover the hidden treasure. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"glennj" | ||
], | ||
"files": { | ||
"solution": [ | ||
"lib/SpiralMatrix.pm" | ||
], | ||
"test": [ | ||
"t/spiral-matrix.t" | ||
], | ||
"example": [ | ||
".meta/solutions/lib/SpiralMatrix.pm" | ||
] | ||
}, | ||
"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://web.archive.org/web/20230607064729/https://old.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/" | ||
} |
30 changes: 30 additions & 0 deletions
30
exercises/practice/spiral-matrix/.meta/solutions/lib/SpiralMatrix.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package SpiralMatrix; | ||
|
||
use strict; | ||
use warnings; | ||
use experimental qw<signatures postderef postderef_qq>; | ||
|
||
use Exporter qw<import>; | ||
our @EXPORT_OK = qw<spiral_matrix>; | ||
|
||
sub spiral_matrix ($size) { | ||
my @mtx; | ||
push @mtx, [ (undef) x $size ] for 1 .. $size; | ||
|
||
my ( $x, $y, $dx, $dy ) = ( 0, 0, 0, 1 ); | ||
|
||
for my $i ( 1 .. $size * $size ) { | ||
$mtx[$x][$y] = $i; | ||
if ( ( $x + $dx == $size || $x + $dx < 0 ) | ||
|| ( $y + $dy == $size || $y + $dy < 0 ) | ||
|| $mtx[ $x + $dx ][ $y + $dy ] | ||
) { | ||
( $dx, $dy ) = ( $dy, -$dx ); | ||
} | ||
$x += $dx; | ||
$y += $dy; | ||
} | ||
return \@mtx; | ||
} | ||
|
||
1; |
1 change: 1 addition & 0 deletions
1
exercises/practice/spiral-matrix/.meta/solutions/t/spiral-matrix.t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../t/spiral-matrix.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
subs: spiral_matrix | ||
|
||
properties: | ||
spiralMatrix: | ||
test: |- | ||
use Data::Dmp; | ||
sprintf(<<~'END', $case->{input}{size}, map {dmp $_} $case->@{qw<expected description>}); | ||
is( | ||
spiral_matrix(%s), | ||
%s, | ||
%s, | ||
); | ||
END | ||
stub: |- | ||
sub spiral_matrix ($size) { | ||
return undef; | ||
} | ||
example: |- | ||
sub spiral_matrix ($size) { | ||
my @mtx; | ||
push @mtx, [ (undef) x $size ] for 1..$size; | ||
my ($x, $y, $dx, $dy) = (0, 0, 0, 1); | ||
for my $i (1 .. $size * $size) { | ||
$mtx[$x][$y] = $i; | ||
if ( ($x + $dx == $size || $x + $dx < 0) | ||
|| ($y + $dy == $size || $y + $dy < 0) | ||
|| $mtx[$x + $dx][$y + $dy] | ||
) { | ||
($dx, $dy) = ($dy, -$dx); | ||
} | ||
$x += $dx; | ||
$y += $dy; | ||
} | ||
return \@mtx; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package SpiralMatrix; | ||
|
||
use v5.40; | ||
|
||
use Exporter qw<import>; | ||
our @EXPORT_OK = qw<spiral_matrix>; | ||
|
||
sub spiral_matrix ($size) { | ||
return undef; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env perl | ||
use Test2::V0; | ||
|
||
use FindBin qw<$Bin>; | ||
use lib "$Bin/../lib", "$Bin/../local/lib/perl5"; | ||
|
||
use SpiralMatrix qw<spiral_matrix>; | ||
|
||
is( # begin: 8f584201-b446-4bc9-b132-811c8edd9040 | ||
spiral_matrix(0), | ||
[], | ||
"empty spiral", | ||
); # end: 8f584201-b446-4bc9-b132-811c8edd9040 | ||
|
||
is( # begin: e40ae5f3-e2c9-4639-8116-8a119d632ab2 | ||
spiral_matrix(1), | ||
[ [1] ], | ||
"trivial spiral", | ||
); # end: e40ae5f3-e2c9-4639-8116-8a119d632ab2 | ||
|
||
is( # begin: cf05e42d-eb78-4098-a36e-cdaf0991bc48 | ||
spiral_matrix(2), | ||
[ [ 1, 2 ], [ 4, 3 ] ], | ||
"spiral of size 2", | ||
); # end: cf05e42d-eb78-4098-a36e-cdaf0991bc48 | ||
|
||
is( # begin: 1c475667-c896-4c23-82e2-e033929de939 | ||
spiral_matrix(3), | ||
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ], | ||
"spiral of size 3", | ||
); # end: 1c475667-c896-4c23-82e2-e033929de939 | ||
|
||
is( # begin: 05ccbc48-d891-44f5-9137-f4ce462a759d | ||
spiral_matrix(4), | ||
[ [ 1, 2, 3, 4 ], [ 12, 13, 14, 5 ], [ 11, 16, 15, 6 ], [ 10, 9, 8, 7 ] ], | ||
"spiral of size 4", | ||
); # end: 05ccbc48-d891-44f5-9137-f4ce462a759d | ||
|
||
is( # begin: f4d2165b-1738-4e0c-bed0-c459045ae50d | ||
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 ] ], | ||
"spiral of size 5", | ||
); # end: f4d2165b-1738-4e0c-bed0-c459045ae50d | ||
|
||
done_testing; |