Решение на Text Info от Радослав Георгиев

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

Към профила на Радослав Георгиев

Резултати

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

Код

pub struct TextInfo {
num_characters: usize,
num_alphabetic: usize,
num_latin: usize,
num_cyrillic: usize,
num_words: usize,
num_sentences: usize,
num_exclamations: usize,
num_questions: usize,
num_statements: usize,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
let mut text_info = Self {
num_characters: 0,
num_alphabetic: 0,
num_latin: 0,
num_cyrillic: 0,
num_words: 0,
num_sentences: 0,
num_exclamations: 0,
num_questions: 0,
num_statements: 0,
};
let mut is_inside_word = false;
let mut is_inside_sentence = false;
for character in s.chars() {
text_info.num_characters += 1;
if character.is_alphabetic() {
text_info.num_alphabetic += 1;
}
match character {
'A'...'Z' | 'a'...'z' | 'А'...'Я' | 'а'...'я' => {
match character {
'A'...'Z' | 'a'...'z' => text_info.num_latin += 1,
_ => text_info.num_cyrillic += 1,
}
if !is_inside_word {
text_info.num_words += 1;
is_inside_word = true;
}
}
_ => is_inside_word = false,
}
match character {
'!' | '?' | '.' => if is_inside_sentence {
text_info.num_sentences += 1;
match character {
'!' => text_info.num_exclamations += 1,
'?' => text_info.num_questions += 1,
_ => text_info.num_statements += 1,
}
is_inside_sentence = false;
},
_ => is_inside_sentence = true,
}
}
text_info
}
pub fn char_count(&self) -> usize {
self.num_characters
}
pub fn alphabetic_count(&self) -> usize {
self.num_alphabetic
}
pub fn latin_letter_count(&self) -> usize {
self.num_latin
}
pub fn cyrillic_letter_count(&self) -> usize {
self.num_cyrillic
}
pub fn word_count(&self) -> usize {
self.num_words
}
pub fn sentence_count(&self) -> usize {
self.num_sentences
}
pub fn emotion(&self) -> String {
String::from(
if self.num_exclamations > self.num_questions
&& self.num_exclamations > self.num_statements
{
"😮"
} else if self.num_questions > self.num_exclamations
&& self.num_questions > self.num_statements
{
"🤔"
} else {
"😐"
},
)
}
}

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

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

История (4 версии и 0 коментара)

Радослав качи първо решение на 25.10.2017 09:51 (преди почти 8 години)

Радослав качи решение на 25.10.2017 22:47 (преди почти 8 години)

pub struct TextInfo {
num_characters: usize,
num_alphabetic: usize,
num_latin: usize,
num_cyrillic: usize,
num_words: usize,
num_sentences: usize,
num_exclamations: usize,
num_questions: usize,
num_statements: usize,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
let mut text_info = Self {
num_characters: 0,
num_alphabetic: 0,
num_latin: 0,
num_cyrillic: 0,
num_words: 0,
num_sentences: 0,
num_exclamations: 0,
num_questions: 0,
num_statements: 0,
};
let mut is_inside_word = false;
let mut is_inside_sentence = false;
for character in s.chars() {
text_info.num_characters += 1;
if character.is_alphabetic() {
text_info.num_alphabetic += 1;
}
- if character >= 'A' && character <= 'Z' || character >= 'a' && character <= 'z' {
- text_info.num_latin += 1;
- if !is_inside_word {
- text_info.num_words += 1;
- }
- is_inside_word = true;
- } else if character >= 'А' && character <= 'Я' || character >= 'а' && character <= 'я' {
- text_info.num_cyrillic += 1;
- if !is_inside_word {
- text_info.num_words += 1;
- }
- is_inside_word = true;
- } else {
- is_inside_word = false;
+ match character {
+ 'A' ... 'Z' | 'a' ... 'z' | 'А' ... 'Я' | 'а' ... 'я' => {
+ match character {
+ 'A' ... 'Z' | 'a' ... 'z' => text_info.num_latin += 1,
+ _ => text_info.num_cyrillic += 1,
+ }
+ if !is_inside_word {
+ text_info.num_words += 1;
+ }
+ is_inside_word = true;
+ },
+ _ => is_inside_word = false,
}
- if character == '!' || character == '?' || character == '.' {
- if is_inside_sentence {
- text_info.num_sentences += 1;
- if character == '!' {
- text_info.num_exclamations += 1;
- } else if character == '?' {
- text_info.num_questions += 1;
- } else {
- text_info.num_statements += 1;
+ match character {
+ '!' | '?' | '.' => {
+ if is_inside_sentence {
+ text_info.num_sentences += 1;
+ match character {
+ '!' => text_info.num_exclamations += 1,
+ '?' => text_info.num_questions += 1,
+ _ => text_info.num_statements += 1,
+ }
}
- }
- is_inside_sentence = false;
- } else {
- is_inside_sentence = true;
+ is_inside_sentence = false;
+ },
+ _ => is_inside_sentence = true,
}
};
text_info
}
pub fn char_count(&self) -> usize {
self.num_characters
}
pub fn alphabetic_count(&self) -> usize {
self.num_alphabetic
}
pub fn latin_letter_count(&self) -> usize {
self.num_latin
}
pub fn cyrillic_letter_count(&self) -> usize {
self.num_cyrillic
}
pub fn word_count(&self) -> usize {
self.num_words
}
pub fn sentence_count(&self) -> usize {
self.num_sentences
}
pub fn emotion(&self) -> String {
String::from(
if self.num_exclamations > self.num_questions && self.num_exclamations > self.num_statements {
"😮"
} else if self.num_questions > self.num_exclamations && self.num_questions > self.num_statements {
"🤔"
} else {
"😐"
}
)
}
}

Радослав качи решение на 25.10.2017 22:53 (преди почти 8 години)

pub struct TextInfo {
num_characters: usize,
num_alphabetic: usize,
num_latin: usize,
num_cyrillic: usize,
num_words: usize,
num_sentences: usize,
num_exclamations: usize,
num_questions: usize,
num_statements: usize,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
let mut text_info = Self {
num_characters: 0,
num_alphabetic: 0,
num_latin: 0,
num_cyrillic: 0,
num_words: 0,
num_sentences: 0,
num_exclamations: 0,
num_questions: 0,
num_statements: 0,
};
let mut is_inside_word = false;
let mut is_inside_sentence = false;
for character in s.chars() {
text_info.num_characters += 1;
if character.is_alphabetic() {
text_info.num_alphabetic += 1;
}
match character {
- 'A' ... 'Z' | 'a' ... 'z' | 'А' ... 'Я' | 'а' ... 'я' => {
+ 'A'...'Z' | 'a'...'z' | 'А'...'Я' | 'а'...'я' => {
match character {
- 'A' ... 'Z' | 'a' ... 'z' => text_info.num_latin += 1,
- _ => text_info.num_cyrillic += 1,
+ 'A'...'Z' | 'a'...'z' => text_info.num_latin += 1,
+ _ => text_info.num_cyrillic += 1,
}
if !is_inside_word {
text_info.num_words += 1;
}
is_inside_word = true;
- },
+ }
_ => is_inside_word = false,
}
match character {
'!' | '?' | '.' => {
if is_inside_sentence {
text_info.num_sentences += 1;
match character {
'!' => text_info.num_exclamations += 1,
'?' => text_info.num_questions += 1,
- _ => text_info.num_statements += 1,
+ _ => text_info.num_statements += 1,
}
}
is_inside_sentence = false;
- },
+ }
_ => is_inside_sentence = true,
}
- };
+ }
text_info
}
pub fn char_count(&self) -> usize {
self.num_characters
}
pub fn alphabetic_count(&self) -> usize {
self.num_alphabetic
}
pub fn latin_letter_count(&self) -> usize {
self.num_latin
}
pub fn cyrillic_letter_count(&self) -> usize {
self.num_cyrillic
}
pub fn word_count(&self) -> usize {
self.num_words
}
pub fn sentence_count(&self) -> usize {
self.num_sentences
}
pub fn emotion(&self) -> String {
- String::from(
- if self.num_exclamations > self.num_questions && self.num_exclamations > self.num_statements {
- "😮"
- } else if self.num_questions > self.num_exclamations && self.num_questions > self.num_statements {
- "🤔"
- } else {
- "😐"
- }
- )
+ String::from(if self.num_exclamations > self.num_questions &&
+ self.num_exclamations > self.num_statements
+ {
+ "😮"
+ } else if self.num_questions > self.num_exclamations &&
+ self.num_questions > self.num_statements
+ {
+ "🤔"
+ } else {
+ "😐"
+ })
}
}

Радослав качи решение на 25.10.2017 22:57 (преди почти 8 години)

pub struct TextInfo {
num_characters: usize,
num_alphabetic: usize,
num_latin: usize,
num_cyrillic: usize,
num_words: usize,
num_sentences: usize,
num_exclamations: usize,
num_questions: usize,
num_statements: usize,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
let mut text_info = Self {
num_characters: 0,
num_alphabetic: 0,
num_latin: 0,
num_cyrillic: 0,
num_words: 0,
num_sentences: 0,
num_exclamations: 0,
num_questions: 0,
num_statements: 0,
};
let mut is_inside_word = false;
let mut is_inside_sentence = false;
for character in s.chars() {
text_info.num_characters += 1;
if character.is_alphabetic() {
text_info.num_alphabetic += 1;
}
match character {
'A'...'Z' | 'a'...'z' | 'А'...'Я' | 'а'...'я' => {
match character {
'A'...'Z' | 'a'...'z' => text_info.num_latin += 1,
_ => text_info.num_cyrillic += 1,
}
if !is_inside_word {
text_info.num_words += 1;
+ is_inside_word = true;
}
- is_inside_word = true;
}
_ => is_inside_word = false,
}
match character {
- '!' | '?' | '.' => {
- if is_inside_sentence {
- text_info.num_sentences += 1;
- match character {
- '!' => text_info.num_exclamations += 1,
- '?' => text_info.num_questions += 1,
- _ => text_info.num_statements += 1,
- }
+ '!' | '?' | '.' => if is_inside_sentence {
+ text_info.num_sentences += 1;
+ match character {
+ '!' => text_info.num_exclamations += 1,
+ '?' => text_info.num_questions += 1,
+ _ => text_info.num_statements += 1,
}
is_inside_sentence = false;
- }
+ },
_ => is_inside_sentence = true,
}
}
text_info
}
pub fn char_count(&self) -> usize {
self.num_characters
}
pub fn alphabetic_count(&self) -> usize {
self.num_alphabetic
}
pub fn latin_letter_count(&self) -> usize {
self.num_latin
}
pub fn cyrillic_letter_count(&self) -> usize {
self.num_cyrillic
}
pub fn word_count(&self) -> usize {
self.num_words
}
pub fn sentence_count(&self) -> usize {
self.num_sentences
}
pub fn emotion(&self) -> String {
- String::from(if self.num_exclamations > self.num_questions &&
- self.num_exclamations > self.num_statements
- {
- "😮"
- } else if self.num_questions > self.num_exclamations &&
- self.num_questions > self.num_statements
- {
- "🤔"
- } else {
- "😐"
- })
+ String::from(
+ if self.num_exclamations > self.num_questions
+ && self.num_exclamations > self.num_statements
+ {
+ "😮"
+ } else if self.num_questions > self.num_exclamations
+ && self.num_questions > self.num_statements
+ {
+ "🤔"
+ } else {
+ "😐"
+ },
+ )
}
}