-
Notifications
You must be signed in to change notification settings - Fork 544
Implement exercise saddle-points #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
e04e3c0
cd20bcf
d9c2e9d
6998c3e
1441317
168135d
fce8915
3833a54
20c0ab0
834b743
50b0d69
72b1604
c784f9d
1d04e9a
f2c139d
c5e0144
c6fa7ff
bb710fc
fb0f63b
45c002d
b64d368
ab95998
1f07992
badcc30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
name = "saddle-points" | ||
version = "1.0.0" | ||
|
||
[dependencies] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pub fn find_saddle_points(input: &[&[u64]]) -> Vec<(u64, u64)>{ | ||
let mut saddle_points: Vec<(u64, u64)> = 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::<Vec<u64>>(); | ||
let row = &input[i]; | ||
|
||
let max = row.iter().max().unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Indeed, if a row is empty. In that case, I will now only optionally ask for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you are right but I would like to leave it like this because I think in the way it is now it is more readable. |
||
let min = column.iter().min().unwrap(); | ||
|
||
let value = input[i][j]; | ||
|
||
if value >= *max && value <= *min { | ||
saddle_points.push((i as u64, j as u64)); | ||
|
||
} | ||
} | ||
} | ||
saddle_points | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub fn find_saddle_points(input: &[&[u64]]) -> Vec<(u64, u64)>{ unimplemented!() } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
extern crate saddle_points; | ||
|
||
use saddle_points::*; | ||
|
||
#[test] | ||
fn test_identify_single_saddle_point() { | ||
let vector: Vec<Vec<u64>> = vec![vec![9, 8, 7], | ||
vec![5, 3, 2], | ||
vec![6, 6, 7]]; | ||
let input: &[&[u64]] = &[&vector[0][..], | ||
|
||
&vector[1][..], | ||
&vector[2][..]]; | ||
assert_eq!(vec![(1,0)], find_saddle_points(input)); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_identify_empty_matrix() { | ||
let vector: Vec<Vec<u64>> = vec![vec![], | ||
vec![], | ||
vec![]]; | ||
let input: &[&[u64]] = &[&vector[0][..], | ||
&vector[1][..], | ||
&vector[2][..]]; | ||
let expected: Vec<(u64, u64)> = Vec::new(); | ||
|
||
assert_eq!(expected, find_saddle_points(input)); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_identify_lack_of_saddle_point() { | ||
let vector: Vec<Vec<u64>> = vec![vec![1, 2, 3], | ||
vec![3, 1, 2], | ||
vec![2, 3, 1]]; | ||
let input: &[&[u64]] = &[&vector[0][..], | ||
&vector[1][..], | ||
&vector[2][..]]; | ||
let expected: Vec<(u64, u64)> = Vec::new(); | ||
assert_eq!(expected, find_saddle_points(input)); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_multiple_saddle_point() { | ||
let vector: Vec<Vec<u64>> = vec![vec![4, 5, 4], | ||
vec![3, 5, 5], | ||
vec![1, 5, 4]]; | ||
let input: &[&[u64]] = &[&vector[0][..], | ||
&vector[1][..], | ||
&vector[2][..]]; | ||
assert_eq!(vec![(0,1), (1,1), (2,1)], find_saddle_points(input)); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_identify_bottom_right_saddle_point() { | ||
let vector: Vec<Vec<u64>> = vec![vec![8, 7, 9], | ||
vec![6, 7, 6], | ||
vec![3, 2, 5]]; | ||
let input: &[&[u64]] = &[&vector[0][..], | ||
&vector[1][..], | ||
&vector[2][..]]; | ||
assert_eq!(vec![(2,2)], find_saddle_points(input)); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_non_square_matrix_high() { | ||
let vector: Vec<Vec<u64>> = vec![vec![1, 5], | ||
vec![3, 6], | ||
vec![2, 7], | ||
vec![3, 8]]; | ||
let input: &[&[u64]] = &[&vector[0][..], | ||
&vector[1][..], | ||
&vector[2][..], | ||
&vector[3][..]]; | ||
assert_eq!(vec![(0, 1)], find_saddle_points(input)); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_non_quadratic_matrix_wide() { | ||
let vector: Vec<Vec<u64>> = vec![vec![8, 7, 10, 7, 9], | ||
vec![8, 7, 13, 7, 9]]; | ||
let input: &[&[u64]] = &[&vector[0][..], | ||
&vector[1][..]]; | ||
assert_eq!(vec![(0, 2)], find_saddle_points(input)); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_vector_matrix() { | ||
let vector: Vec<Vec<u64>> = vec![vec![1], | ||
vec![3], | ||
vec![2], | ||
vec![3]]; | ||
let input: &[&[u64]] = &[&vector[0][..], | ||
&vector[1][..], | ||
&vector[2][..], | ||
&vector[3][..]]; | ||
assert_eq!(vec![(0, 0)], find_saddle_points(input)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this is only based on
row
, which is only based oni
and notj
, it would be possible to assign it in the outer loop and not the inner one. Unless that makes the code less understandable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the matrix is empty the two for loops prevent an unwrap on
row
in line 13.