diff --git a/config.json b/config.json index 4ed1298..379e8e3 100644 --- a/config.json +++ b/config.json @@ -454,6 +454,14 @@ "practices": [], "prerequisites": [], "difficulty": 5 + }, + { + "slug": "spiral-matrix", + "name": "Spiral Matrix", + "uuid": "bba5483b-a021-4a18-a1db-ed6529cba0fb", + "practices": [], + "prerequisites": [], + "difficulty": 5 } ] }, diff --git a/exercises/practice/spiral-matrix/.busted b/exercises/practice/spiral-matrix/.busted new file mode 100644 index 0000000..86b84e7 --- /dev/null +++ b/exercises/practice/spiral-matrix/.busted @@ -0,0 +1,5 @@ +return { + default = { + ROOT = { '.' } + } +} diff --git a/exercises/practice/spiral-matrix/.docs/instructions.md b/exercises/practice/spiral-matrix/.docs/instructions.md new file mode 100644 index 0000000..01e8a77 --- /dev/null +++ b/exercises/practice/spiral-matrix/.docs/instructions.md @@ -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 +``` diff --git a/exercises/practice/spiral-matrix/.docs/introduction.md b/exercises/practice/spiral-matrix/.docs/introduction.md new file mode 100644 index 0000000..25c7eb5 --- /dev/null +++ b/exercises/practice/spiral-matrix/.docs/introduction.md @@ -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. diff --git a/exercises/practice/spiral-matrix/.meta/config.json b/exercises/practice/spiral-matrix/.meta/config.json new file mode 100644 index 0000000..9e152fb --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "spiral_matrix.moon" + ], + "test": [ + "spiral_matrix_spec.moon" + ], + "example": [ + ".meta/example.moon" + ] + }, + "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/" +} diff --git a/exercises/practice/spiral-matrix/.meta/example.moon b/exercises/practice/spiral-matrix/.meta/example.moon new file mode 100644 index 0000000..81b4a29 --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/example.moon @@ -0,0 +1,17 @@ +{ + spiral_matrix: (size) -> + mtx = [ [nil for _ = 1, size] for _ = 1, size] + x, y = 1, 1 + dx, dy = 0, 1 + + outside = (n) -> n < 1 or n > size + turn = -> dx, dy = dy, -dx + + for i = 1, size*size + mtx[x][y] = i + turn! if outside(x + dx) or outside(y + dy) or mtx[x + dx][y + dy] + x += dx + y += dy + + mtx +} diff --git a/exercises/practice/spiral-matrix/.meta/spec_generator.moon b/exercises/practice/spiral-matrix/.meta/spec_generator.moon new file mode 100644 index 0000000..4f94165 --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/spec_generator.moon @@ -0,0 +1,23 @@ +int_list = (list) -> "{#{table.concat list, ', '}}" + +int_lists = (list, level) -> + switch #list + when 0 then '{}' + when 1 then '{{1}}' + else + rows = [indent int_list(row), level + 1 for row in *list] + table.insert rows, 1, '{' + table.insert rows, indent('}', level) + table.concat rows, '\n' + +{ + module_imports: {'spiral_matrix'}, + + generate_test: (case, level) -> + lines = { + "result = spiral_matrix #{case.input.size}", + "expected = #{int_lists case.expected, level}", + "assert.are.same expected, result" + } + table.concat [indent line, level for line in *lines], '\n' +} diff --git a/exercises/practice/spiral-matrix/.meta/tests.toml b/exercises/practice/spiral-matrix/.meta/tests.toml new file mode 100644 index 0000000..9ac5bac --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/tests.toml @@ -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" diff --git a/exercises/practice/spiral-matrix/spiral_matrix.moon b/exercises/practice/spiral-matrix/spiral_matrix.moon new file mode 100644 index 0000000..e69de29 diff --git a/exercises/practice/spiral-matrix/spiral_matrix_spec.moon b/exercises/practice/spiral-matrix/spiral_matrix_spec.moon new file mode 100644 index 0000000..8e5a96c --- /dev/null +++ b/exercises/practice/spiral-matrix/spiral_matrix_spec.moon @@ -0,0 +1,50 @@ +import spiral_matrix from require 'spiral_matrix' + +describe 'spiral-matrix', -> + it 'empty spiral', -> + result = spiral_matrix 0 + expected = {} + assert.are.same expected, result + + pending 'trivial spiral', -> + result = spiral_matrix 1 + expected = {{1}} + assert.are.same expected, result + + pending 'spiral of size 2', -> + result = spiral_matrix 2 + expected = { + {1, 2} + {4, 3} + } + assert.are.same expected, result + + pending 'spiral of size 3', -> + result = spiral_matrix 3 + expected = { + {1, 2, 3} + {8, 9, 4} + {7, 6, 5} + } + assert.are.same expected, result + + pending 'spiral of size 4', -> + result = spiral_matrix 4 + expected = { + {1, 2, 3, 4} + {12, 13, 14, 5} + {11, 16, 15, 6} + {10, 9, 8, 7} + } + assert.are.same expected, result + + pending 'spiral of size 5', -> + result = spiral_matrix 5 + expected = { + {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} + } + assert.are.same expected, result