|
@ -0,0 +1,54 @@ |
|
|
|
|
|
#![allow(unused)]
|
|
|
|
|
|
fn main() {
|
|
|
|
|
|
println!();
|
|
|
|
|
|
let args = vec![
|
|
|
|
|
|
(vec![1,2], vec![1]),
|
|
|
|
|
|
(vec![1,2,2], vec![2]),
|
|
|
|
|
|
(vec![1,2,2], vec![]),
|
|
|
|
|
|
(vec![], vec![1,2]),
|
|
|
|
|
|
(vec![1,2,3], vec![1,2]),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
for (arg1, arg2) in args {
|
|
|
|
|
|
println!("{:?} \tless {:?} \t leaves {:?}, {:?}, {:?}\n",
|
|
|
|
|
|
arg1.clone(),
|
|
|
|
|
|
arg2.clone(),
|
|
|
|
|
|
array_diff(arg1.clone(), arg2.clone()),
|
|
|
|
|
|
not_contains(arg1.clone(), arg2.clone()),
|
|
|
|
|
|
retains(arg1, arg2)
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
println!();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn array_diff<T: PartialEq + std::fmt::Display + std::fmt::Debug>(a: Vec<T>, b: Vec<T>) -> Vec<T> {
|
|
|
|
|
|
a.into_iter()
|
|
|
|
|
|
.filter(
|
|
|
|
|
|
|i| b.iter().all(|e| *e != *i))
|
|
|
|
|
|
.collect::<Vec<T>>()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn not_contains<T: PartialEq>(a: Vec<T>, b: Vec<T>) -> Vec<T> {
|
|
|
|
|
|
a.into_iter().filter(|x| !b.contains(x)).collect()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn retains<T: PartialEq>(a: Vec<T>, b: Vec<T>) -> Vec<T> {
|
|
|
|
|
|
let mut a = a;
|
|
|
|
|
|
a.retain(|x| !b.contains(x));
|
|
|
|
|
|
a
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn returns_expected() {
|
|
|
|
|
|
assert_eq!(array_diff(vec![1,2], vec![1]), vec![2]);
|
|
|
|
|
|
assert_eq!(array_diff(vec![1,2,2], vec![1]), vec![2,2]);
|
|
|
|
|
|
assert_eq!(array_diff(vec![1,2,2], vec![2]), vec![1]);
|
|
|
|
|
|
assert_eq!(array_diff(vec![1,2,2], vec![]), vec![1,2,2]);
|
|
|
|
|
|
assert_eq!(array_diff(vec![], vec![1,2]), vec![]);
|
|
|
|
|
|
assert_eq!(array_diff(vec![1,2,3], vec![1,2]), vec![3]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|