From ea81e59710e08b658f9207f78d05a5642b03c6bc Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Sun, 18 Jul 2021 11:09:09 -0500 Subject: [PATCH] initial commit --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 8 ++++++++ src/main.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7be9e92 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "scratch" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d2d4e21 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "scratch" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..fd5baa8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,46 @@ +#![allow(unused)] + +fn main() { + use std::collections::BTreeSet; + + let a1 = vec!["foo", "live", "", "strong"]; + let a2 = vec!["arp", "live", "strong"]; + let a3 = vec!["live", "arp", "foo", "strong", "arp"]; + let b1 = vec!["lively", "alive", "harp", "sharp", "armstrong"]; + + println!("\n"); + + print!("1: {:?}\t", in_array(&a1,&b1)); + println!("{:?}\n", in_array(&a3,&b1)); + + print!("2: {:?}\t", in_array2(&a1,&b1)); + println!("{:?}\n", in_array2(&a3,&b1)); + +} + +fn in_array(arr_a: &[&str], arr_b: &[&str]) -> Vec { + let mut arr: Vec<_> = arr_a.iter() + .filter(|e| !e.is_empty()) + .copied().collect(); + arr.sort(); + arr.dedup(); + + let mut res = vec![]; + for a in arr.iter() { + if arr_b.iter().any(|&b| b.contains(a)) { + res.push(a.to_string()); + } + } + res +} + +fn in_array2(arr_a: &[&str], arr_b: &[&str]) -> Vec { + use std::collections::BTreeSet; + let arr: BTreeSet<_> = arr_a.iter().copied().collect(); + + arr.iter() + .filter(|&a| !a.is_empty()) + .filter(|&a| arr_b.iter().any(|&b| b.contains(a))) + .map(|a| a.to_string()) + .collect::>() +} \ No newline at end of file