ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# n0mn0m's solution

## to Sublist in the Rust Track

Published at Oct 28 2020 · 0 comments
Instructions
Test suite
Solution

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

## Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

## Writing the Code

Execute the tests with:

``````\$ 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 all ignored tests without editing the tests source file, use:

``````\$ cargo test -- --ignored
``````

To run a specific test, for example `some_test`, you can use:

``````\$ cargo test some_test
``````

If the specific test is ignored use:

``````\$ cargo test some_test -- --ignored
``````

## Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

``````cargo fmt
``````

To see, if your solution contains some common ineffective use cases, inside the solution directory use

``````cargo clippy --all-targets
``````

## 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 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!

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.

### sublist.rs

``````use sublist::{sublist, Comparison};

#[test]
fn empty_equals_empty() {
let v: &[u32] = &[];

assert_eq!(Comparison::Equal, sublist(&v, &v));
}

#[test]
#[ignore]
fn test_empty_is_a_sublist_of_anything() {
assert_eq!(Comparison::Sublist, sublist(&[], &['a', 's', 'd', 'f']));
}

#[test]
#[ignore]
fn test_anything_is_a_superlist_of_empty() {
assert_eq!(Comparison::Superlist, sublist(&['a', 's', 'd', 'f'], &[]));
}

#[test]
#[ignore]
fn test_1_is_not_2() {
assert_eq!(Comparison::Unequal, sublist(&[1], &[2]));
}

#[test]
#[ignore]
fn test_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]
#[ignore]
fn test_sublist_at_start() {
assert_eq!(Comparison::Sublist, sublist(&[1, 2, 3], &[1, 2, 3, 4, 5]));
}

#[test]
#[ignore]
fn sublist_in_middle() {
assert_eq!(Comparison::Sublist, sublist(&[4, 3, 2], &[5, 4, 3, 2, 1]));
}

#[test]
#[ignore]
fn sublist_at_end() {
assert_eq!(Comparison::Sublist, sublist(&[3, 4, 5], &[1, 2, 3, 4, 5]));
}

#[test]
#[ignore]
fn partially_matching_sublist_at_start() {
assert_eq!(Comparison::Sublist, sublist(&[1, 1, 2], &[1, 1, 1, 2]));
}

#[test]
#[ignore]
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]
#[ignore]
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]
#[ignore]
fn superlist_at_start() {
assert_eq!(Comparison::Superlist, sublist(&[1, 2, 3, 4, 5], &[1, 2, 3]));
}

#[test]
#[ignore]
fn superlist_in_middle() {
assert_eq!(Comparison::Superlist, sublist(&[5, 4, 3, 2, 1], &[4, 3, 2]));
}

#[test]
#[ignore]
fn superlist_at_end() {
assert_eq!(Comparison::Superlist, sublist(&[1, 2, 3, 4, 5], &[3, 4, 5]));
}

#[test]
#[ignore]
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]
#[ignore]
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]
#[ignore]
fn recurring_values_unequal() {
assert_eq!(
Comparison::Unequal,
sublist(&[1, 2, 1, 2, 3], &[1, 2, 3, 1, 2, 3, 2, 3, 2, 1])
);
}``````
``````#[derive(Debug, PartialEq)]
pub enum Comparison {
Equal,
Sublist,
Superlist,
Unequal,
}

pub fn sublist<T: PartialEq>(_first_list: &[T], _second_list: &[T]) -> Comparison {
match (_first_list.len(), _second_list.len()) {
_ if _first_list == _second_list => Comparison::Equal,
(a, b) if a < b && contains(_first_list, _second_list) => Comparison::Sublist,
(a, b) if a > b && contains(_second_list, _first_list) => Comparison::Superlist,
_ => Comparison::Unequal,
}
}

pub fn contains<T: PartialEq>(x: &[T], y: &[T]) -> bool {
x.is_empty() || y.windows(x.len()).any(|_window| _window == x)
}``````