Skip to content

Commit

Permalink
Sync saddle-point with problem-specifications (#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor authored Nov 14, 2023
1 parent 253fcb6 commit f6a005b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 77 deletions.
6 changes: 3 additions & 3 deletions exercises/practice/saddle-points/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ Your task is to find the potential trees where you could build your tree house.
The data company provides the data as grids that show the heights of the trees.
The rows of the grid represent the east-west direction, and the columns represent the north-south direction.

An acceptable tree will be the the largest in its row, while being the smallest in its column.
An acceptable tree will be the largest in its row, while being the smallest in its column.

A grid might not have any good trees at all.
Or it might have one, or even several.

Here is a grid that has exactly one candidate tree.

```
```text
1 2 3 4
|-----------
1 | 9 8 7 8
2 | 5 3 2 4 <--- potential tree house at row 2, column 1, for tree with height 5
3 | 6 6 7 1
```

- Row 2 has values 5, 3, and 1. The largest value is 5.
- Row 2 has values 5, 3, 2, and 4. The largest value is 5.
- Column 1 has values 9, 5, and 6. The smallest value is 5.

So the point at `[2, 1]` (row: 2, column: 1) is a great spot for a tree house.
12 changes: 7 additions & 5 deletions exercises/practice/saddle-points/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Introduction

You are planning on building a tree house in the woods near your house so that you can watch the sun rise and set.
You plan to build a tree house in the woods near your house so that you can watch the sun rise and set.

You've obtained data from a local survey company that shows the heights of all the trees in each rectangular section of the map.
You need to analyze each grid on the map to find the perfect tree for your tree house.
You've obtained data from a local survey company that show the height of every tree in each rectangular section of the map.
You need to analyze each grid on the map to find good trees for your tree house.

The best tree will be the tallest tree compared to all the other trees to the east and west, so that you have the best possible view of the sunrises and sunsets.
You don't like climbing too much, so the perfect tree will also be the shortest among all the trees to the north and to the south.
A good tree is both:

- taller than every tree to the east and west, so that you have the best possible view of the sunrises and sunsets.
- shorter than every tree to the north and south, to minimize the amount of tree climbing.
2 changes: 1 addition & 1 deletion exercises/practice/saddle-points/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
},
"blurb": "Detect saddle points in a matrix.",
"source": "J Dalbey's Programming Practice problems",
"source_url": "http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html"
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html"
}
24 changes: 24 additions & 0 deletions exercises/practice/saddle-points/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// We don't care about order
fn find_sorted_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
let mut result = saddle_points::find_saddle_points(input);
result.sort_unstable();
result
}
{% for test in cases %}
#[test]
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let input = &[{% for row in test.input.matrix %}
vec!{{ row }},
{% endfor %}];
let output = find_sorted_saddle_points(input);
let expected = &[
{% for p in test.expected | sort(attribute = "column") | sort(attribute = "row") %}
({{ p.row - 1 }}, {{ p.column - 1 }}),
{% endfor %}
];
assert_eq!(output, expected);
}
{% endfor -%}
40 changes: 37 additions & 3 deletions exercises/practice/saddle-points/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
# 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.

[3e374e63-a2e0-4530-a39a-d53c560382bd]
description = "Can identify single saddle point"

[6b501e2b-6c1f-491f-b1bb-7f278f760534]
description = "Can identify that empty matrix has no saddle points"

[8c27cc64-e573-4fcb-a099-f0ae863fb02f]
description = "Can identify lack of saddle points when there are none"

[6d1399bd-e105-40fd-a2c9-c6609507d7a3]
description = "Can identify multiple saddle points in a column"

[3e81dce9-53b3-44e6-bf26-e328885fd5d1]
description = "Can identify multiple saddle points in a row"

[88868621-b6f4-4837-bb8b-3fad8b25d46b]
description = "Can identify saddle point in bottom right corner"

[5b9499ca-fcea-4195-830a-9c4584a0ee79]
description = "Can identify saddle points in a non square matrix"

[ee99ccd2-a1f1-4283-ad39-f8c70f0cf594]
description = "Can identify that saddle points in a single column matrix are those with the minimum value"

[63abf709-a84b-407f-a1b3-456638689713]
description = "Can identify that saddle points in a single row matrix are those with the maximum value"
110 changes: 45 additions & 65 deletions exercises/practice/saddle-points/tests/saddle-points.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use saddle_points::find_saddle_points;

// We don't care about order
fn find_sorted_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
let mut result = saddle_points::find_saddle_points(input);
Expand All @@ -8,99 +6,81 @@ fn find_sorted_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
}

#[test]
fn identify_single_saddle_point() {
let input = vec![vec![9, 8, 7], vec![5, 3, 2], vec![6, 6, 7]];
assert_eq!(vec![(1, 0)], find_saddle_points(&input));
}

#[test]
#[ignore]
fn identify_empty_matrix() {
let input = vec![vec![], vec![], vec![]];
let expected: Vec<(usize, usize)> = Vec::new();
assert_eq!(expected, find_saddle_points(&input));
}

#[test]
#[ignore]
fn identify_lack_of_saddle_point() {
let input = vec![vec![1, 2, 3], vec![3, 1, 2], vec![2, 3, 1]];
let expected: Vec<(usize, usize)> = Vec::new();
assert_eq!(expected, find_saddle_points(&input));
fn can_identify_single_saddle_point() {
let input = &[vec![9, 8, 7], vec![5, 3, 2], vec![6, 6, 7]];
let output = find_sorted_saddle_points(input);
let expected = &[(1, 0)];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn multiple_saddle_points_in_col() {
let input = vec![vec![4, 5, 4], vec![3, 5, 5], vec![1, 5, 4]];
assert_eq!(
vec![(0, 1), (1, 1), (2, 1)],
find_sorted_saddle_points(&input)
);
fn can_identify_that_empty_matrix_has_no_saddle_points() {
let input = &[vec![]];
let output = find_sorted_saddle_points(input);
let expected = &[];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn multiple_saddle_points_in_row() {
let input = vec![vec![6, 7, 8], vec![5, 5, 5], vec![7, 5, 6]];
assert_eq!(
vec![(1, 0), (1, 1), (1, 2)],
find_sorted_saddle_points(&input)
);
fn can_identify_lack_of_saddle_points_when_there_are_none() {
let input = &[vec![1, 2, 3], vec![3, 1, 2], vec![2, 3, 1]];
let output = find_sorted_saddle_points(input);
let expected = &[];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn identify_bottom_right_saddle_point() {
let input = vec![vec![8, 7, 9], vec![6, 7, 6], vec![3, 2, 5]];
assert_eq!(vec![(2, 2)], find_saddle_points(&input));
fn can_identify_multiple_saddle_points_in_a_column() {
let input = &[vec![4, 5, 4], vec![3, 5, 5], vec![1, 5, 4]];
let output = find_sorted_saddle_points(input);
let expected = &[(0, 1), (1, 1), (2, 1)];
assert_eq!(output, expected);
}

// track specific as of v1.3
#[test]
#[ignore]
fn non_square_matrix_high() {
let input = vec![vec![1, 5], vec![3, 6], vec![2, 7], vec![3, 8]];
assert_eq!(vec![(0, 1)], find_saddle_points(&input));
fn can_identify_multiple_saddle_points_in_a_row() {
let input = &[vec![6, 7, 8], vec![5, 5, 5], vec![7, 5, 6]];
let output = find_sorted_saddle_points(input);
let expected = &[(1, 0), (1, 1), (1, 2)];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn non_square_matrix_wide() {
let input = vec![vec![3, 1, 3], vec![3, 2, 4]];
assert_eq!(vec![(0, 0), (0, 2)], find_sorted_saddle_points(&input));
fn can_identify_saddle_point_in_bottom_right_corner() {
let input = &[vec![8, 7, 9], vec![6, 7, 6], vec![3, 2, 5]];
let output = find_sorted_saddle_points(input);
let expected = &[(2, 2)];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn single_column_matrix() {
let input = vec![vec![2], vec![1], vec![4], vec![1]];
assert_eq!(vec![(1, 0), (3, 0)], find_sorted_saddle_points(&input));
fn can_identify_saddle_points_in_a_non_square_matrix() {
let input = &[vec![3, 1, 3], vec![3, 2, 4]];
let output = find_sorted_saddle_points(input);
let expected = &[(0, 0), (0, 2)];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn single_row_matrix() {
let input = vec![vec![2, 5, 3, 5]];
assert_eq!(vec![(0, 1), (0, 3)], find_sorted_saddle_points(&input));
fn can_identify_that_saddle_points_in_a_single_column_matrix_are_those_with_the_minimum_value() {
let input = &[vec![2], vec![1], vec![4], vec![1]];
let output = find_sorted_saddle_points(input);
let expected = &[(1, 0), (3, 0)];
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn identify_all_saddle_points() {
let input = vec![vec![5, 5, 5], vec![5, 5, 5], vec![5, 5, 5]];
assert_eq!(
vec![
(0, 0),
(0, 1),
(0, 2),
(1, 0),
(1, 1),
(1, 2),
(2, 0),
(2, 1),
(2, 2)
],
find_sorted_saddle_points(&input)
);
fn can_identify_that_saddle_points_in_a_single_row_matrix_are_those_with_the_maximum_value() {
let input = &[vec![2, 5, 3, 5]];
let output = find_sorted_saddle_points(input);
let expected = &[(0, 1), (0, 3)];
assert_eq!(output, expected);
}

0 comments on commit f6a005b

Please sign in to comment.