Решение на Search от Иван Иванов
Резултати
- 15 точки от тестове
- 0 бонус точки
- 15 точки общо
- 5 успешни тест(а)
- 0 неуспешни тест(а)
Код
// First version passing the tests
pub fn extract_words(text: &str) -> Vec<String> {
text
.split(|c: char| !c.is_alphabetic())
.map(|x: &str| String::from(x))
.filter(|x: &String| x != "")
.into_iter()
.collect::<Vec<String>>()
}
use std::collections::HashSet;
pub struct TextIndex {
search_index: HashSet<String>
}
impl TextIndex {
pub fn new() -> Self {
TextIndex{ search_index: HashSet::new() }
}
pub fn push(&mut self, text: &str) {
self.search_index.insert(String::from(text));
}
pub fn search(&self, query: &str) -> HashSet<&str> {
let mut result: HashSet<&str> = HashSet::new();
let words = extract_words(query);
for s in &self.search_index {
for w in &words {
if s.contains(w) {
result.insert(&s);
}
}
}
result
}
}
Лог от изпълнението
Compiling solution v0.1.0 (file:///tmp/d20180105-6053-1pjo6rs/solution) Finished dev [unoptimized + debuginfo] target(s) in 6.90 secs Running target/debug/deps/solution-3f98bfa5c86a5dd9 running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Running target/debug/deps/solution_test-3d9e4ea2eafbbc82 running 5 tests test solution_test::test_extract_words_basic ... ok test solution_test::test_extract_words_extra ... ok test solution_test::test_search_multiple_words ... ok test solution_test::test_search_special_cases ... ok test solution_test::test_search_word ... ok test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Doc-tests solution running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out