aboutsummaryrefslogtreecommitdiff
path: root/saddle-points
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2020-07-13 09:20:52 +0200
committerFederico Igne <git@federicoigne.com>2021-11-03 18:55:08 +0000
commit99dbef8747116ae11b6e601af0187a72447dd021 (patch)
tree6e90a2420262624a1309241d978acf8ee79cc52f /saddle-points
parent354a0ed1725ae5408e097164714599b6b1bb66c8 (diff)
downloadexercism-99dbef8747116ae11b6e601af0187a72447dd021.tar.gz
exercism-99dbef8747116ae11b6e601af0187a72447dd021.zip
[rust] Saddle Points
Diffstat (limited to 'saddle-points')
-rw-r--r--saddle-points/.exercism/metadata.json1
-rw-r--r--saddle-points/.gitignore8
-rw-r--r--saddle-points/Cargo.toml6
-rw-r--r--saddle-points/README.md132
-rw-r--r--saddle-points/src/lib.rs16
-rw-r--r--saddle-points/tests/saddle-points.rs98
6 files changed, 261 insertions, 0 deletions
diff --git a/saddle-points/.exercism/metadata.json b/saddle-points/.exercism/metadata.json
new file mode 100644
index 0000000..cb44aa3
--- /dev/null
+++ b/saddle-points/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"saddle-points","id":"18964e9c5971482790e97a569346a40d","url":"https://exercism.io/my/solutions/18964e9c5971482790e97a569346a40d","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/saddle-points/.gitignore b/saddle-points/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/saddle-points/.gitignore
@@ -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
8Cargo.lock
diff --git a/saddle-points/Cargo.toml b/saddle-points/Cargo.toml
new file mode 100644
index 0000000..5f262e2
--- /dev/null
+++ b/saddle-points/Cargo.toml
@@ -0,0 +1,6 @@
1[package]
2edition = "2018"
3name = "saddle-points"
4version = "1.3.0"
5
6[dependencies]
diff --git a/saddle-points/README.md b/saddle-points/README.md
new file mode 100644
index 0000000..84647d0
--- /dev/null
+++ b/saddle-points/README.md
@@ -0,0 +1,132 @@
1# Saddle Points
2
3Detect saddle points in a matrix.
4
5So say you have a matrix like so:
6
7```text
8 1 2 3
9 |---------
101 | 9 8 7
112 | 5 3 2 <--- saddle point at column 1, row 2, with value 5
123 | 6 6 7
13```
14
15It has a saddle point at column 1, row 2.
16
17It's called a "saddle point" because it is greater than or equal to
18every element in its row and less than or equal to every element in
19its column.
20
21A matrix may have zero or more saddle points.
22
23Your code should be able to provide the (possibly empty) list of all the
24saddle points for any given matrix.
25
26The matrix can have a different number of rows and columns (Non square).
27
28Note that you may find other definitions of matrix saddle points online,
29but the tests for this exercise follow the above unambiguous definition.
30
31## Rust Indices Start At 0
32
33By convention, ordered sequences of values in Rust have their contents numbered
34("indexed") starting from 0. This applies regardless of what the rest of the
35exercise description in this README says, such as references to indices that
36start at 1, so you will have to subtract 1 to translate those index numbers
37to Rust index numbers.
38
39## Efficiency Notice
40
41This exercise uses a _vector of vectors_ to store the content of matrices. While
42this exercise is designed to help students understand basic concepts about
43vectors, such as indexing, and that nested data types are legal, _vector of
44vectors_ is a suboptimal choice for high-performance matrix algebra and any
45similar efficient processing of larger amounts of data.
46
47The detailed explanation of this inefficiency is beyond the scope of this
48exercise and this learning track in general. This aspect is known as
49[cache locality](https://stackoverflow.com/questions/12065774/why-does-cache-locality-matter-for-array-performance)
50and you can find a good introduction to it by clicking that link if you'd like
51to learn more about details of a modern computer architecture.
52
53
54## Rust Installation
55
56Refer to the [exercism help page][help-page] for Rust installation and learning
57resources.
58
59## Writing the Code
60
61Execute the tests with:
62
63```bash
64$ cargo test
65```
66
67All but the first test have been ignored. After you get the first test to
68pass, open the tests source file which is located in the `tests` directory
69and remove the `#[ignore]` flag from the next test and get the tests to pass
70again. Each separate test is a function with `#[test]` flag above it.
71Continue, until you pass every test.
72
73If you wish to run all ignored tests without editing the tests source file, use:
74
75```bash
76$ cargo test -- --ignored
77```
78
79To run a specific test, for example `some_test`, you can use:
80
81```bash
82$ cargo test some_test
83```
84
85If the specific test is ignored use:
86
87```bash
88$ cargo test some_test -- --ignored
89```
90
91To learn more about Rust tests refer to the [online test documentation][rust-tests]
92
93Make sure to read the [Modules][modules] chapter if you
94haven't already, it will help you with organizing your files.
95
96## Further improvements
97
98After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
99
100To format your solution, inside the solution directory use
101
102```bash
103cargo fmt
104```
105
106To see, if your solution contains some common ineffective use cases, inside the solution directory use
107
108```bash
109cargo clippy --all-targets
110```
111
112## Submitting the solution
113
114Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
115
116## Feedback, Issues, Pull Requests
117
118The [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 are happy to help!
119
120If 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).
121
122[help-page]: https://exercism.io/tracks/rust/learning
123[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
124[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
125[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
126
127## Source
128
129J 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)
130
131## Submitting Incomplete Solutions
132It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/saddle-points/src/lib.rs b/saddle-points/src/lib.rs
new file mode 100644
index 0000000..7123243
--- /dev/null
+++ b/saddle-points/src/lib.rs
@@ -0,0 +1,16 @@
1fn is_min(mat: &[Vec<u64>], col: usize, row: usize) -> bool {
2 (0..mat.len()).all(|r| mat[r][col] >= mat[row][col])
3}
4
5pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
6 let mut res = vec![];
7 for (row,relem) in input.iter().enumerate() {
8 let m = relem.iter().max();
9 for (col,celem) in relem.iter().enumerate() {
10 if (m == Some(celem)) && is_min(input,col,row) {
11 res.push((row,col));
12 }
13 }
14 }
15 res
16}
diff --git a/saddle-points/tests/saddle-points.rs b/saddle-points/tests/saddle-points.rs
new file mode 100644
index 0000000..1fbc2de
--- /dev/null
+++ b/saddle-points/tests/saddle-points.rs
@@ -0,0 +1,98 @@
1use saddle_points;
2
3use saddle_points::find_saddle_points;
4
5// We don't care about order
6fn find_sorted_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
7 let mut result = saddle_points::find_saddle_points(input);
8 result.sort();
9 result
10}
11
12#[test]
13fn identify_single_saddle_point() {
14 let input = vec![vec![9, 8, 7], vec![5, 3, 2], vec![6, 6, 7]];
15 assert_eq!(vec![(1, 0)], find_saddle_points(&input));
16}
17
18#[test]
19fn identify_empty_matrix() {
20 let input = vec![vec![], vec![], vec![]];
21 let expected: Vec<(usize, usize)> = Vec::new();
22 assert_eq!(expected, find_saddle_points(&input));
23}
24
25#[test]
26fn identify_lack_of_saddle_point() {
27 let input = vec![vec![1, 2, 3], vec![3, 1, 2], vec![2, 3, 1]];
28 let expected: Vec<(usize, usize)> = Vec::new();
29 assert_eq!(expected, find_saddle_points(&input));
30}
31
32#[test]
33fn multiple_saddle_points_in_col() {
34 let input = vec![vec![4, 5, 4], vec![3, 5, 5], vec![1, 5, 4]];
35 assert_eq!(
36 vec![(0, 1), (1, 1), (2, 1)],
37 find_sorted_saddle_points(&input)
38 );
39}
40
41#[test]
42fn multiple_saddle_points_in_row() {
43 let input = vec![vec![6, 7, 8], vec![5, 5, 5], vec![7, 5, 6]];
44 assert_eq!(
45 vec![(1, 0), (1, 1), (1, 2)],
46 find_sorted_saddle_points(&input)
47 );
48}
49
50#[test]
51fn identify_bottom_right_saddle_point() {
52 let input = vec![vec![8, 7, 9], vec![6, 7, 6], vec![3, 2, 5]];
53 assert_eq!(vec![(2, 2)], find_saddle_points(&input));
54}
55
56// track specific as of v1.3
57#[test]
58fn non_square_matrix_high() {
59 let input = vec![vec![1, 5], vec![3, 6], vec![2, 7], vec![3, 8]];
60 assert_eq!(vec![(0, 1)], find_saddle_points(&input));
61}
62
63#[test]
64fn non_square_matrix_wide() {
65 let input = vec![vec![3, 1, 3], vec![3, 2, 4]];
66 assert_eq!(vec![(0, 0), (0, 2)], find_sorted_saddle_points(&input));
67}
68
69#[test]
70fn single_column_matrix() {
71 let input = vec![vec![2], vec![1], vec![4], vec![1]];
72 assert_eq!(vec![(1, 0), (3, 0)], find_sorted_saddle_points(&input));
73}
74
75#[test]
76fn single_row_matrix() {
77 let input = vec![vec![2, 5, 3, 5]];
78 assert_eq!(vec![(0, 1), (0, 3)], find_sorted_saddle_points(&input));
79}
80
81#[test]
82fn identify_all_saddle_points() {
83 let input = vec![vec![5, 5, 5], vec![5, 5, 5], vec![5, 5, 5]];
84 assert_eq!(
85 vec![
86 (0, 0),
87 (0, 1),
88 (0, 2),
89 (1, 0),
90 (1, 1),
91 (1, 2),
92 (2, 0),
93 (2, 1),
94 (2, 2)
95 ],
96 find_sorted_saddle_points(&input)
97 );
98}