Skip to content

Commit

Permalink
Add spiral-matrix exercise (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfertyk authored Feb 10, 2024
1 parent 4280977 commit 0431038
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "spiral-matrix",
"name": "Spiral Matrix",
"uuid": "7fbea4aa-0454-43a0-a189-4169b0f762e5",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/spiral-matrix/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
```
19 changes: 19 additions & 0 deletions exercises/practice/spiral-matrix/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"pfertyk"
],
"files": {
"solution": [
"spiral_matrix.gd"
],
"test": [
"spiral_matrix_test.gd"
],
"example": [
".meta/example.gd"
]
},
"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/"
}
23 changes: 23 additions & 0 deletions exercises/practice/spiral-matrix/.meta/example.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
func spiral_matrix(size):
var matrix = []
for i in range(size):
var row = []
for j in range(size):
row.append(0)
matrix.append(row)

var idx = 0
var jdx = -1
var element = 1

var digital = [0, 1, 0, -1]
var disco = [1, 0, -1, 0]

for edx in range(2 * size - 1):
for i in range((2 * size - edx) / 2):
idx += digital[edx % 4]
jdx += disco[edx % 4]
matrix[idx][jdx] = element
element += 1

return matrix
28 changes: 28 additions & 0 deletions exercises/practice/spiral-matrix/.meta/tests.toml
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"
2 changes: 2 additions & 0 deletions exercises/practice/spiral-matrix/spiral_matrix.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func spiral_matrix(size):
pass
52 changes: 52 additions & 0 deletions exercises/practice/spiral-matrix/spiral_matrix_test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
func test_empty_spiral(solution_script):
return [solution_script.spiral_matrix(0), []]


func test_trivial_spiral(solution_script):
return [solution_script.spiral_matrix(1), [[1]]]


func test_spiral_of_size_2(solution_script):
return [
solution_script.spiral_matrix(2),
[
[1, 2],
[4, 3]
]
]


func test_spiral_of_size_3(solution_script):
return [
solution_script.spiral_matrix(3),
[
[1, 2, 3],
[8, 9, 4],
[7, 6, 5]
]
]


func test_spiral_of_size_4(solution_script):
return [
solution_script.spiral_matrix(4),
[
[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]
]
]


func test_spiral_of_size_5(solution_script):
return [
solution_script.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],
]
]

0 comments on commit 0431038

Please sign in to comment.