From d0662395cbeb2bf7414f19086e8610c18a2d097c Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Wed, 15 May 2024 06:44:36 +1000 Subject: [PATCH] Add spiral-matrix exercise --- config.json | 8 ++++ .../spiral-matrix/.docs/instructions.md | 24 ++++++++++ .../practice/spiral-matrix/.meta/config.json | 19 ++++++++ .../practice/spiral-matrix/.meta/example.el | 41 ++++++++++++++++ .../practice/spiral-matrix/.meta/tests.toml | 28 +++++++++++ .../spiral-matrix/spiral-matrix-test.el | 47 +++++++++++++++++++ .../practice/spiral-matrix/spiral-matrix.el | 14 ++++++ 7 files changed, 181 insertions(+) create mode 100644 exercises/practice/spiral-matrix/.docs/instructions.md create mode 100644 exercises/practice/spiral-matrix/.meta/config.json create mode 100644 exercises/practice/spiral-matrix/.meta/example.el create mode 100644 exercises/practice/spiral-matrix/.meta/tests.toml create mode 100644 exercises/practice/spiral-matrix/spiral-matrix-test.el create mode 100644 exercises/practice/spiral-matrix/spiral-matrix.el diff --git a/config.json b/config.json index 5d9d873a..60a7a381 100644 --- a/config.json +++ b/config.json @@ -731,6 +731,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "spiral-matrix", + "name": "Spiral Matrix", + "uuid": "4ce2b869-140a-4d5d-90dd-6fc717ef7696", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "word-count", "name": "Word Count", diff --git a/exercises/practice/spiral-matrix/.docs/instructions.md b/exercises/practice/spiral-matrix/.docs/instructions.md new file mode 100644 index 00000000..ba99e12c --- /dev/null +++ b/exercises/practice/spiral-matrix/.docs/instructions.md @@ -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 +``` diff --git a/exercises/practice/spiral-matrix/.meta/config.json b/exercises/practice/spiral-matrix/.meta/config.json new file mode 100644 index 00000000..20a3b7fb --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "spiral-matrix.el" + ], + "test": [ + "spiral-matrix-test.el" + ], + "example": [ + ".meta/example.el" + ] + }, + "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.el b/exercises/practice/spiral-matrix/.meta/example.el new file mode 100644 index 00000000..dfd6ddc9 --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/example.el @@ -0,0 +1,41 @@ +;;; spiral-matrix.el --- Spiral Matrix (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + +(require 'cl-lib) + +(defun spiral-matrix (size) + (let* ((result (make-vector size nil)) + (value 1) + (row 0) + (column 0) + (side (1- size))) + (cl-loop for r below size + do (aset result r (make-vector size (* size size)))) + (while (>= side 1) + (cl-loop repeat side + do (aset (aref result row) column value) + do (setq value (1+ value)) + do (setq column (1+ column))) + (cl-loop repeat side + do (aset (aref result row) column value) + do (setq value (1+ value)) + do (setq row (1+ row))) + (cl-loop repeat side + do (aset (aref result row) column value) + do (setq value (1+ value)) + do (setq column (1- column))) + (cl-loop repeat side + do (aset (aref result row) column value) + do (setq value (1+ value)) + do (setq row (1- row))) + (setq row (1+ row)) + (setq column (1+ column)) + (setq side (- side 2))) + result)) + +(provide 'spiral-matrix) +;;; spiral-matrix.el ends here + diff --git a/exercises/practice/spiral-matrix/.meta/tests.toml b/exercises/practice/spiral-matrix/.meta/tests.toml new file mode 100644 index 00000000..9ac5baca --- /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-test.el b/exercises/practice/spiral-matrix/spiral-matrix-test.el new file mode 100644 index 00000000..ee9f6fd5 --- /dev/null +++ b/exercises/practice/spiral-matrix/spiral-matrix-test.el @@ -0,0 +1,47 @@ +;;; spiral-matrix-test.el --- Tests for Spiral Matrix (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(load-file "spiral-matrix.el") +(declare-function spiral-matrix "spiral-matrix.el" (size)) + + +(ert-deftest empty-spiral () + (should (equal [] (spiral-matrix 0)))) + + +(ert-deftest trivial-spiral () + (should (equal [[1]] (spiral-matrix 1)))) + + +(ert-deftest spiral-of-size-2 () + (should (equal [[1 2] + [4 3]] (spiral-matrix 2)))) + + +(ert-deftest spiral-of-size-3 () + (should (equal [[1 2 3] + [8 9 4] + [7 6 5]] (spiral-matrix 3)))) + + +(ert-deftest spiral-of-size-4 () + (should (equal [[1 2 3 4] + [12 13 14 5] + [11 16 15 6] + [10 9 8 7]] (spiral-matrix 4)))) + + +(ert-deftest spiral-of-size-5 () + (should (equal [[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-matrix 5)))) + + +(provide 'spiral-matrix-test) +;;; spiral-matrix-test.el ends here diff --git a/exercises/practice/spiral-matrix/spiral-matrix.el b/exercises/practice/spiral-matrix/spiral-matrix.el new file mode 100644 index 00000000..efefe513 --- /dev/null +++ b/exercises/practice/spiral-matrix/spiral-matrix.el @@ -0,0 +1,14 @@ +;;; spiral-matrix.el --- Spiral Matrix (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(defun spiral-matrix (size) + (error "Delete this S-Expression and write your own implementation")) + + +(provide 'spiral-matrix) +;;; spiral-matrix.el ends here +