Решение на Search от Мартин Георгиев

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

Към профила на Мартин Георгиев

Резултати

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

Код

pub fn extract_words(text: &str) -> Vec<String> {
let mut words: Vec<String> = vec!();
let mut copy_text = String::from(text);
while true {
copy_text = copy_text.chars().skip_while(|ch| !ch.is_alphabetic()).collect();
if copy_text.len() == 0 {
break;
}
let word: String = copy_text.chars().take_while(|ch| ch.is_alphabetic()).collect();
words.push(String::from(word));
copy_text = copy_text.chars().skip_while(|ch| ch.is_alphabetic()).collect();
if copy_text.len() == 0 {
break;
}
}
return words;
}
use std::collections::HashSet;
use std::collections::HashMap;
pub struct TextIndex {
wordPhrases: HashMap<String, String>
}
impl TextIndex {
pub fn new() -> Self {
TextIndex {
wordPhrases: HashMap::new()
}
}
pub fn push(&mut self, text: &str) {
let words = extract_words(text);
for word in words {
self.wordPhrases.insert(word, String::from(text));
}
}
pub fn search(&self, query: &str) -> HashSet<&str> {
let mut resultSet: HashSet<&str> = HashSet::new();
let words = extract_words(query);
for word in words {
match self.wordPhrases.get(&word) {
Some(phrase) => resultSet.insert(phrase),
None => true
};
}
resultSet
}
}

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

Compiling solution v0.1.0 (file:///tmp/d20180105-6053-1vyl7eg/solution)
warning: denote infinite loops with `loop { ... }`
  --> src/lib.rs:5:5
   |
5  |       while true {
   |       ^---------
   |       |
   |  _____help: use `loop`
   | |
6  | |      copy_text = copy_text.chars().skip_while(|ch| !ch.is_alphabetic()).collect();
7  | |      if copy_text.len() == 0 {
8  | |       break;
...  |
15 | |      }
16 | |     }
   | |_____^
   |
   = note: #[warn(while_true)] on by default

warning: structure field `wordPhrases` should have a snake case name such as `word_phrases`
  --> src/lib.rs:25:5
   |
25 |     wordPhrases: HashMap<String, String>
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: #[warn(non_snake_case)] on by default

warning: variable `resultSet` should have a snake case name such as `result_set`
  --> src/lib.rs:44:13
   |
44 |         let mut resultSet: HashSet<&str> = HashSet::new();
   |             ^^^^^^^^^^^^^

warning: denote infinite loops with `loop { ... }`
  --> src/lib.rs:5:5
   |
5  |       while true {
   |       ^---------
   |       |
   |  _____help: use `loop`
   | |
6  | |      copy_text = copy_text.chars().skip_while(|ch| !ch.is_alphabetic()).collect();
7  | |      if copy_text.len() == 0 {
8  | |       break;
...  |
15 | |      }
16 | |     }
   | |_____^
   |
   = note: #[warn(while_true)] on by default

warning: structure field `wordPhrases` should have a snake case name such as `word_phrases`
  --> src/lib.rs:25:5
   |
25 |     wordPhrases: HashMap<String, String>
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: #[warn(non_snake_case)] on by default

warning: variable `resultSet` should have a snake case name such as `result_set`
  --> src/lib.rs:44:13
   |
44 |         let mut resultSet: HashSet<&str> = HashSet::new();
   |             ^^^^^^^^^^^^^

    Finished dev [unoptimized + debuginfo] target(s) in 4.86 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 ... FAILED
test solution_test::test_search_word ... FAILED

failures:

---- solution_test::test_search_special_cases stdout ----
	thread 'solution_test::test_search_special_cases' panicked at 'assertion failed: `(left == right)`
  left: `{"one, two", "two, three"}`,
 right: `{"two, three"}`', tests/solution_test.rs:71:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.

---- solution_test::test_search_word stdout ----
	thread 'solution_test::test_search_word' panicked at 'assertion failed: `(left == right)`
  left: `{"one/five/six", "one, two, three"}`,
 right: `{"one/five/six"}`', tests/solution_test.rs:49:4


failures:
    solution_test::test_search_special_cases
    solution_test::test_search_word

test result: FAILED. 3 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed, to rerun pass '--test solution_test'

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

Мартин качи първо решение на 04.01.2018 23:56 (преди над 7 години)

Направил си го с HashMap, но копираш нов низ за всяка дума, което е доста неефективно откъм памет :). Ако беше го обвил в Rc, щеше да получиш бонус точка. Oh, well.