Решение на Text Info от Димитър Узунов

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

Към профила на Димитър Узунов

Резултати

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

Код

pub struct TextInfo {
text: String,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
Self { text: String::from(s) }
}
pub fn char_count(&self) -> usize {
self.text.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
let mut count = 0;
for c in self.text.chars() {
if c.is_alphabetic() {
count += 1;
}
}
count
}
pub fn latin_letter_count(&self) -> usize {
let mut count = 0;
for c in self.text.chars() {
if Self::is_latin_letter(c) {
count += 1;
}
}
count
}
pub fn cyrillic_letter_count(&self) -> usize {
let mut count = 0;
for c in self.text.chars() {
if Self::is_cyrillic_letter(c) {
count += 1;
}
}
count
}
pub fn word_count(&self) -> usize {
let mut count = 0;
let mut in_word = false;
for c in self.text.chars() {
if Self::is_letter(c) && !in_word {
in_word = true;
count += 1;
} else if !Self::is_letter(c) && in_word {
in_word = false;
}
}
count
}
pub fn sentence_count(&self) -> usize {
let mut count = 0;
let mut in_separators = true;
for c in self.text.chars() {
if Self::is_sentence_separator(c) && !in_separators {
in_separators = true;
count += 1;
} else if !Self::is_sentence_separator(c) && in_separators {
in_separators = false;
}
}
count
}
pub fn emotion(&self) -> String {
let declarative_sentence_count = self.typed_sentence_count('.');
let interrogative_sentence_count = self.typed_sentence_count('?');
let exclamation_sentence_count = self.typed_sentence_count('!');
if exclamation_sentence_count > declarative_sentence_count &&
exclamation_sentence_count > interrogative_sentence_count {
return String::from("😮");
} else if interrogative_sentence_count > declarative_sentence_count &&
interrogative_sentence_count > exclamation_sentence_count {
return String::from("🤔");
} else {
return String::from("😐");
}
}
fn is_latin_letter(c: char) -> bool {
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
fn is_cyrillic_letter(c: char) -> bool {
(c >= 'а' && c <= 'я') || (c >= 'А' && c <= 'Я')
}
fn is_letter(c: char) -> bool {
Self::is_latin_letter(c) || Self::is_cyrillic_letter(c)
}
fn is_sentence_separator(c: char) -> bool {
c == '.' || c == '?' || c == '!'
}
fn typed_sentence_count(&self, separator: char) -> usize {
let mut count = 0;
let mut in_separators = true;
for c in self.text.chars() {
if c == separator && !in_separators {
in_separators = true;
count += 1;
} else if !Self::is_sentence_separator(c) && in_separators {
in_separators = false;
}
}
count
}
}

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

Compiling solution v0.1.0 (file:///tmp/d20171026-5817-15zk608/solution)
    Finished dev [unoptimized + debuginfo] target(s) in 3.23 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 версия и 0 коментара)

Димитър качи първо решение на 22.10.2017 19:03 (преди почти 8 години)