-
-
Save rust-play/6d697a29901d4b82cc8b184526d4b8f1 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code)] | |
fn all_postfixes_sorted(arr: &[&str]) -> Vec<String> { | |
let mut postfixes: std::collections::HashSet<String> = std::collections::HashSet::new(); | |
for &w in arr { | |
for start_idx in 0..(w.chars().count()) { | |
let slice: &str = &w[start_idx..]; | |
let w = String::from(slice); | |
postfixes.insert(w); | |
} | |
} | |
let mut r = postfixes.into_iter().collect::<Vec<String>>(); | |
r.sort(); | |
r | |
} | |
fn filter_array_by_contains(arr_a: &[&str], arr_b: &[&str]) -> Vec<String> { | |
let postfixes = all_postfixes_sorted(arr_b); | |
let pred = |x: &&str| -> Option<String> { | |
let idx = postfixes.partition_point(|y| *y < x.to_string()); | |
eprintln!("postfixes[idx] = '{:?} vs '{:?}' ", postfixes[idx], x); | |
if postfixes[idx].contains(x) { | |
Some(x.to_string()) | |
} else { | |
None | |
} | |
}; | |
arr_a | |
.iter() | |
.filter_map(pred) | |
.collect() | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn examples() { | |
assert_eq!(filter_array_by_contains( | |
&["xyz", "live", "strong"], | |
&["lively", "alive", "harp", "sharp", "armstrong"], | |
), ["live", "strong"]); | |
assert_eq!(filter_array_by_contains( | |
&["live", "strong", "arp"], | |
&["lively", "alive", "harp", "sharp", "armstrong"], | |
), ["live", "strong", "arp"]); | |
assert_eq!(filter_array_by_contains( | |
&["tarp", "mice", "bull"], | |
&["lively", "alive", "harp", "sharp", "armstrong"], | |
), [] as [&str; 0]); | |
assert_eq!(filter_array_by_contains( | |
&["live", "strong", "arp", "arp"], | |
&["lively", "alive", "harp", "sharp", "armstrong"], | |
), ["live", "strong", "arp", "arp"]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment