diff options
| author | Federico Igne <git@federicoigne.com> | 2022-07-13 11:31:40 +0100 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2022-07-13 11:31:40 +0100 |
| commit | 17bf45b273913a24a72caa964ee16a4fe84c6ec6 (patch) | |
| tree | 32346bc91982a1f14eaee0f5ff80bd6043d9dcee /rust/sublist/src/lib.rs | |
| parent | 438d66b1783918c18d1df76c419e31cc98f5fb85 (diff) | |
| download | exercism-17bf45b273913a24a72caa964ee16a4fe84c6ec6.tar.gz exercism-17bf45b273913a24a72caa964ee16a4fe84c6ec6.zip | |
[rust] Sublist
Diffstat (limited to 'rust/sublist/src/lib.rs')
| -rw-r--r-- | rust/sublist/src/lib.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/rust/sublist/src/lib.rs b/rust/sublist/src/lib.rs new file mode 100644 index 0000000..cc62362 --- /dev/null +++ b/rust/sublist/src/lib.rs | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #[derive(Debug, PartialEq, Eq)] | ||
| 2 | pub enum Comparison { | ||
| 3 | Equal, | ||
| 4 | Sublist, | ||
| 5 | Superlist, | ||
| 6 | Unequal, | ||
| 7 | } | ||
| 8 | |||
| 9 | pub fn sublist<T: PartialEq>(fst: &[T], snd: &[T]) -> Comparison { | ||
| 10 | match (fst.len(),snd.len()) { | ||
| 11 | (0,0) => Comparison::Equal, | ||
| 12 | (0,_) => Comparison::Sublist, | ||
| 13 | (_,0) => Comparison::Superlist, | ||
| 14 | (n,m) if n < m && snd.windows(n).any(|s| fst == s) => Comparison::Sublist, | ||
| 15 | (n,m) if n > m && fst.windows(m).any(|f| snd == f) => Comparison::Superlist, | ||
| 16 | _ if fst == snd => Comparison::Equal, | ||
| 17 | _ => Comparison::Unequal | ||
| 18 | } | ||
| 19 | } | ||
