Решение на Text Info от Михаил Стойков

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

Към профила на Михаил Стойков

Резултати

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

Код

pub struct TextInfo {
char_count: usize,
alphabetic_count: usize,
cyrillic_letter_count: usize,
latin_letter_count: usize,
word_count: usize,
sentence_count: usize,
emotion: String,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
let mut result = TextInfo {
char_count: 0,
alphabetic_count: 0,
cyrillic_letter_count: 0,
latin_letter_count: 0,
word_count: 0,
sentence_count: 0,
emotion: String::from("0"),
};
let mut in_word = false;
let mut in_sentence = false;
let mut sentencetype: (usize, usize, usize) = (0, 0, 0); // сигурно има як начин с enum-и, но е късно, ще пробвам утре :)
for ch in s.chars() {
result.char_count += 1;
if ch.is_alphabetic() {
in_sentence = true;
in_word = true;
result.alphabetic_count += 1;
match ch {
'а'...'я' | 'А'...'Я' => result.cyrillic_letter_count += 1,
'a'...'z' | 'A'...'Z' => result.latin_letter_count += 1,
_ => {}
}
} else {
if in_word {
in_word = false;
result.word_count += 1;
}
match ch {
'!' | '?' | '.' => {
if in_sentence {
in_sentence = false;
result.sentence_count += 1;
}
match ch {
'!' => sentencetype.0 += 1,
'?' => sentencetype.1 += 1,
'.' => sentencetype.2 += 1,
_ => assert!(false), // NOPE, not smart enough
}
}
_ => {
in_sentence = true;
}
}
}
}
if in_word {
// if the sentence ends in a 'word' char than we wouldn't count the last word
result.word_count += 1
}
if sentencetype.0 > sentencetype.1 && sentencetype.0 > sentencetype.2 {
result.emotion = String::from("😮");
} else if sentencetype.1 > sentencetype.0 && sentencetype.1 > sentencetype.2 {
result.emotion = String::from("🤔");
} else {
result.emotion = String::from("😐");
}
result
}
pub fn char_count(&self) -> usize {
self.char_count
}
pub fn alphabetic_count(&self) -> usize {
self.alphabetic_count
}
pub fn cyrillic_letter_count(&self) -> usize {
self.cyrillic_letter_count
}
pub fn latin_letter_count(&self) -> usize {
self.latin_letter_count
}
pub fn word_count(&self) -> usize {
self.word_count
}
pub fn sentence_count(&self) -> usize {
self.sentence_count
}
pub fn emotion(&self) -> String {
self.emotion.clone()
}
}

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

Compiling solution v0.1.0 (file:///tmp/d20171026-5817-vj29r6/solution)
    Finished dev [unoptimized + debuginfo] target(s) in 2.74 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 ... FAILED
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

failures:

---- solution_test::test_emotions_repeated_punctuation stdout ----
	thread 'solution_test::test_emotions_repeated_punctuation' panicked at 'assertion failed: `(left == right)`
  left: `"😮"`,
 right: `"🤔"`', tests/solution_test.rs:104:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.


failures:
    solution_test::test_emotions_repeated_punctuation

test result: FAILED. 14 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

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

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

Михаил качи решение на 22.10.2017 11:32 (преди почти 8 години)

pub struct TextInfo {
char_count: usize,
alphabetic_count: usize,
cyrillic_letter_count: usize,
latin_letter_count: usize,
word_count: usize,
sentence_count: usize,
emotion: String,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
let mut result = TextInfo {
char_count: 0,
alphabetic_count: 0,
cyrillic_letter_count: 0,
latin_letter_count: 0,
word_count: 0,
sentence_count: 0,
emotion: String::from("0"),
};
let mut in_word = false;
let mut in_sentence = false;
let mut sentencetype: (usize, usize, usize) = (0, 0, 0); // сигурно има як начин с enum-и, но е късно, ще пробвам утре :)
for ch in s.chars() {
result.char_count += 1;
if ch.is_alphabetic() {
in_sentence = true;
in_word = true;
result.alphabetic_count += 1;
match ch {
'а'...'я' | 'А'...'Я' => result.cyrillic_letter_count += 1,
'a'...'z' | 'A'...'Z' => result.latin_letter_count += 1,
_ => {}
}
} else {
if in_word {
in_word = false;
result.word_count += 1;
}
match ch {
- '?' | '!' | '.' => {
+ '!' | '?' | '.' => {
if in_sentence {
in_sentence = false;
result.sentence_count += 1;
}
match ch {
- '?' => sentencetype.0 += 1,
- '!' => sentencetype.1 += 1,
+ '!' => sentencetype.0 += 1,
+ '?' => sentencetype.1 += 1,
'.' => sentencetype.2 += 1,
_ => assert!(false), // NOPE, not smart enough
}
}
_ => {
in_sentence = true;
}
}
}
}
if in_word {
// if the sentence ends in a 'word' char than we wouldn't count the last word
result.word_count += 1
}
if sentencetype.0 > sentencetype.1 && sentencetype.0 > sentencetype.2 {
result.emotion = String::from("😮");
} else if sentencetype.1 > sentencetype.0 && sentencetype.1 > sentencetype.2 {
result.emotion = String::from("🤔");
} else {
result.emotion = String::from("😐");
}
result
}
pub fn char_count(&self) -> usize {
self.char_count
}
pub fn alphabetic_count(&self) -> usize {
self.alphabetic_count
}
pub fn cyrillic_letter_count(&self) -> usize {
self.cyrillic_letter_count
}
pub fn latin_letter_count(&self) -> usize {
self.latin_letter_count
}
pub fn word_count(&self) -> usize {
self.word_count
}
pub fn sentence_count(&self) -> usize {
self.sentence_count
}
pub fn emotion(&self) -> String {
self.emotion.clone()
}
}