Решение на Text Info от Емилиан Станков

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

Към профила на Емилиан Станков

Резултати

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

Код

pub struct TextInfo {
text: String,
latin: String,
cyrilic: String,
punctuation: String,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
TextInfo {
text: String::from(s),
latin: String::from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
cyrilic: String::from("абвгдежзийклмнопрстуфхцчшщъьюяАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ"),
punctuation: String::from(".!?"),
}
}
pub fn char_count(&self) -> usize {
self.text.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if ch.is_alphabetic() {count += 1; count} else {count}
)
}
pub fn latin_letter_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if self.is_latin(ch) {count += 1; count} else {count}
)
}
pub fn cyrillic_letter_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if self.is_cyrilic(ch) {count += 1; count} else {count}
)
}
pub fn word_count(&self) -> usize {
self.get_words().iter().count()
}
pub fn sentence_count(&self) -> usize {
self.get_sentences().iter().count()
}
pub fn emotion(&self) -> String {
let mut surprised = 0;
let mut thinking = 0;
let mut neutral = 0;
self.get_sentences().iter().for_each(|sentence|
if sentence.ends_with("!") {
surprised += 1
} else if sentence.ends_with("?") {
thinking += 1
} else {
neutral += 1
}
);
if surprised > thinking && surprised > neutral {
String::from("😮")
} else if thinking > surprised && thinking > neutral {
String::from("🤔")
} else {
String::from("😐")
}
}
fn get_words(&self) -> Vec<String> {
let mut words = vec![];
let mut prev = '\x00';
let mut word = "".to_string();
self.text.chars().for_each(|ch|
if !self.is_latin(ch) && !self.is_cyrilic(ch) {
if prev != '\x00' && (self.is_latin(prev) || self.is_cyrilic(prev)) {
words.push(word.clone());
word = "".to_string();
prev = '\x00';
} else {
prev = ch;
}
} else {
prev = ch;
word.push_str(&ch.to_string());
}
);
if word != "".to_string() {words.push(word)};
words
}
fn get_sentences(&self) -> Vec<String> {
let mut sentences = vec![];
let mut prev = '\x00';
let mut sentence = "".to_string();
self.text.chars().for_each(|ch|
if self.is_punctuation(ch) {
if prev != '\x00' {
prev = '\x00';
sentence.push_str(&ch.to_string());
sentences.push(sentence.clone());
sentence = "".to_string();
}
} else {
prev = ch;
sentence.push_str(&ch.to_string())
}
);
sentences
}
fn is_latin(&self, ch: char) -> bool {
self.latin.contains(ch)
}
fn is_cyrilic(&self, ch: char) -> bool {
self.cyrilic.contains(ch)
}
fn is_punctuation(&self, ch: char) -> bool {
self.punctuation.contains(ch)
}
}

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

Compiling solution v0.1.0 (file:///tmp/d20171026-5817-ufyhtr/solution)
invalid expression
!3250 = !DIExpression(6, 34, 0, 6)
invalid expression
!3253 = !DIExpression(6, 34, 8, 6)
invalid expression
!4039 = !DIExpression(6, 34, 0)
invalid expression
!3249 = !DIExpression(6, 34, 0, 6)
invalid expression
!3252 = !DIExpression(6, 34, 8, 6)
invalid expression
!4038 = !DIExpression(6, 34, 0)
    Finished dev [unoptimized + debuginfo] target(s) in 3.93 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 версии и 1 коментар)

Емилиан качи първо решение на 22.10.2017 12:12 (преди почти 8 години)

Емилиан качи решение на 22.10.2017 14:32 (преди почти 8 години)

pub struct TextInfo {
text: String,
latin: String,
cyrilic: String,
punctuation: String,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
TextInfo {
text: String::from(s),
latin: String::from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
cyrilic: String::from("абвгдежзийклмнопрстуфхцчшщъьюяАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ"),
punctuation: String::from(".!?"),
}
}
pub fn char_count(&self) -> usize {
self.text.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if ch.is_alphabetic() {count += 1; count} else {count}
)
}
pub fn latin_letter_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if self.latin.chars().find(|&latin| latin == ch).is_some() {
count += 1;
count
} else {
count
}
)
}
pub fn cyrillic_letter_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if self.cyrilic.chars().find(|&cyrilic| cyrilic == ch).is_some() {
count += 1;
count
} else {
count
}
)
}
pub fn word_count(&self) -> usize {
- let mut prev = '\x00';
- self.text.chars().fold(0, |mut count, ch|
- if (self.cyrilic.chars().find(|&cyrilic| cyrilic == prev).is_some()
- || self.latin.chars().find(|&latin| latin == prev).is_some())
- && (self.cyrilic.chars().find(|&cyrilic| cyrilic == ch).is_some()
- || self.latin.chars().find(|&latin| latin == ch).is_some()) {
- prev = '\x00';
- count += 1;
- count
- } else {
- prev = ch;
- count
- }
- )
+ self.get_words().iter().count()
}
pub fn sentence_count(&self) -> usize {
self.get_sentences().iter().count()
}
pub fn emotion(&self) -> String {
let mut surprised = 0;
let mut thinking = 0;
let mut neutral = 0;
self.get_sentences().iter().for_each(|sentence|
if sentence.ends_with("!") {
surprised += 1
} else if sentence.ends_with("?") {
thinking += 1
} else {
neutral += 1
}
);
if surprised > thinking && surprised > neutral {
String::from("😮")
} else if thinking > surprised && thinking > neutral {
String::from("🤔")
} else {
String::from("😐")
}
}
+ fn get_words(&self) -> Vec<String> {
+ let mut words = vec![];
+ let mut prev = '\x00';
+ let mut word = "".to_string();
+ self.text.chars().for_each(|ch|
+ if !self.latin.chars().find(|&latin| latin == ch).is_some()
+ && !self.cyrilic.chars().find(|&cyrilic| cyrilic == ch).is_some() {
+ if prev != '\x00' && (self.latin.chars().find(|&latin| latin == prev).is_some()
+ || self.cyrilic.chars().find(|&cyrilic| cyrilic == prev).is_some()) {
+ words.push(word.clone());
+ word = "".to_string();
+ prev = '\x00';
+ } else {
+ prev = ch;
+ }
+ } else {
+ prev = ch;
+ word.push_str(&ch.to_string());
+ }
+ );
+ if word != "".to_string() {words.push(word)};
+ words
+ }
+
fn get_sentences(&self) -> Vec<String> {
let mut sentences = vec![];
let mut prev = '\x00';
let mut sentence = "".to_string();
self.text.chars().for_each(|ch|
if self.punctuation.chars().find(|&sign| sign == ch).is_some() {
if prev != '\x00' {
prev = '\x00';
sentence.push_str(&ch.to_string());
sentences.push(sentence.clone());
+ sentence = "".to_string();
}
} else {
prev = ch;
sentence.push_str(&ch.to_string())
}
);
sentences
}
-}
+}

Емилиан качи решение на 22.10.2017 14:43 (преди почти 8 години)

pub struct TextInfo {
text: String,
latin: String,
cyrilic: String,
punctuation: String,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
TextInfo {
text: String::from(s),
latin: String::from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
cyrilic: String::from("абвгдежзийклмнопрстуфхцчшщъьюяАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ"),
punctuation: String::from(".!?"),
}
}
pub fn char_count(&self) -> usize {
self.text.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if ch.is_alphabetic() {count += 1; count} else {count}
)
}
pub fn latin_letter_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
- if self.latin.chars().find(|&latin| latin == ch).is_some() {
- count += 1;
- count
- } else {
- count
- }
+ if self.is_latin(ch) {count += 1; count} else {count}
)
}
pub fn cyrillic_letter_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
- if self.cyrilic.chars().find(|&cyrilic| cyrilic == ch).is_some() {
- count += 1;
- count
- } else {
- count
- }
+ if self.is_cyrilic(ch) {count += 1; count} else {count}
)
}
pub fn word_count(&self) -> usize {
self.get_words().iter().count()
}
pub fn sentence_count(&self) -> usize {
self.get_sentences().iter().count()
}
pub fn emotion(&self) -> String {
let mut surprised = 0;
let mut thinking = 0;
let mut neutral = 0;
self.get_sentences().iter().for_each(|sentence|
if sentence.ends_with("!") {
surprised += 1
} else if sentence.ends_with("?") {
thinking += 1
} else {
neutral += 1
}
);
if surprised > thinking && surprised > neutral {
String::from("😮")
} else if thinking > surprised && thinking > neutral {
String::from("🤔")
} else {
String::from("😐")
}
}
fn get_words(&self) -> Vec<String> {
let mut words = vec![];
let mut prev = '\x00';
let mut word = "".to_string();
self.text.chars().for_each(|ch|
- if !self.latin.chars().find(|&latin| latin == ch).is_some()
- && !self.cyrilic.chars().find(|&cyrilic| cyrilic == ch).is_some() {
- if prev != '\x00' && (self.latin.chars().find(|&latin| latin == prev).is_some()
- || self.cyrilic.chars().find(|&cyrilic| cyrilic == prev).is_some()) {
+ if !self.is_latin(ch) && !self.is_cyrilic(ch) {
+ if prev != '\x00' && (self.is_latin(prev) || self.is_cyrilic(prev)) {
words.push(word.clone());
word = "".to_string();
prev = '\x00';
} else {
prev = ch;
}
} else {
prev = ch;
word.push_str(&ch.to_string());
}
);
if word != "".to_string() {words.push(word)};
words
}
fn get_sentences(&self) -> Vec<String> {
let mut sentences = vec![];
let mut prev = '\x00';
let mut sentence = "".to_string();
self.text.chars().for_each(|ch|
- if self.punctuation.chars().find(|&sign| sign == ch).is_some() {
+ if self.is_punctuation(ch) {
if prev != '\x00' {
prev = '\x00';
sentence.push_str(&ch.to_string());
sentences.push(sentence.clone());
sentence = "".to_string();
}
} else {
prev = ch;
sentence.push_str(&ch.to_string())
}
);
sentences
+ }
+
+ fn is_latin(&self, ch: char) -> bool {
+ self.latin.chars().find(|&latin| latin == ch).is_some()
+ }
+
+ fn is_cyrilic(&self, ch: char) -> bool {
+ self.cyrilic.chars().find(|&cyrilic| cyrilic == ch).is_some()
+ }
+
+ fn is_punctuation(&self, ch: char) -> bool {
+ self.punctuation.chars().find(|&sign| sign == ch).is_some()
}
}

Емилиан качи решение на 24.10.2017 21:18 (преди почти 8 години)

pub struct TextInfo {
text: String,
latin: String,
cyrilic: String,
punctuation: String,
}
impl TextInfo {
pub fn new(s: &str) -> Self {
TextInfo {
text: String::from(s),
latin: String::from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
cyrilic: String::from("абвгдежзийклмнопрстуфхцчшщъьюяАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ"),
punctuation: String::from(".!?"),
}
}
pub fn char_count(&self) -> usize {
self.text.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if ch.is_alphabetic() {count += 1; count} else {count}
)
}
pub fn latin_letter_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if self.is_latin(ch) {count += 1; count} else {count}
)
}
pub fn cyrillic_letter_count(&self) -> usize {
self.text.chars().fold(0, |mut count, ch|
if self.is_cyrilic(ch) {count += 1; count} else {count}
)
}
pub fn word_count(&self) -> usize {
self.get_words().iter().count()
}
pub fn sentence_count(&self) -> usize {
self.get_sentences().iter().count()
}
pub fn emotion(&self) -> String {
let mut surprised = 0;
let mut thinking = 0;
let mut neutral = 0;
self.get_sentences().iter().for_each(|sentence|
if sentence.ends_with("!") {
surprised += 1
} else if sentence.ends_with("?") {
thinking += 1
} else {
neutral += 1
}
);
if surprised > thinking && surprised > neutral {
String::from("😮")
} else if thinking > surprised && thinking > neutral {
String::from("🤔")
} else {
String::from("😐")
}
}
fn get_words(&self) -> Vec<String> {
let mut words = vec![];
let mut prev = '\x00';
let mut word = "".to_string();
self.text.chars().for_each(|ch|
if !self.is_latin(ch) && !self.is_cyrilic(ch) {
if prev != '\x00' && (self.is_latin(prev) || self.is_cyrilic(prev)) {
words.push(word.clone());
word = "".to_string();
prev = '\x00';
} else {
prev = ch;
}
} else {
prev = ch;
word.push_str(&ch.to_string());
}
);
if word != "".to_string() {words.push(word)};
words
}
fn get_sentences(&self) -> Vec<String> {
let mut sentences = vec![];
let mut prev = '\x00';
let mut sentence = "".to_string();
self.text.chars().for_each(|ch|
if self.is_punctuation(ch) {
if prev != '\x00' {
prev = '\x00';
sentence.push_str(&ch.to_string());
sentences.push(sentence.clone());
sentence = "".to_string();
}
} else {
prev = ch;
sentence.push_str(&ch.to_string())
}
);
sentences
}
fn is_latin(&self, ch: char) -> bool {
- self.latin.chars().find(|&latin| latin == ch).is_some()
+ self.latin.contains(ch)
}
fn is_cyrilic(&self, ch: char) -> bool {
- self.cyrilic.chars().find(|&cyrilic| cyrilic == ch).is_some()
+ self.cyrilic.contains(ch)
}
fn is_punctuation(&self, ch: char) -> bool {
- self.punctuation.chars().find(|&sign| sign == ch).is_some()
+ self.punctuation.contains(ch)
}
}