Skip to content

Commit

Permalink
Sync spiral-matrix with problem-specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Nov 13, 2023
1 parent 025710e commit bf2f49b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 40 deletions.
5 changes: 2 additions & 3 deletions exercises/practice/spiral-matrix/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

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:
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
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/spiral-matrix/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
".meta/example.rs"
]
},
"blurb": " Given the size, return a square matrix of numbers in spiral order.",
"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://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/"
}
16 changes: 16 additions & 0 deletions exercises/practice/spiral-matrix/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% for test in cases %}
#[test]
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let input = {{ test.input.size | json_encode() }};
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
let expected: [[u32; {{ test.input.size }}]; {{ test.input.size }}] = [
{% for i in test.expected %}
{{ i | json_encode }},
{% endfor %}
];
assert_eq!(output, expected);
}
{% endfor -%}
28 changes: 25 additions & 3 deletions exercises/practice/spiral-matrix/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# 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"
74 changes: 41 additions & 33 deletions exercises/practice/spiral-matrix/tests/spiral-matrix.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,63 @@
use spiral_matrix::*;

#[test]
fn empty_spiral() {
let expected: Vec<Vec<u32>> = Vec::new();
assert_eq!(spiral_matrix(0), expected);
let input = 0;
let output = spiral_matrix::spiral_matrix(input);
let expected: [[u32; 0]; 0] = [];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn size_one_spiral() {
let expected: Vec<Vec<u32>> = vec![vec![1]];
assert_eq!(spiral_matrix(1), expected);
fn trivial_spiral() {
let input = 1;
let output = spiral_matrix::spiral_matrix(input);
let expected: [[u32; 1]; 1] = [[1]];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn size_two_spiral() {
let expected: Vec<Vec<u32>> = vec![vec![1, 2], vec![4, 3]];
assert_eq!(spiral_matrix(2), expected);
fn spiral_of_size_2() {
let input = 2;
let output = spiral_matrix::spiral_matrix(input);
let expected: [[u32; 2]; 2] = [[1, 2], [4, 3]];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn size_three_spiral() {
#[rustfmt::skip]
let expected: Vec<Vec<u32>> = vec![
vec![1, 2, 3],
vec![8, 9, 4],
vec![7, 6, 5],
];
assert_eq!(spiral_matrix(3), expected);
fn spiral_of_size_3() {
let input = 3;
let output = spiral_matrix::spiral_matrix(input);
let expected: [[u32; 3]; 3] = [[1, 2, 3], [8, 9, 4], [7, 6, 5]];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn size_four_spiral() {
let expected: Vec<Vec<u32>> = vec![
vec![1, 2, 3, 4],
vec![12, 13, 14, 5],
vec![11, 16, 15, 6],
vec![10, 9, 8, 7],
fn spiral_of_size_4() {
let input = 4;
let output = spiral_matrix::spiral_matrix(input);
let expected: [[u32; 4]; 4] = [
[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7],
];
assert_eq!(spiral_matrix(4), expected);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn size_five_spiral() {
let expected: Vec<Vec<u32>> = vec![
vec![1, 2, 3, 4, 5],
vec![16, 17, 18, 19, 6],
vec![15, 24, 25, 20, 7],
vec![14, 23, 22, 21, 8],
vec![13, 12, 11, 10, 9],
fn spiral_of_size_5() {
let input = 5;
let output = spiral_matrix::spiral_matrix(input);
let expected: [[u32; 5]; 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],
];
assert_eq!(spiral_matrix(5), expected);
assert_eq!(output, expected);
}

0 comments on commit bf2f49b

Please sign in to comment.