Sublist
1. Readme
子列表
给定两个列表确定第一个列表是否包含在第二个列表中;第二个列表是否包含在第一个列表中;两个列表是否包含在彼此,或者这些都不是真的。
具体来说,列表 A 是列表 B 的子列表,在于是否 B 的前面删除 0 个或更多元素和 B 的后面删除 0 个或更多元素,则得到完全等于 A 的列表。
例子:
- A =
[1,2,3]
,B =[1,2,3,4,5]
,A 是 B 的子列表 - A =
[3,4,5]
,B =[1,2,3,4,5]
,A 是 B 的子列表 - A =
[3,4]
,B =[1,2,3,4,5]
,A 是 B 的子列表 - A =
[1,2,3]
,B =[1,2,3]
,A 等于 B。 - A =
[1,2,3,4,5]
,B =[2,3,4]
,A 是 B 的父列表 - A =
[1,2,4]
,B =[1,2,3,4,5]
,A 不是 B 的父列表,子列表,或等于 B 的列表
2. 开始你的表演
#[derive(Debug, PartialEq)] pub enum Comparison { Equal, Sublist, Superlist, Unequal, } pub fn sublist<T: PartialEq>(_first_list: &[T], _second_list: &[T]) -> Comparison { unimplemented!("Determine if the first list is equal to, sublist of, superlist of or unequal to the second list."); }
3. 测试代码查看
# #![allow(unused_variables)] #fn main() { #[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..1000000).collect(); assert_eq!(Comparison::Sublist, sublist(&[3, 4, 5], &huge)); } #[test] //#[ignore] fn huge_sublist_not_in_huge_list() { let v1: Vec<u64> = (10..1000001).collect(); let v2: Vec<u64> = (1..1000000).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..1000000).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]) ); } #}
4. 答案
# #![allow(unused_variables)] #fn main() { #[derive(PartialEq, Eq, Debug)] pub enum Comparison { Equal, Sublist, Superlist, Unequal, } pub fn sublist<T: PartialEq>(a: &[T], b: &[T]) -> Comparison { if a == b { Comparison::Equal } else if contains(a, b) { Comparison::Superlist } else if contains(b, a) { Comparison::Sublist } else { Comparison::Unequal } } fn contains<T: PartialEq>(a: &[T], b: &[T]) -> bool { if a.len() < b.len() { return false; } if a.starts_with(b) { return true; } contains(&a[1..], b) } #}