Skip to content

Commit dfcb5b1

Browse files
ccaddenkotp
andauthored
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 <[email protected]> * Update exercises/practice/spiral-matrix/.docs/instructions.md Co-authored-by: Victor Goff <[email protected]> --------- Co-authored-by: KOTP <[email protected]>
1 parent 0aa2712 commit dfcb5b1

File tree

7 files changed

+198
-0
lines changed

7 files changed

+198
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,14 @@
561561
],
562562
"difficulty": 5
563563
},
564+
{
565+
"slug": "spiral-matrix",
566+
"name": "Spiral Matrix",
567+
"uuid": "44f6c85e-a99c-4bf1-9269-67d9f25e93df",
568+
"practices": [],
569+
"prerequisites": [],
570+
"difficulty": 5
571+
},
564572
{
565573
"slug": "sum-of-multiples",
566574
"name": "Sum of Multiples",
Lines changed: 24 additions & 0 deletions
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.
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+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"mr-sigma"
4+
],
5+
"files": {
6+
"solution": [
7+
"spiral_matrix.rb"
8+
],
9+
"test": [
10+
"spiral_matrix_test.rb"
11+
],
12+
"example": [
13+
".meta/example.rb"
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class SpiralMatrix
2+
private
3+
attr_reader :counter, :dx, :dy, :x, :y
4+
5+
def initialize_matrix
6+
Array.new(size) { Array.new(size, 0) }
7+
end
8+
9+
def generate_matrix
10+
while matrix_includes_zeroes?
11+
@matrix[y][x] = counter
12+
13+
@dy, @dx = @dx, -@dy if next_step_invalid?
14+
15+
@x += dx
16+
@y += dy
17+
18+
@counter += 1
19+
end
20+
end
21+
22+
def next_step_invalid?
23+
@y + @dy == @size ||
24+
(@y + @dy).negative? ||
25+
@x + @dx == @size ||
26+
(@x + @dx).negative? ||
27+
(@matrix[@y + @dy][@x + @dx]).positive?
28+
end
29+
30+
def matrix_includes_zeroes?
31+
@matrix.any? { |row| row.any?(0) }
32+
end
33+
34+
public
35+
attr_reader :size, :matrix
36+
37+
def initialize(size)
38+
@size = size
39+
@matrix = initialize_matrix
40+
@dx = 1
41+
@dy = 0
42+
@y = 0
43+
@x = 0
44+
@counter = 1
45+
46+
generate_matrix
47+
end
48+
end
Lines changed: 28 additions & 0 deletions
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"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=begin
2+
Write your code for the 'Spiral Matrix' exercise in this file. Make the tests in
3+
`spiraL_matrix_test.rb` pass.
4+
5+
To get started with TDD, see the `README.md` file in your
6+
`ruby/spiral-matrix` directory.
7+
=end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require 'minitest/autorun'
2+
require_relative 'spiral_matrix'
3+
4+
class SpiralMatrixTest < Minitest::Test
5+
def test_empty_spiral
6+
# skip
7+
spiral = SpiralMatrix.new(0).matrix
8+
expected = []
9+
assert_equal expected, spiral
10+
end
11+
12+
def test_trivial_spiral
13+
skip
14+
spiral = SpiralMatrix.new(1).matrix
15+
expected = [[1]]
16+
assert_equal expected, spiral
17+
end
18+
19+
def test_spiral_of_size_2
20+
skip
21+
spiral = SpiralMatrix.new(2).matrix
22+
expected = [
23+
[1, 2],
24+
[4, 3]
25+
]
26+
assert_equal expected, spiral
27+
end
28+
29+
def test_spiral_of_size_3
30+
skip
31+
spiral = SpiralMatrix.new(3).matrix
32+
expected = [
33+
[1, 2, 3],
34+
[8, 9, 4],
35+
[7, 6, 5]
36+
]
37+
assert_equal expected, spiral
38+
end
39+
40+
def test_spiral_of_size_4
41+
skip
42+
spiral = SpiralMatrix.new(4).matrix
43+
expected = [
44+
[1, 2, 3, 4],
45+
[12, 13, 14, 5],
46+
[11, 16, 15, 6],
47+
[10, 9, 8, 7]
48+
]
49+
assert_equal expected, spiral
50+
end
51+
52+
def test_spiral_of_size_5
53+
skip
54+
spiral = SpiralMatrix.new(5).matrix
55+
expected = [
56+
[1, 2, 3, 4, 5],
57+
[16, 17, 18, 19, 6],
58+
[15, 24, 25, 20, 7],
59+
[14, 23, 22, 21, 8],
60+
[13, 12, 11, 10, 9]
61+
]
62+
assert_equal expected, spiral
63+
end
64+
end

0 commit comments

Comments
 (0)