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

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

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

Резултати

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

Код

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
pub fn extract_words(text: &str) -> Vec<String> {
let mut temp = String::new();
let mut container = Vec::new();
for c in text.chars(){
if c.is_alphabetic(){
temp.push(c);
}
else if !c.is_alphabetic() && !temp.is_empty(){
container.push(temp);
temp = String::new();
}
}
if !temp.is_empty(){
container.push(temp);
}
return container;
}
use std::collections::HashSet;
pub struct TextIndex {
pub words : HashSet<String>,
}
impl TextIndex {
pub fn new() -> Self {
Self{
words : HashSet::new(),
}
}
pub fn push(&mut self, text: &str) {
self.words.insert(text.to_string());
}
pub fn search(&self, query: &str) -> HashSet<&str> {
let query_words = extract_words(query);
let mut res = HashSet::new();
for i in query_words{
for j in &self.words{
let temp = extract_words(j);
for k in temp{
if i == k{
let s_slice: &str = &*j;
res.insert(s_slice);
}
}
}
}
return res;
}
}

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

Compiling solution v0.1.0 (file:///tmp/d20180105-6053-pqgbnu/solution)
    Finished dev [unoptimized + debuginfo] target(s) in 6.92 secs
     Running target/debug/deps/solution-3f98bfa5c86a5dd9

running 1 test
test tests::it_works ... ok

test result: ok. 1 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

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

Стефан качи първо решение на 02.01.2018 00:10 (преди над 7 години)

Стефан качи решение на 02.01.2018 21:11 (преди над 7 години)

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
pub fn extract_words(text: &str) -> Vec<String> {
let mut temp = String::new();
let mut container = Vec::new();
for c in text.chars(){
if c.is_alphabetic(){
temp.push(c);
}
else if !c.is_alphabetic() && !temp.is_empty(){
container.push(temp);
temp = String::new();
}
}
- container.push(temp);
+ if !temp.is_empty(){
+ container.push(temp);
+ }
return container;
}
use std::collections::HashSet;
pub struct TextIndex {
pub words : HashSet<String>,
}
impl TextIndex {
pub fn new() -> Self {
Self{
words : HashSet::new(),
}
}
pub fn push(&mut self, text: &str) {
self.words.insert(text.to_string());
}
pub fn search(&self, query: &str) -> HashSet<&str> {
let query_words = extract_words(query);
let mut res = HashSet::new();
for i in query_words{
for j in &self.words{
let temp = extract_words(j);
for k in temp{
if i == k{
let s_slice: &str = &*j;
res.insert(s_slice);
}
}
}
}
return res;
}
}