Skip to content

Commit

Permalink
Sync sublist with problem-specifications (#1767)
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor authored Nov 14, 2023
1 parent 532fffe commit 253fcb6
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 70 deletions.
31 changes: 19 additions & 12 deletions exercises/practice/sublist/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
# 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.
Given any two lists `A` and `B`, determine if:

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.
- List `A` is equal to list `B`; or
- List `A` contains list `B` (`A` is a superlist of `B`); or
- List `A` is contained by list `B` (`A` is a sublist of `B`); or
- None of the above is true, thus lists `A` and `B` are unequal

Specifically, list `A` is equal to list `B` if both lists have the same values in the same order.
List `A` is a superlist of `B` if `A` contains a sub-sequence of values equal to `B`.
List `A` is a sublist of `B` if `B` contains a sub-sequence of values 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
- If `A = []` and `B = []` (both lists are empty), then `A` and `B` are equal
- If `A = [1, 2, 3]` and `B = []`, then `A` is a superlist of `B`
- If `A = []` and `B = [1, 2, 3]`, then `A` is a sublist of `B`
- If `A = [1, 2, 3]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B`
- If `A = [3, 4, 5]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B`
- If `A = [3, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B`
- If `A = [1, 2, 3]` and `B = [1, 2, 3]`, then `A` and `B` are equal
- If `A = [1, 2, 3, 4, 5]` and `B = [2, 3, 4]`, then `A` is a superlist of `B`
- If `A = [1, 2, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` and `B` are unequal
- If `A = [1, 2, 3]` and `B = [1, 3, 2]`, then `A` and `B` are unequal
22 changes: 22 additions & 0 deletions exercises/practice/sublist/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use sublist::Comparison;
{% for test in cases %}
#[test]
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let list_one: &[i32] = &{{ test.input.listOne | json_encode() }};
let list_two: &[i32] = &{{ test.input.listTwo | json_encode() }};
let output = {{ crate_name }}::{{ fn_names[0] }}(list_one, list_two);
let expected = {% if test.expected == "equal" -%}
Comparison::Equal
{%- elif test.expected == "sublist" -%}
Comparison::Sublist
{%- elif test.expected == "superlist" -%}
Comparison::Superlist
{%- else -%}
Comparison::Unequal
{%- endif %};
assert_eq!(output, expected);
}
{% endfor -%}
61 changes: 58 additions & 3 deletions exercises/practice/sublist/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,64 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[97319c93-ebc5-47ab-a022-02a1980e1d29]
description = "empty lists"

[de27dbd4-df52-46fe-a336-30be58457382]
description = "empty list within non empty list"

[5487cfd1-bc7d-429f-ac6f-1177b857d4fb]
description = "non empty list contains empty list"

[1f390b47-f6b2-4a93-bc23-858ba5dda9a6]
description = "list equals itself"

[7ed2bfb2-922b-4363-ae75-f3a05e8274f5]
description = "different lists"

[3b8a2568-6144-4f06-b0a1-9d266b365341]
description = "false start"

[dc39ed58-6311-4814-be30-05a64bc8d9b1]
description = "consecutive"

[d1270dab-a1ce-41aa-b29d-b3257241ac26]
description = "sublist at start"

[81f3d3f7-4f25-4ada-bcdc-897c403de1b6]
description = "sublist in middle"

[43bcae1e-a9cf-470e-923e-0946e04d8fdd]
description = "sublist at end"

[76cf99ed-0ff0-4b00-94af-4dfb43fe5caa]
description = "at start of superlist"

[b83989ec-8bdf-4655-95aa-9f38f3e357fd]
description = "in middle of superlist"

[26f9f7c3-6cf6-4610-984a-662f71f8689b]
description = "at end of superlist"

[0a6db763-3588-416a-8f47-76b1cedde31e]
description = "first list missing element from second list"

[83ffe6d8-a445-4a3c-8795-1e51a95e65c3]
description = "second list missing element from first list"

[7bc76cb8-5003-49ca-bc47-cdfbe6c2bb89]
description = "first list missing additional digits from second list"

[0d7ee7c1-0347-45c8-9ef5-b88db152b30b]
description = "order matters to a list"

[5f47ce86-944e-40f9-9f31-6368aad70aa6]
description = "same digits but different numbers"
163 changes: 108 additions & 55 deletions exercises/practice/sublist/tests/sublist.rs
Original file line number Diff line number Diff line change
@@ -1,127 +1,180 @@
use sublist::{sublist, Comparison};
use sublist::Comparison;

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

assert_eq!(Comparison::Equal, sublist(v, v));
fn empty_lists() {
let list_one: &[i32] = &[];
let list_two: &[i32] = &[];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Equal;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn empty_is_a_sublist_of_anything() {
assert_eq!(Comparison::Sublist, sublist(&[], &['a', 's', 'd', 'f']));
fn empty_list_within_non_empty_list() {
let list_one: &[i32] = &[];
let list_two: &[i32] = &[1, 2, 3];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Sublist;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn anything_is_a_superlist_of_empty() {
assert_eq!(Comparison::Superlist, sublist(&['a', 's', 'd', 'f'], &[]));
fn non_empty_list_contains_empty_list() {
let list_one: &[i32] = &[1, 2, 3];
let list_two: &[i32] = &[];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Superlist;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn one_is_not_two() {
assert_eq!(Comparison::Unequal, sublist(&[1], &[2]));
fn list_equals_itself() {
let list_one: &[i32] = &[1, 2, 3];
let list_two: &[i32] = &[1, 2, 3];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Equal;
assert_eq!(output, expected);
}

#[test]
#[ignore]
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));
fn different_lists() {
let list_one: &[i32] = &[1, 2, 3];
let list_two: &[i32] = &[2, 3, 4];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Unequal;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn sublist_at_start() {
assert_eq!(Comparison::Sublist, sublist(&[1, 2, 3], &[1, 2, 3, 4, 5]));
fn false_start() {
let list_one: &[i32] = &[1, 2, 5];
let list_two: &[i32] = &[0, 1, 2, 3, 1, 2, 5, 6];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Sublist;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn sublist_in_middle() {
assert_eq!(Comparison::Sublist, sublist(&[4, 3, 2], &[5, 4, 3, 2, 1]));
fn consecutive() {
let list_one: &[i32] = &[1, 1, 2];
let list_two: &[i32] = &[0, 1, 1, 1, 2, 1, 2];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Sublist;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn sublist_at_end() {
assert_eq!(Comparison::Sublist, sublist(&[3, 4, 5], &[1, 2, 3, 4, 5]));
fn sublist_at_start() {
let list_one: &[i32] = &[0, 1, 2];
let list_two: &[i32] = &[0, 1, 2, 3, 4, 5];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Sublist;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn partially_matching_sublist_at_start() {
assert_eq!(Comparison::Sublist, sublist(&[1, 1, 2], &[1, 1, 1, 2]));
fn sublist_in_middle() {
let list_one: &[i32] = &[2, 3, 4];
let list_two: &[i32] = &[0, 1, 2, 3, 4, 5];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Sublist;
assert_eq!(output, expected);
}

#[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));
fn sublist_at_end() {
let list_one: &[i32] = &[3, 4, 5];
let list_two: &[i32] = &[0, 1, 2, 3, 4, 5];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Sublist;
assert_eq!(output, expected);
}

#[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));
fn at_start_of_superlist() {
let list_one: &[i32] = &[0, 1, 2, 3, 4, 5];
let list_two: &[i32] = &[0, 1, 2];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Superlist;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn superlist_at_start() {
assert_eq!(Comparison::Superlist, sublist(&[1, 2, 3, 4, 5], &[1, 2, 3]));
fn in_middle_of_superlist() {
let list_one: &[i32] = &[0, 1, 2, 3, 4, 5];
let list_two: &[i32] = &[2, 3];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Superlist;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn superlist_in_middle() {
assert_eq!(Comparison::Superlist, sublist(&[5, 4, 3, 2, 1], &[4, 3, 2]));
fn at_end_of_superlist() {
let list_one: &[i32] = &[0, 1, 2, 3, 4, 5];
let list_two: &[i32] = &[3, 4, 5];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Superlist;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn superlist_at_end() {
assert_eq!(Comparison::Superlist, sublist(&[1, 2, 3, 4, 5], &[3, 4, 5]));
fn first_list_missing_element_from_second_list() {
let list_one: &[i32] = &[1, 3];
let list_two: &[i32] = &[1, 2, 3];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Unequal;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn second_list_missing_element_from_first_list() {
assert_eq!(Comparison::Unequal, sublist(&[1, 2, 3], &[1, 3]));
let list_one: &[i32] = &[1, 2, 3];
let list_two: &[i32] = &[1, 3];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Unequal;
assert_eq!(output, expected);
}

#[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]));
fn first_list_missing_additional_digits_from_second_list() {
let list_one: &[i32] = &[1, 2];
let list_two: &[i32] = &[1, 22];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Unequal;
assert_eq!(output, expected);
}

#[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])
);
fn order_matters_to_a_list() {
let list_one: &[i32] = &[1, 2, 3];
let list_two: &[i32] = &[3, 2, 1];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Unequal;
assert_eq!(output, expected);
}

#[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])
);
fn same_digits_but_different_numbers() {
let list_one: &[i32] = &[1, 0, 1];
let list_two: &[i32] = &[10, 1];
let output = sublist::sublist(list_one, list_two);
let expected = Comparison::Unequal;
assert_eq!(output, expected);
}

0 comments on commit 253fcb6

Please sign in to comment.