Browse Source

initial commit

master
Jim Infield 3 years ago
commit
ea81e59710
  1. 1
      .gitignore
  2. 7
      Cargo.lock
  3. 8
      Cargo.toml
  4. 46
      src/main.rs

1
.gitignore

@ -0,0 +1 @@
/target

7
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"

8
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]

46
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<String> {
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<String> {
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::<Vec<String>>()
}
Loading…
Cancel
Save