From 2e255e2c04acbf9c14082cb8b2bc73b806d96de9 Mon Sep 17 00:00:00 2001 From: Avery Winters Date: Wed, 1 Nov 2023 15:16:46 -0500 Subject: [PATCH] Solve rust/sublist --- rust/sublist/.exercism/config.json | 37 +++++++++ rust/sublist/.exercism/metadata.json | 1 + rust/sublist/.gitignore | 8 ++ rust/sublist/Cargo.toml | 4 + rust/sublist/HELP.md | 86 +++++++++++++++++++++ rust/sublist/README.md | 49 ++++++++++++ rust/sublist/src/lib.rs | 28 +++++++ rust/sublist/tests/sublist.rs | 110 +++++++++++++++++++++++++++ 8 files changed, 323 insertions(+) create mode 100644 rust/sublist/.exercism/config.json create mode 100644 rust/sublist/.exercism/metadata.json create mode 100644 rust/sublist/.gitignore create mode 100644 rust/sublist/Cargo.toml create mode 100644 rust/sublist/HELP.md create mode 100644 rust/sublist/README.md create mode 100644 rust/sublist/src/lib.rs create mode 100644 rust/sublist/tests/sublist.rs diff --git a/rust/sublist/.exercism/config.json b/rust/sublist/.exercism/config.json new file mode 100644 index 0000000..fa42dec --- /dev/null +++ b/rust/sublist/.exercism/config.json @@ -0,0 +1,37 @@ +{ + "authors": [ + "EduardoBautista" + ], + "contributors": [ + "ashleygwilliams", + "coriolinus", + "cwhakes", + "eddyp", + "EduardoBautista", + "efx", + "ErikSchierboom", + "IanWhitney", + "kytrinyx", + "lutostag", + "mkantor", + "nfiles", + "petertseng", + "rofrol", + "stringparser", + "xakon", + "ZapAnton" + ], + "files": { + "solution": [ + "src/lib.rs", + "Cargo.toml" + ], + "test": [ + "tests/sublist.rs" + ], + "example": [ + ".meta/example.rs" + ] + }, + "blurb": "Write a function to determine if a list is a sublist of another list." +} diff --git a/rust/sublist/.exercism/metadata.json b/rust/sublist/.exercism/metadata.json new file mode 100644 index 0000000..2387468 --- /dev/null +++ b/rust/sublist/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"sublist","id":"c3b8d7480bf54327a772a073d46d3e7e","url":"https://exercism.org/tracks/rust/exercises/sublist","handle":"averywinters","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/sublist/.gitignore b/rust/sublist/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/rust/sublist/.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/rust/sublist/Cargo.toml b/rust/sublist/Cargo.toml new file mode 100644 index 0000000..6493981 --- /dev/null +++ b/rust/sublist/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2021" +name = "sublist" +version = "0.0.0" diff --git a/rust/sublist/HELP.md b/rust/sublist/HELP.md new file mode 100644 index 0000000..67add76 --- /dev/null +++ b/rust/sublist/HELP.md @@ -0,0 +1,86 @@ +# Help + +## Running the tests + +Execute the tests with: + +```bash +$ cargo test +``` + +All but the first test have been ignored. After you get the first test to +pass, open the tests source file which is located in the `tests` directory +and remove the `#[ignore]` flag from the next test and get the tests to pass +again. Each separate test is a function with `#[test]` flag above it. +Continue, until you pass every test. + +If you wish to run _only ignored_ tests without editing the tests source file, use: + +```bash +$ cargo test -- --ignored +``` + +If you are using Rust 1.51 or later, you can run _all_ tests with + +```bash +$ cargo test -- --include-ignored +``` + +To run a specific test, for example `some_test`, you can use: + +```bash +$ cargo test some_test +``` + +If the specific test is ignored, use: + +```bash +$ cargo test some_test -- --ignored +``` + +To learn more about Rust tests refer to the online [test documentation][rust-tests]. + +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Submitting your solution + +You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Rust track's documentation](https://exercism.org/docs/tracks/rust) +- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Submitting the solution + +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. + +## Feedback, Issues, Pull Requests + +The GitHub [track repository][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! + +If you want to know more about Exercism, take a look at the [contribution guide]. + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + +[help-page]: https://exercism.org/tracks/rust/learning +[github]: https://github.com/exercism/rust +[contribution guide]: https://exercism.org/docs/community/contributors \ No newline at end of file diff --git a/rust/sublist/README.md b/rust/sublist/README.md new file mode 100644 index 0000000..2c905a4 --- /dev/null +++ b/rust/sublist/README.md @@ -0,0 +1,49 @@ +# Sublist + +Welcome to Sublist on Exercism's Rust Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Given two lists determine if the first list is contained within the second +list, if the second list is contained within the first list, if both lists are +contained within each other or if none of these are true. + +Specifically, a list A is a sublist of list B if by dropping 0 or more elements +from the front of B and 0 or more elements from the back of B you get a list +that's completely equal to A. + +Examples: + + * A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B + * A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B + * A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B + * A = [1, 2, 3], B = [1, 2, 3], A is equal to B + * A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B + * A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B + +## Source + +### Created by + +- @EduardoBautista + +### Contributed to by + +- @ashleygwilliams +- @coriolinus +- @cwhakes +- @eddyp +- @EduardoBautista +- @efx +- @ErikSchierboom +- @IanWhitney +- @kytrinyx +- @lutostag +- @mkantor +- @nfiles +- @petertseng +- @rofrol +- @stringparser +- @xakon +- @ZapAnton \ No newline at end of file diff --git a/rust/sublist/src/lib.rs b/rust/sublist/src/lib.rs new file mode 100644 index 0000000..ce403af --- /dev/null +++ b/rust/sublist/src/lib.rs @@ -0,0 +1,28 @@ +#[derive(Debug, PartialEq, Eq)] +pub enum Comparison { + Equal, + Sublist, + Superlist, + Unequal, +} + +pub fn sublist(first: &[T], second: &[T]) -> Comparison { + let is_sublist = |first: &[_], second: &[_]| { + second + .iter() + .enumerate() + .map(|(i, _)| &second[i..]) + .take_while(|suffix| first.len() <= suffix.len()) + .map(|suffix| &suffix[..first.len()]) + .any(|sublist| first == sublist) + }; + if first == second { + Comparison::Equal + } else if is_sublist(first, second) { + Comparison::Sublist + } else if is_sublist(second, first) { + Comparison::Superlist + } else { + Comparison::Unequal + } +} diff --git a/rust/sublist/tests/sublist.rs b/rust/sublist/tests/sublist.rs new file mode 100644 index 0000000..1fd99f3 --- /dev/null +++ b/rust/sublist/tests/sublist.rs @@ -0,0 +1,110 @@ +use sublist::{sublist, Comparison}; + +#[test] +fn empty_equals_empty() { + let v: &[u32] = &[]; + + assert_eq!(Comparison::Equal, sublist(v, v)); +} + +#[test] +fn empty_is_a_sublist_of_anything() { + assert_eq!(Comparison::Sublist, sublist(&[], &['a', 's', 'd', 'f'])); +} + +#[test] +fn anything_is_a_superlist_of_empty() { + assert_eq!(Comparison::Superlist, sublist(&['a', 's', 'd', 'f'], &[])); +} + +#[test] +fn one_is_not_two() { + assert_eq!(Comparison::Unequal, sublist(&[1], &[2])); +} + +#[test] +fn compare_larger_equal_lists() { + use std::iter::repeat; + + let v: Vec = repeat('x').take(1000).collect(); + + assert_eq!(Comparison::Equal, sublist(&v, &v)); +} + +#[test] +fn sublist_at_start() { + assert_eq!(Comparison::Sublist, sublist(&[1, 2, 3], &[1, 2, 3, 4, 5])); +} + +#[test] +fn sublist_in_middle() { + assert_eq!(Comparison::Sublist, sublist(&[4, 3, 2], &[5, 4, 3, 2, 1])); +} + +#[test] +fn sublist_at_end() { + assert_eq!(Comparison::Sublist, sublist(&[3, 4, 5], &[1, 2, 3, 4, 5])); +} + +#[test] +fn partially_matching_sublist_at_start() { + assert_eq!(Comparison::Sublist, sublist(&[1, 1, 2], &[1, 1, 1, 2])); +} + +#[test] +fn sublist_early_in_huge_list() { + let huge: Vec = (1..1_000_000).collect(); + + assert_eq!(Comparison::Sublist, sublist(&[3, 4, 5], &huge)); +} + +#[test] +fn huge_sublist_not_in_huge_list() { + let v1: Vec = (10..1_000_001).collect(); + let v2: Vec = (1..1_000_000).collect(); + + assert_eq!(Comparison::Unequal, sublist(&v1, &v2)); +} + +#[test] +fn superlist_at_start() { + assert_eq!(Comparison::Superlist, sublist(&[1, 2, 3, 4, 5], &[1, 2, 3])); +} + +#[test] +fn superlist_in_middle() { + assert_eq!(Comparison::Superlist, sublist(&[5, 4, 3, 2, 1], &[4, 3, 2])); +} + +#[test] +fn superlist_at_end() { + assert_eq!(Comparison::Superlist, sublist(&[1, 2, 3, 4, 5], &[3, 4, 5])); +} + +#[test] +fn second_list_missing_element_from_first_list() { + assert_eq!(Comparison::Unequal, sublist(&[1, 2, 3], &[1, 3])); +} + +#[test] +fn superlist_early_in_huge_list() { + let huge: Vec = (1..1_000_000).collect(); + + assert_eq!(Comparison::Superlist, sublist(&huge, &[3, 4, 5])); +} + +#[test] +fn recurring_values_sublist() { + assert_eq!( + Comparison::Sublist, + sublist(&[1, 2, 1, 2, 3], &[1, 2, 3, 1, 2, 1, 2, 3, 2, 1]) + ); +} + +#[test] +fn recurring_values_unequal() { + assert_eq!( + Comparison::Unequal, + sublist(&[1, 2, 1, 2, 3], &[1, 2, 3, 1, 2, 3, 2, 3, 2, 1]) + ); +}