Skip to content

Commit 0431038

Browse files
authored
Add spiral-matrix exercise (#49)
1 parent 4280977 commit 0431038

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@
170170
"practices": [],
171171
"prerequisites": [],
172172
"difficulty": 3
173+
},
174+
{
175+
"slug": "spiral-matrix",
176+
"name": "Spiral Matrix",
177+
"uuid": "7fbea4aa-0454-43a0-a189-4169b0f762e5",
178+
"practices": [],
179+
"prerequisites": [],
180+
"difficulty": 4
173181
}
174182
]
175183
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Instructions
2+
3+
Given the size, return a square matrix of numbers in spiral order.
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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"pfertyk"
4+
],
5+
"files": {
6+
"solution": [
7+
"spiral_matrix.gd"
8+
],
9+
"test": [
10+
"spiral_matrix_test.gd"
11+
],
12+
"example": [
13+
".meta/example.gd"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func spiral_matrix(size):
2+
var matrix = []
3+
for i in range(size):
4+
var row = []
5+
for j in range(size):
6+
row.append(0)
7+
matrix.append(row)
8+
9+
var idx = 0
10+
var jdx = -1
11+
var element = 1
12+
13+
var digital = [0, 1, 0, -1]
14+
var disco = [1, 0, -1, 0]
15+
16+
for edx in range(2 * size - 1):
17+
for i in range((2 * size - edx) / 2):
18+
idx += digital[edx % 4]
19+
jdx += disco[edx % 4]
20+
matrix[idx][jdx] = element
21+
element += 1
22+
23+
return matrix
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
func spiral_matrix(size):
2+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
func test_empty_spiral(solution_script):
2+
return [solution_script.spiral_matrix(0), []]
3+
4+
5+
func test_trivial_spiral(solution_script):
6+
return [solution_script.spiral_matrix(1), [[1]]]
7+
8+
9+
func test_spiral_of_size_2(solution_script):
10+
return [
11+
solution_script.spiral_matrix(2),
12+
[
13+
[1, 2],
14+
[4, 3]
15+
]
16+
]
17+
18+
19+
func test_spiral_of_size_3(solution_script):
20+
return [
21+
solution_script.spiral_matrix(3),
22+
[
23+
[1, 2, 3],
24+
[8, 9, 4],
25+
[7, 6, 5]
26+
]
27+
]
28+
29+
30+
func test_spiral_of_size_4(solution_script):
31+
return [
32+
solution_script.spiral_matrix(4),
33+
[
34+
[1, 2, 3, 4],
35+
[12, 13, 14, 5],
36+
[11, 16, 15, 6],
37+
[10, 9, 8, 7]
38+
]
39+
]
40+
41+
42+
func test_spiral_of_size_5(solution_script):
43+
return [
44+
solution_script.spiral_matrix(5),
45+
[
46+
[1, 2, 3, 4, 5],
47+
[16, 17, 18, 19, 6],
48+
[15, 24, 25, 20, 7],
49+
[14, 23, 22, 21, 8],
50+
[13, 12, 11, 10, 9],
51+
]
52+
]

0 commit comments

Comments
 (0)