From dfcb5b14df19414ef1aecd71d664c0a7671ab24e Mon Sep 17 00:00:00 2001 From: Chris Cadden Date: Wed, 14 Feb 2024 01:42:36 -0500 Subject: [PATCH] Add spiral matrix exercise (#1636) * bin/configlet create --practice-exercise spiral-matrix * Add spiral-matrix exercise difficulty * Add sprial_matrix.rb stub file * Add spiral-matrix test cases * Example spiral-matrix implementation Co-authored-by: KOTP * Update exercises/practice/spiral-matrix/.docs/instructions.md Co-authored-by: Victor Goff --------- Co-authored-by: KOTP --- config.json | 8 +++ .../spiral-matrix/.docs/instructions.md | 24 +++++++ .../practice/spiral-matrix/.meta/config.json | 19 ++++++ .../practice/spiral-matrix/.meta/example.rb | 48 ++++++++++++++ .../practice/spiral-matrix/.meta/tests.toml | 28 ++++++++ .../practice/spiral-matrix/spiral_matrix.rb | 7 ++ .../spiral-matrix/spiral_matrix_test.rb | 64 +++++++++++++++++++ 7 files changed, 198 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.rb create mode 100644 exercises/practice/spiral-matrix/.meta/tests.toml create mode 100644 exercises/practice/spiral-matrix/spiral_matrix.rb create mode 100644 exercises/practice/spiral-matrix/spiral_matrix_test.rb diff --git a/config.json b/config.json index e1ab066170..b1608fc58c 100644 --- a/config.json +++ b/config.json @@ -561,6 +561,14 @@ ], "difficulty": 5 }, + { + "slug": "spiral-matrix", + "name": "Spiral Matrix", + "uuid": "44f6c85e-a99c-4bf1-9269-67d9f25e93df", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "sum-of-multiples", "name": "Sum of Multiples", diff --git a/exercises/practice/spiral-matrix/.docs/instructions.md b/exercises/practice/spiral-matrix/.docs/instructions.md new file mode 100644 index 0000000000..87cc3c1199 --- /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. + +## 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 0000000000..1547e7f005 --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "mr-sigma" + ], + "files": { + "solution": [ + "spiral_matrix.rb" + ], + "test": [ + "spiral_matrix_test.rb" + ], + "example": [ + ".meta/example.rb" + ] + }, + "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.rb b/exercises/practice/spiral-matrix/.meta/example.rb new file mode 100644 index 0000000000..0fb82b7f53 --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/example.rb @@ -0,0 +1,48 @@ +class SpiralMatrix + private + attr_reader :counter, :dx, :dy, :x, :y + + def initialize_matrix + Array.new(size) { Array.new(size, 0) } + end + + def generate_matrix + while matrix_includes_zeroes? + @matrix[y][x] = counter + + @dy, @dx = @dx, -@dy if next_step_invalid? + + @x += dx + @y += dy + + @counter += 1 + end + end + + def next_step_invalid? + @y + @dy == @size || + (@y + @dy).negative? || + @x + @dx == @size || + (@x + @dx).negative? || + (@matrix[@y + @dy][@x + @dx]).positive? + end + + def matrix_includes_zeroes? + @matrix.any? { |row| row.any?(0) } + end + + public + attr_reader :size, :matrix + + def initialize(size) + @size = size + @matrix = initialize_matrix + @dx = 1 + @dy = 0 + @y = 0 + @x = 0 + @counter = 1 + + generate_matrix + end +end diff --git a/exercises/practice/spiral-matrix/.meta/tests.toml b/exercises/practice/spiral-matrix/.meta/tests.toml new file mode 100644 index 0000000000..9ac5bacaa2 --- /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.rb b/exercises/practice/spiral-matrix/spiral_matrix.rb new file mode 100644 index 0000000000..ed045cbcf2 --- /dev/null +++ b/exercises/practice/spiral-matrix/spiral_matrix.rb @@ -0,0 +1,7 @@ +=begin +Write your code for the 'Spiral Matrix' exercise in this file. Make the tests in +`spiraL_matrix_test.rb` pass. + +To get started with TDD, see the `README.md` file in your +`ruby/spiral-matrix` directory. +=end diff --git a/exercises/practice/spiral-matrix/spiral_matrix_test.rb b/exercises/practice/spiral-matrix/spiral_matrix_test.rb new file mode 100644 index 0000000000..89cf822af5 --- /dev/null +++ b/exercises/practice/spiral-matrix/spiral_matrix_test.rb @@ -0,0 +1,64 @@ +require 'minitest/autorun' +require_relative 'spiral_matrix' + +class SpiralMatrixTest < Minitest::Test + def test_empty_spiral + # skip + spiral = SpiralMatrix.new(0).matrix + expected = [] + assert_equal expected, spiral + end + + def test_trivial_spiral + skip + spiral = SpiralMatrix.new(1).matrix + expected = [[1]] + assert_equal expected, spiral + end + + def test_spiral_of_size_2 + skip + spiral = SpiralMatrix.new(2).matrix + expected = [ + [1, 2], + [4, 3] + ] + assert_equal expected, spiral + end + + def test_spiral_of_size_3 + skip + spiral = SpiralMatrix.new(3).matrix + expected = [ + [1, 2, 3], + [8, 9, 4], + [7, 6, 5] + ] + assert_equal expected, spiral + end + + def test_spiral_of_size_4 + skip + spiral = SpiralMatrix.new(4).matrix + expected = [ + [1, 2, 3, 4], + [12, 13, 14, 5], + [11, 16, 15, 6], + [10, 9, 8, 7] + ] + assert_equal expected, spiral + end + + def test_spiral_of_size_5 + skip + spiral = SpiralMatrix.new(5).matrix + 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_equal expected, spiral + end +end