Skip to content

Commit 6a8fa36

Browse files
authored
Add spiral matrix (#299)
* Add spiral matrix * Fix lint error
1 parent 5bbb74e commit 6a8fa36

File tree

8 files changed

+197
-0
lines changed

8 files changed

+197
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,14 @@
609609
"prerequisites": [],
610610
"difficulty": 4
611611
},
612+
{
613+
"slug": "spiral-matrix",
614+
"name": "Spiral Matrix",
615+
"uuid": "a9cf7720-d21e-4ab6-9ec3-78a8c44c348b",
616+
"practices": [],
617+
"prerequisites": [],
618+
"difficulty": 5
619+
},
612620
{
613621
"slug": "transpose",
614622
"name": "Transpose",
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+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"spiral_matrix.vim"
8+
],
9+
"test": [
10+
"spiral_matrix.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function! SpiralMatrix(n)
2+
if a:n == 0
3+
return []
4+
endif
5+
6+
let l:matrix = repeat([0], a:n * a:n)
7+
let l:currentX = 0
8+
let l:stopX = a:n - 1
9+
let l:currentY = 0
10+
let l:stopY = a:n - 1
11+
12+
let l:currentNumber = 1
13+
14+
while l:currentX < l:stopX && l:currentY < l:stopY
15+
for l:i in range(l:currentX, l:stopX)
16+
let l:matrix[l:currentY * a:n + l:i] = l:currentNumber
17+
let l:currentNumber += 1
18+
endfor
19+
let l:currentY += 1
20+
21+
for l:i in range(l:currentY, l:stopY)
22+
let l:matrix[l:i * a:n + l:stopX] = l:currentNumber
23+
let l:currentNumber += 1
24+
endfor
25+
let l:stopX -= 1
26+
27+
if l:currentY == l:stopY
28+
break
29+
endif
30+
31+
for l:i in range(l:stopX, l:currentX, -1)
32+
let l:matrix[l:stopY * a:n + l:i] = l:currentNumber
33+
let l:currentNumber += 1
34+
endfor
35+
let l:stopY -= 1
36+
37+
for l:i in range(l:stopY, l:currentY, -1)
38+
let l:matrix[l:i * a:n + l:currentX] = l:currentNumber
39+
let l:currentNumber += 1
40+
endfor
41+
let l:currentX += 1
42+
endwhile
43+
44+
let l:matrix[l:currentY * a:n + l:currentX] = l:currentNumber
45+
46+
let l:results = []
47+
for l:i in range(0, len(l:matrix) - 1, a:n)
48+
call add(l:results, l:matrix[l:i : l:i + a:n - 1])
49+
endfor
50+
51+
return l:results
52+
endfunction
53+
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
Execute (empty spiral):
3+
let g:size = 0
4+
let g:expected = []
5+
AssertEqual g:expected, SpiralMatrix(g:size)
6+
7+
Execute (trivial spiral):
8+
let g:size = 1
9+
let g:expected = [[1]]
10+
AssertEqual g:expected, SpiralMatrix(g:size)
11+
12+
Execute (spiral of size 2):
13+
let g:size = 2
14+
let g:expected = [[1, 2],
15+
\ [4, 3]]
16+
AssertEqual g:expected, SpiralMatrix(g:size)
17+
18+
Execute (spiral of size 3):
19+
let g:size = 3
20+
let g:expected = [[1, 2, 3],
21+
\ [8, 9, 4],
22+
\ [7, 6, 5]]
23+
AssertEqual g:expected, SpiralMatrix(g:size)
24+
25+
Execute (spiral of size 4):
26+
let g:size = 4
27+
let g:expected = [[1, 2, 3, 4],
28+
\ [12, 13, 14, 5],
29+
\ [11, 16, 15, 6],
30+
\ [10, 9, 8, 7]]
31+
AssertEqual g:expected, SpiralMatrix(g:size)
32+
33+
Execute (spiral of size 5):
34+
let g:size = 5
35+
let g:expected = [[ 1, 2, 3, 4, 5],
36+
\ [16, 17, 18, 19, 6],
37+
\ [15, 24, 25, 20, 7],
38+
\ [14, 23, 22, 21, 8],
39+
\ [13, 12, 11, 10, 9]]
40+
AssertEqual g:expected, SpiralMatrix(g:size)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"
2+
" Generate a square matrix of natural numbers,
3+
" starting from 1 in the top-left corner and incrementing
4+
" in a clockwise spiral order.
5+
"
6+
" Example:
7+
"
8+
" :echo SpiralMatrix(3)
9+
" [[1, 2, 3], [8, 9, 4], [7, 6, 5]]
10+
"
11+
function! SpiralMatrix(n)
12+
" your code goes here
13+
endfunction
14+

0 commit comments

Comments
 (0)