diff --git a/config.json b/config.json index 65d9a339f..eb375519a 100644 --- a/config.json +++ b/config.json @@ -185,6 +185,17 @@ "mathematics" ] }, + { + "uuid": "ccebfa12-d224-11e7-8941-cec278b6b50a", + "slug": "saddle-points", + "core": false, + "unlocked_by": null, + "difficulty": 3, + "topics": [ + "vectors", + "iterators" + ] + }, { "uuid": "79613fd8-b7da-11e7-abc4-cec278b6b50a", "slug": "isogram", diff --git a/exercises/saddle-points/.gitignore b/exercises/saddle-points/.gitignore new file mode 100644 index 000000000..db7f315c0 --- /dev/null +++ b/exercises/saddle-points/.gitignore @@ -0,0 +1,8 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ +**/*.rs.bk + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock +Cargo.lock diff --git a/exercises/saddle-points/Cargo.toml b/exercises/saddle-points/Cargo.toml new file mode 100644 index 000000000..f6f20014b --- /dev/null +++ b/exercises/saddle-points/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "saddle-points" +version = "1.0.0" + +[dependencies] diff --git a/exercises/saddle-points/README.md b/exercises/saddle-points/README.md new file mode 100644 index 000000000..2fa2c18ec --- /dev/null +++ b/exercises/saddle-points/README.md @@ -0,0 +1,66 @@ +# Saddle Points + +Detect saddle points in a matrix. + +So say you have a matrix like so: + +```text + 0 1 2 + |--------- +0 | 9 8 7 +1 | 5 3 2 <--- saddle point at (1,0) +2 | 6 6 7 +``` + +It has a saddle point at (1, 0). + +It's called a "saddle point" because it is greater than or equal to +every element in its row and less than or equal to every element in +its column. + +A matrix may have zero or more saddle points. + +Your code should be able to provide the (possibly empty) list of all the +saddle points for any given matrix. + +Note that you may find other definitions of matrix saddle points online, +but the tests for this exercise follow the above unambiguous definition. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +Execute the tests with: + +```bash +$ cargo test +``` + +All but the first test have been ignored. After you get the first test to +pass, remove the ignore flag (`#[ignore]`) from the next test and get the tests +to pass again. The test file is located in the `tests` directory. You can +also remove the ignore flag from all the tests to get them to run all at once +if you wish. + +Make sure to read the [Modules](https://doc.rust-lang.org/book/second-edition/ch07-00-modules.html) chapter if you +haven't already, it will help you with organizing your files. + +## Feedback, Issues, Pull Requests + +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! + +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). + +[help-page]: http://exercism.io/languages/rust +[modules]: https://doc.rust-lang.org/book/second-edition/ch07-00-modules.html +[cargo]: https://doc.rust-lang.org/book/second-edition/ch14-00-more-about-cargo.html + +## Source + +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) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/saddle-points/example.rs b/exercises/saddle-points/example.rs new file mode 100644 index 000000000..e3d9c7a7c --- /dev/null +++ b/exercises/saddle-points/example.rs @@ -0,0 +1,24 @@ +pub fn find_saddle_points(input: &[Vec]) -> Vec<(usize, usize)> { + let mut saddle_points = Vec::new(); + + let width = input.len(); + let height = input[0].len(); + + for i in 0..width { + for j in 0..height { + + let column = input.iter().map(|x| x[j]).collect::>(); + let row = &input[i]; + + let max = row.iter().max().unwrap(); + let min = column.iter().min().unwrap(); + + let value = input[i][j]; + + if value >= *max && value <= *min { + saddle_points.push((i, j)); + } + } + } + saddle_points +} diff --git a/exercises/saddle-points/src/lib.rs b/exercises/saddle-points/src/lib.rs new file mode 100644 index 000000000..7c3aee704 --- /dev/null +++ b/exercises/saddle-points/src/lib.rs @@ -0,0 +1,3 @@ +pub fn find_saddle_points(input: &[Vec]) -> Vec<(usize, usize)> { + unimplemented!() +} diff --git a/exercises/saddle-points/tests/saddle-points.rs b/exercises/saddle-points/tests/saddle-points.rs new file mode 100644 index 000000000..8021b79dc --- /dev/null +++ b/exercises/saddle-points/tests/saddle-points.rs @@ -0,0 +1,60 @@ +extern crate saddle_points; + +use saddle_points::*; + +#[test] +fn test_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 test_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 test_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)); +} + +#[test] +#[ignore] +fn test_multiple_saddle_point() { + 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_saddle_points(&input)); +} + +#[test] +#[ignore] +fn test_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)); +} + +#[test] +#[ignore] +fn test_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)); +} + +#[test] +#[ignore] +fn test_non_square_matrix_wide() { + let input = vec![vec![8, 7, 10, 7, 9], vec![8, 7, 13, 7, 9]]; + assert_eq!(vec![(0, 2)], find_saddle_points(&input)); +} + +#[test] +#[ignore] +fn test_vector_matrix() { + let input = vec![vec![1], vec![3], vec![2], vec![3]]; + assert_eq!(vec![(0, 0)], find_saddle_points(&input)); +}