Решение на Text Info от Исмаил Алиджиков

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

Към профила на Исмаил Алиджиков

Резултати

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

Код

pub struct TextInfo {
text: String
}
impl TextInfo {
pub fn new(text: &str) -> Self {
TextInfo {
text: String::from(text),
}
}
pub fn char_count(&self) -> usize {
self.text.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
TextInfo::latin_letter_count(self) + TextInfo::cyrillic_letter_count(self)
}
pub fn latin_letter_count(&self) -> usize {
return self.text.chars().into_iter()
.filter(|&ch| TextInfo::is_latin_letter(ch))
.count();
}
pub fn cyrillic_letter_count(&self) -> usize {
return self.text.chars().into_iter()
.filter(|&ch| TextInfo::is_cyrillic_letter(ch))
.count();

Тук мисля, че би могъл да опростиш нещата, ако използваш matches метода на &str: https://doc.rust-lang.org/std/primitive.str.html#method.matches. Нещо, което да пробваш, ако искаш. Освен това мисля, че можеш да викнеш директно .filter(TextInfo::is_latin_letter), но не съм го пробвал -- опитай :).

}
pub fn word_count(&self) -> usize {
let mut word_count = 0;
let mut is_in_word = false;
for ch in self.text.chars() {
if ch.is_alphabetic() {
if !is_in_word {
word_count += 1;
is_in_word = true;
}
} else {
is_in_word = false;
}
}
word_count
}
pub fn sentence_count(&self) -> usize {
TextInfo::declarative_count(&self.text) +
TextInfo::question_count(&self.text) +
TextInfo::exclamatory_count(&self.text)
}
pub fn emotion(&self) -> String {
let declaratives = TextInfo::declarative_count(&self.text);
let questions = TextInfo::question_count(&self.text);
let exclamatories = TextInfo::exclamatory_count(&self.text);
if exclamatories > declaratives && exclamatories > questions {
String::from("😮")
} else if questions > declaratives && questions > exclamatories {
String::from("🤔")
} else {
String::from("😐")
}
}
fn is_latin_letter(ch: char) -> bool {
match ch {
'a'...'z' | 'A'...'Z' => true,
_ => false,
}
}
fn is_cyrillic_letter(ch: char) -> bool {
match ch {
'а' ... 'я' | 'А' ... 'Я' => true,
_ => false,
}
}
fn declarative_count(text: &String) -> usize {
TextInfo::sentence_count_by_punctuation(text, '.')
}
fn question_count(text: &String) -> usize {
TextInfo::sentence_count_by_punctuation(text, '?')
}
fn exclamatory_count(text: &String) -> usize {
TextInfo::sentence_count_by_punctuation(text, '!')
}
fn sentence_count_by_punctuation(text: &String, punctuation: char) -> usize {
let mut sentence_count = 0;
for (i, ch) in text.chars().enumerate() {
if ch == punctuation && i != 0 {
let previous = text.chars().nth(i - 1).unwrap();
if !TextInfo::is_sentence_punctuation(previous) && !previous.is_whitespace() {
sentence_count += 1;
}
}
}
sentence_count
}
fn is_sentence_punctuation(ch: char) -> bool {
ch == '.' || ch == '?' || ch == '!'
}
}

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

Compiling solution v0.1.0 (file:///tmp/d20171026-5817-sknuz0/solution)
invalid expression
!1248 = !DIExpression(6, 34, 0, 6)
invalid expression
!1249 = !DIExpression(6, 34, 0, 6)
    Finished dev [unoptimized + debuginfo] target(s) in 3.39 secs
     Running target/debug/deps/solution-f5dd4e94aa395cae

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-c3b431457e2a7a27

running 15 tests
test solution_test::test_alpha_count ... ok
test solution_test::test_alpha_count_2 ... ok
test solution_test::test_char_count ... ok
test solution_test::test_cyrillic_letter_count ... ok
test solution_test::test_emotions ... ok
test solution_test::test_emotions_repeated_punctuation ... ok
test solution_test::test_empty_string ... ok
test solution_test::test_latin_letter_count ... ok
test solution_test::test_sentence_count ... ok
test solution_test::test_sentence_count_2 ... ok
test solution_test::test_triple_dots_count ... ok
test solution_test::test_unicode_char_count ... ok
test solution_test::test_word_count ... ok
test solution_test::test_word_count_2 ... ok
test solution_test::test_word_count_3 ... ok

test result: ok. 15 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 коментар)

Исмаил качи първо решение на 21.10.2017 22:33 (преди почти 8 години)