Решение на Search от Недялко Андреев

Обратно към всички решения

Към профила на Недялко Андреев

Резултати

  • 15 точки от тестове
  • 3 бонус точки
  • 18 точки общо
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::str::CharIndices;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
struct WordIterator<'a> {
text: &'a str,
iter: CharIndices<'a>,
word_start: Option<usize>,
}
impl<'a> WordIterator<'a> {
pub fn new(text: &'a str) -> Self {
WordIterator {
text: text,
iter: text.char_indices(),
word_start: None,
}
}
}
// This probably would have been much nicer if std::str::pattern::Pattern was stable...
impl<'a> Iterator for WordIterator<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
loop {
match (self.word_start, self.iter.next()) {
(None, Some((pos, c))) => {
if c.is_alphabetic() {
self.word_start = Some(pos);
}
}
(Some(pos), None) => {
self.word_start = None;
return Some(&self.text[pos..]);
}
(Some(start_pos), Some((end_pos, c))) => {
if !c.is_alphabetic() {
self.word_start = None;
return Some(&self.text[start_pos..end_pos]);
}
}
(None, None) => return None,
}
}
}
}
pub fn extract_words(text: &str) -> Vec<String> {
WordIterator::new(text).map(String::from).collect()
}
pub struct TextIndex {
index: HashMap<String, HashSet<Rc<String>>>,
}
impl TextIndex {
pub fn new() -> Self {
TextIndex {
index: HashMap::new(),
}
}
pub fn push(&mut self, text: &str) {
let rc_text = Rc::new(String::from(text));
for word in extract_words(text) {
let mut textset = self.index.entry(word).or_insert(HashSet::new());
textset.insert(rc_text.clone());
}
}
pub fn search(&self, query: &str) -> HashSet<&str> {
let mut result = HashSet::new();
for word in extract_words(query) {
if let Some(textset) = self.index.get(&word) {
for text in textset {
result.insert(text.as_ref().as_ref());
}
}
}
return result;
}
}

Лог от изпълнението

Compiling solution v0.1.0 (file:///tmp/d20180105-6053-in821p/solution)
warning: variable does not need to be mutable
  --> src/lib.rs:69:17
   |
69 |             let mut textset = self.index.entry(word).or_insert(HashSet::new());
   |                 ---^^^^^^^^
   |                 |
   |                 help: remove this `mut`
   |
   = note: #[warn(unused_mut)] on by default

warning: variable does not need to be mutable
  --> src/lib.rs:69:17
   |
69 |             let mut textset = self.index.entry(word).or_insert(HashSet::new());
   |                 ---^^^^^^^^
   |                 |
   |                 help: remove this `mut`
   |
   = note: #[warn(unused_mut)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 7.98 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

История (1 версия и 1 коментар)