diff options
| author | Federico Igne <git@federicoigne.com> | 2020-12-26 17:48:38 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch) | |
| tree | 8e39798fcaf27931d91c2088423fd4e97adcfc2d /saddle-points | |
| parent | 4e2052c4d792540c2f742b2c2a081b11117ed41d (diff) | |
| download | exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip | |
Move Rust exercises in a subdirectory
Diffstat (limited to 'saddle-points')
| -rw-r--r-- | saddle-points/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | saddle-points/.gitignore | 8 | ||||
| -rw-r--r-- | saddle-points/Cargo.toml | 6 | ||||
| -rw-r--r-- | saddle-points/README.md | 132 | ||||
| -rw-r--r-- | saddle-points/src/lib.rs | 16 | ||||
| -rw-r--r-- | saddle-points/tests/saddle-points.rs | 98 |
6 files changed, 0 insertions, 261 deletions
diff --git a/saddle-points/.exercism/metadata.json b/saddle-points/.exercism/metadata.json deleted file mode 100644 index cb44aa3..0000000 --- a/saddle-points/.exercism/metadata.json +++ /dev/null | |||
| @@ -1 +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 deleted file mode 100644 index db7f315..0000000 --- a/saddle-points/.gitignore +++ /dev/null | |||
| @@ -1,8 +0,0 @@ | |||
| 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 | ||
diff --git a/saddle-points/Cargo.toml b/saddle-points/Cargo.toml deleted file mode 100644 index 5f262e2..0000000 --- a/saddle-points/Cargo.toml +++ /dev/null | |||
| @@ -1,6 +0,0 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2018" | ||
| 3 | name = "saddle-points" | ||
| 4 | version = "1.3.0" | ||
| 5 | |||
| 6 | [dependencies] | ||
diff --git a/saddle-points/README.md b/saddle-points/README.md deleted file mode 100644 index 84647d0..0000000 --- a/saddle-points/README.md +++ /dev/null | |||
| @@ -1,132 +0,0 @@ | |||
| 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 | 1 2 3 | ||
| 9 | |--------- | ||
| 10 | 1 | 9 8 7 | ||
| 11 | 2 | 5 3 2 <--- saddle point at column 1, row 2, with value 5 | ||
| 12 | 3 | 6 6 7 | ||
| 13 | ``` | ||
| 14 | |||
| 15 | It has a saddle point at column 1, row 2. | ||
| 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 | The matrix can have a different number of rows and columns (Non square). | ||
| 27 | |||
| 28 | Note that you may find other definitions of matrix saddle points online, | ||
| 29 | but the tests for this exercise follow the above unambiguous definition. | ||
| 30 | |||
| 31 | ## Rust Indices Start At 0 | ||
| 32 | |||
| 33 | By 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 | ||
| 35 | exercise description in this README says, such as references to indices that | ||
| 36 | start at 1, so you will have to subtract 1 to translate those index numbers | ||
| 37 | to Rust index numbers. | ||
| 38 | |||
| 39 | ## Efficiency Notice | ||
| 40 | |||
| 41 | This exercise uses a _vector of vectors_ to store the content of matrices. While | ||
| 42 | this exercise is designed to help students understand basic concepts about | ||
| 43 | vectors, such as indexing, and that nested data types are legal, _vector of | ||
| 44 | vectors_ is a suboptimal choice for high-performance matrix algebra and any | ||
| 45 | similar efficient processing of larger amounts of data. | ||
| 46 | |||
| 47 | The detailed explanation of this inefficiency is beyond the scope of this | ||
| 48 | exercise 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) | ||
| 50 | and you can find a good introduction to it by clicking that link if you'd like | ||
| 51 | to learn more about details of a modern computer architecture. | ||
| 52 | |||
| 53 | |||
| 54 | ## Rust Installation | ||
| 55 | |||
| 56 | Refer to the [exercism help page][help-page] for Rust installation and learning | ||
| 57 | resources. | ||
| 58 | |||
| 59 | ## Writing the Code | ||
| 60 | |||
| 61 | Execute the tests with: | ||
| 62 | |||
| 63 | ```bash | ||
| 64 | $ cargo test | ||
| 65 | ``` | ||
| 66 | |||
| 67 | All but the first test have been ignored. After you get the first test to | ||
| 68 | pass, open the tests source file which is located in the `tests` directory | ||
| 69 | and remove the `#[ignore]` flag from the next test and get the tests to pass | ||
| 70 | again. Each separate test is a function with `#[test]` flag above it. | ||
| 71 | Continue, until you pass every test. | ||
| 72 | |||
| 73 | If you wish to run all ignored tests without editing the tests source file, use: | ||
| 74 | |||
| 75 | ```bash | ||
| 76 | $ cargo test -- --ignored | ||
| 77 | ``` | ||
| 78 | |||
| 79 | To run a specific test, for example `some_test`, you can use: | ||
| 80 | |||
| 81 | ```bash | ||
| 82 | $ cargo test some_test | ||
| 83 | ``` | ||
| 84 | |||
| 85 | If the specific test is ignored use: | ||
| 86 | |||
| 87 | ```bash | ||
| 88 | $ cargo test some_test -- --ignored | ||
| 89 | ``` | ||
| 90 | |||
| 91 | To learn more about Rust tests refer to the [online test documentation][rust-tests] | ||
| 92 | |||
| 93 | Make sure to read the [Modules][modules] chapter if you | ||
| 94 | haven't already, it will help you with organizing your files. | ||
| 95 | |||
| 96 | ## Further improvements | ||
| 97 | |||
| 98 | After 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 | |||
| 100 | To format your solution, inside the solution directory use | ||
| 101 | |||
| 102 | ```bash | ||
| 103 | cargo fmt | ||
| 104 | ``` | ||
| 105 | |||
| 106 | To see, if your solution contains some common ineffective use cases, inside the solution directory use | ||
| 107 | |||
| 108 | ```bash | ||
| 109 | cargo clippy --all-targets | ||
| 110 | ``` | ||
| 111 | |||
| 112 | ## Submitting the solution | ||
| 113 | |||
| 114 | Generally 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 | |||
| 118 | 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 are happy to help! | ||
| 119 | |||
| 120 | 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). | ||
| 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 | |||
| 129 | 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) | ||
| 130 | |||
| 131 | ## Submitting Incomplete Solutions | ||
| 132 | It'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 deleted file mode 100644 index 7123243..0000000 --- a/saddle-points/src/lib.rs +++ /dev/null | |||
| @@ -1,16 +0,0 @@ | |||
| 1 | fn is_min(mat: &[Vec<u64>], col: usize, row: usize) -> bool { | ||
| 2 | (0..mat.len()).all(|r| mat[r][col] >= mat[row][col]) | ||
| 3 | } | ||
| 4 | |||
| 5 | pub 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 deleted file mode 100644 index 1fbc2de..0000000 --- a/saddle-points/tests/saddle-points.rs +++ /dev/null | |||
| @@ -1,98 +0,0 @@ | |||
| 1 | use saddle_points; | ||
| 2 | |||
| 3 | use saddle_points::find_saddle_points; | ||
| 4 | |||
| 5 | // We don't care about order | ||
| 6 | fn 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] | ||
| 13 | fn 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] | ||
| 19 | fn 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] | ||
| 26 | fn 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] | ||
| 33 | fn 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] | ||
| 42 | fn 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] | ||
| 51 | fn 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] | ||
| 58 | fn 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] | ||
| 64 | fn 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] | ||
| 70 | fn 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] | ||
| 76 | fn 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] | ||
| 82 | fn 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 | } | ||
