Skip to content

Commit 9a2b8d9

Browse files
hekrausecoriolinus
authored andcommitted
Implement exercise saddle-points (#400)
* Initial saddle-points commit. * Remove diffie-hellman left-overs. * Remove old README.md * Generate new README.md * Try only to change README.md * Made requested changes. * Change tests again. * Fix merge conflict with master * Update find_saddle_points signature * Rename test `*quadratic*` -> `*square*`
1 parent bd7c659 commit 9a2b8d9

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@
185185
"mathematics"
186186
]
187187
},
188+
{
189+
"uuid": "ccebfa12-d224-11e7-8941-cec278b6b50a",
190+
"slug": "saddle-points",
191+
"core": false,
192+
"unlocked_by": null,
193+
"difficulty": 3,
194+
"topics": [
195+
"vectors",
196+
"iterators"
197+
]
198+
},
188199
{
189200
"uuid": "79613fd8-b7da-11e7-abc4-cec278b6b50a",
190201
"slug": "isogram",

exercises/saddle-points/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target/
4+
**/*.rs.bk
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8+
Cargo.lock

exercises/saddle-points/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "saddle-points"
3+
version = "1.0.0"
4+
5+
[dependencies]

exercises/saddle-points/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Saddle Points
2+
3+
Detect saddle points in a matrix.
4+
5+
So say you have a matrix like so:
6+
7+
```text
8+
0 1 2
9+
|---------
10+
0 | 9 8 7
11+
1 | 5 3 2 <--- saddle point at (1,0)
12+
2 | 6 6 7
13+
```
14+
15+
It has a saddle point at (1, 0).
16+
17+
It's called a "saddle point" because it is greater than or equal to
18+
every element in its row and less than or equal to every element in
19+
its column.
20+
21+
A matrix may have zero or more saddle points.
22+
23+
Your code should be able to provide the (possibly empty) list of all the
24+
saddle points for any given matrix.
25+
26+
Note that you may find other definitions of matrix saddle points online,
27+
but the tests for this exercise follow the above unambiguous definition.
28+
29+
## Rust Installation
30+
31+
Refer to the [exercism help page][help-page] for Rust installation and learning
32+
resources.
33+
34+
## Writing the Code
35+
36+
Execute the tests with:
37+
38+
```bash
39+
$ cargo test
40+
```
41+
42+
All but the first test have been ignored. After you get the first test to
43+
pass, remove the ignore flag (`#[ignore]`) from the next test and get the tests
44+
to pass again. The test file is located in the `tests` directory. You can
45+
also remove the ignore flag from all the tests to get them to run all at once
46+
if you wish.
47+
48+
Make sure to read the [Modules](https://doc.rust-lang.org/book/second-edition/ch07-00-modules.html) chapter if you
49+
haven't already, it will help you with organizing your files.
50+
51+
## Feedback, Issues, Pull Requests
52+
53+
The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the [rust track team](https://github.com/orgs/exercism/teams/rust) are happy to help!
54+
55+
If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
56+
57+
[help-page]: http://exercism.io/languages/rust
58+
[modules]: https://doc.rust-lang.org/book/second-edition/ch07-00-modules.html
59+
[cargo]: https://doc.rust-lang.org/book/second-edition/ch14-00-more-about-cargo.html
60+
61+
## Source
62+
63+
J Dalbey's Programming Practice problems [http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html](http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html)
64+
65+
## Submitting Incomplete Solutions
66+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

exercises/saddle-points/example.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
2+
let mut saddle_points = Vec::new();
3+
4+
let width = input.len();
5+
let height = input[0].len();
6+
7+
for i in 0..width {
8+
for j in 0..height {
9+
10+
let column = input.iter().map(|x| x[j]).collect::<Vec<u64>>();
11+
let row = &input[i];
12+
13+
let max = row.iter().max().unwrap();
14+
let min = column.iter().min().unwrap();
15+
16+
let value = input[i][j];
17+
18+
if value >= *max && value <= *min {
19+
saddle_points.push((i, j));
20+
}
21+
}
22+
}
23+
saddle_points
24+
}

exercises/saddle-points/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
2+
unimplemented!()
3+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
extern crate saddle_points;
2+
3+
use saddle_points::*;
4+
5+
#[test]
6+
fn test_identify_single_saddle_point() {
7+
let input = vec![vec![9, 8, 7], vec![5, 3, 2], vec![6, 6, 7]];
8+
assert_eq!(vec![(1, 0)], find_saddle_points(&input));
9+
}
10+
11+
#[test]
12+
#[ignore]
13+
fn test_identify_empty_matrix() {
14+
let input = vec![vec![], vec![], vec![]];
15+
let expected: Vec<(usize, usize)> = Vec::new();
16+
assert_eq!(expected, find_saddle_points(&input));
17+
}
18+
19+
#[test]
20+
#[ignore]
21+
fn test_identify_lack_of_saddle_point() {
22+
let input = vec![vec![1, 2, 3], vec![3, 1, 2], vec![2, 3, 1]];
23+
let expected: Vec<(usize, usize)> = Vec::new();
24+
assert_eq!(expected, find_saddle_points(&input));
25+
}
26+
27+
#[test]
28+
#[ignore]
29+
fn test_multiple_saddle_point() {
30+
let input = vec![vec![4, 5, 4], vec![3, 5, 5], vec![1, 5, 4]];
31+
assert_eq!(vec![(0, 1), (1, 1), (2, 1)], find_saddle_points(&input));
32+
}
33+
34+
#[test]
35+
#[ignore]
36+
fn test_identify_bottom_right_saddle_point() {
37+
let input = vec![vec![8, 7, 9], vec![6, 7, 6], vec![3, 2, 5]];
38+
assert_eq!(vec![(2, 2)], find_saddle_points(&input));
39+
}
40+
41+
#[test]
42+
#[ignore]
43+
fn test_non_square_matrix_high() {
44+
let input = vec![vec![1, 5], vec![3, 6], vec![2, 7], vec![3, 8]];
45+
assert_eq!(vec![(0, 1)], find_saddle_points(&input));
46+
}
47+
48+
#[test]
49+
#[ignore]
50+
fn test_non_square_matrix_wide() {
51+
let input = vec![vec![8, 7, 10, 7, 9], vec![8, 7, 13, 7, 9]];
52+
assert_eq!(vec![(0, 2)], find_saddle_points(&input));
53+
}
54+
55+
#[test]
56+
#[ignore]
57+
fn test_vector_matrix() {
58+
let input = vec![vec![1], vec![3], vec![2], vec![3]];
59+
assert_eq!(vec![(0, 0)], find_saddle_points(&input));
60+
}

0 commit comments

Comments
 (0)