Solve rust/sublist

This commit is contained in:
Avery Winters 2023-11-01 15:16:46 -05:00
parent 59369bd05b
commit 2e255e2c04
Signed by: avery
SSH key fingerprint: SHA256:eesvLB5MMqHLZrAMFt6kEhqJWnASMLcET6Sgmw0FqZI
8 changed files with 323 additions and 0 deletions

View file

@ -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."
}

View file

@ -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}

8
rust/sublist/.gitignore vendored Normal file
View file

@ -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

4
rust/sublist/Cargo.toml Normal file
View file

@ -0,0 +1,4 @@
[package]
edition = "2021"
name = "sublist"
version = "0.0.0"

86
rust/sublist/HELP.md Normal file
View file

@ -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

49
rust/sublist/README.md Normal file
View file

@ -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

28
rust/sublist/src/lib.rs Normal file
View file

@ -0,0 +1,28 @@
#[derive(Debug, PartialEq, Eq)]
pub enum Comparison {
Equal,
Sublist,
Superlist,
Unequal,
}
pub fn sublist<T: PartialEq>(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
}
}

View file

@ -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<char> = 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<u32> = (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<u64> = (10..1_000_001).collect();
let v2: Vec<u64> = (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<u32> = (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])
);
}