Решение на Думи на Фибоначи от Даниел Янев

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

Към профила на Даниел Янев

Резултати

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

Код

pub struct FibIter {
fb_prev: u32,
fb_curr: u32,
fb_next: u32,
}
impl FibIter {
pub fn new() -> FibIter {
let fb_prev: u32 = 1;
let fb_curr: u32 = 1;
let fb_next: u32 = 2;
FibIter {
fb_prev,
fb_curr,
fb_next,
}
}
pub fn next(&mut self) -> u32 {
let result = self.fb_prev;
let next_el = self.fb_curr + self.fb_next;
self.fb_prev = self.fb_curr;
self.fb_curr = self.fb_next;
self.fb_next = next_el;
result
}
}
impl FibIter {
pub fn rev(self) -> RevFibIter {
let rev_fib_prev: Option<u32> = Option::Some(self.fb_curr);
let rev_fib_curr: Option<u32> = Option::Some(self.fb_prev);
let rev_fib_next = if self.fb_curr == 1 && self.fb_prev == 1 {
None
} else {
Some(self.fb_curr - self.fb_prev)
};
RevFibIter {
rev_fib_prev,
rev_fib_curr,
rev_fib_next,
}
}
}
pub struct RevFibIter {
rev_fib_prev: Option<u32>,
rev_fib_curr: Option<u32>,
rev_fib_next: Option<u32>,
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
let result = self.rev_fib_next;
match (self.rev_fib_curr, self.rev_fib_next) {
(Some(first), Some(second)) if first == 1 && second == 1 => self.rev_fib_next = None,
(Some(first), Some(second)) => {
self.rev_fib_next = Some(first - second);
self.rev_fib_curr = Some(second);
self.rev_fib_prev = Some(first);
}
(None, _) => self.rev_fib_prev = None,
(_, None) => self.rev_fib_curr = None,
}
result
}
}
pub fn words_concat_len(words: &Vec<String>) -> usize {
let mut sum = 0;
for word in words {
sum = sum + word.len();
}
sum
}
pub fn fib_split(text: &str) -> Vec<String> {
let mut words: Vec<String> = Vec::new();
let mut fib_series = FibIter::new();
let mut index = 0;
let mut word_length = fib_series.next() as usize;
let text_length = text.len();
loop {
let start = index;
let end = word_length as usize + index;
words.push(String::from(&text[start..end]));
index = word_length + index;
word_length = fib_series.next() as usize;
if index + word_length > text_length {
if index != text_length {
words.push(String::from(&text[index..]));
}
break words;
}
}
}
pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) {
let words: Vec<String> = fib_split(&text);
let amount = n as usize;
if words.len() < amount {
panic!("Insufficent words!");
}
let result_words: Vec<String> = words[0..amount].to_vec();
let rest = words_concat_len(&result_words);
(result_words, &text[rest..])
}
pub fn reverse_iterator(amount: usize) -> RevFibIter {
let mut dummy_fib_iter = FibIter::new();
let mut index = 0;
while index < amount {
dummy_fib_iter.next();
index = index + 1;
}
dummy_fib_iter.rev()
}
pub fn fib_split_rev(text: &str, mut rev_fib_series: RevFibIter) -> Vec<String> {
let mut words: Vec<String> = Vec::new();
let mut index = 0;
while let Some(word_length) = rev_fib_series.next() {
let start = index;
let end = word_length as usize + index;
if end > text.len() {
panic!("Insufficent words!");
}
words.push(String::from(&text[start..end]));
index = word_length as usize + index;
}
words
}
pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) {
let split = fib_split_n(&text, n);
let amount = n as usize;
let mut result: Vec<String> = split.0[0..amount].to_vec();
let rev_fib_iter = reverse_iterator(amount);
let mut rev_words: Vec<String> = fib_split_rev(split.1, rev_fib_iter);
let rest = words_concat_len(&result) + words_concat_len(&rev_words);
result.append(&mut rev_words);
(result, &text[rest..])
}

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

Compiling solution v0.1.0 (/tmp/d20241104-1739405-1hoyc1u/solution)
    Finished test [unoptimized + debuginfo] target(s) in 0.88s
     Running tests/solution_test.rs (target/debug/deps/solution_test-1428e1090729d165)

running 20 tests
test solution_test::fib_iter ... ok
test solution_test::fib_split_ascii ... ok
test solution_test::fib_split_empty ... FAILED
test solution_test::fib_split_cyrillic ... FAILED
test solution_test::fib_split_n_ascii ... ok
test solution_test::fib_split_n_ascii_exact ... ok
test solution_test::fib_split_n_ascii_panic ... FAILED
test solution_test::fib_split_n_cyrillic ... FAILED
test solution_test::fib_split_n_cyrillic_exact ... FAILED
test solution_test::fib_split_n_cyrillic_panic ... ok
test solution_test::fib_split_n_symmetric_ascii ... ok
test solution_test::fib_split_n_symmetric_ascii_exact ... ok
test solution_test::fib_split_n_symmetric_ascii_panic ... ok
test solution_test::fib_split_n_symmetric_cyrillic ... FAILED
test solution_test::fib_split_n_symmetric_cyrillic_exact ... FAILED
test solution_test::fib_split_n_symmetric_cyrillic_panic ... ok
test solution_test::fib_split_n_zero ... FAILED
test solution_test::fib_split_n_symmetric_zero ... FAILED
test solution_test::rev_fib_iter ... ok
test solution_test::rev_fib_iter_empty ... ok

failures:

---- solution_test::fib_split_empty stdout ----
thread 'solution_test::fib_split_empty' panicked at 'byte index 1 is out of bounds of ``', src/lib.rs:98:34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::fib_split_cyrillic stdout ----
thread 'solution_test::fib_split_cyrillic' panicked at 'byte index 1 is not a char boundary; it is inside 'м' (bytes 0..2) of `манджа с грозде`', src/lib.rs:98:34

---- solution_test::fib_split_n_ascii_panic stdout ----
thread 'solution_test::fib_split_n_ascii_panic' panicked at 'byte index 1 is out of bounds of ``', src/lib.rs:98:34
thread 'solution_test::fib_split_n_ascii_panic' panicked at 'assertion failed: catch_unwind(|| fib_split_n(\"abcd_123\", 5)).is_err()', tests/solution_test.rs:96:5

---- solution_test::fib_split_n_cyrillic stdout ----
thread 'solution_test::fib_split_n_cyrillic' panicked at 'byte index 1 is not a char boundary; it is inside 'м' (bytes 0..2) of `манджа_with_грозде`', src/lib.rs:98:34

---- solution_test::fib_split_n_cyrillic_exact stdout ----
thread 'solution_test::fib_split_n_cyrillic_exact' panicked at 'byte index 1 is not a char boundary; it is inside 'м' (bytes 0..2) of `манджа_with_`', src/lib.rs:98:34

---- solution_test::fib_split_n_symmetric_cyrillic stdout ----
thread 'solution_test::fib_split_n_symmetric_cyrillic' panicked at 'byte index 1 is not a char boundary; it is inside 'м' (bytes 0..2) of `манджа_with_грозде`', src/lib.rs:98:34

---- solution_test::fib_split_n_symmetric_cyrillic_exact stdout ----
thread 'solution_test::fib_split_n_symmetric_cyrillic_exact' panicked at 'byte index 1 is not a char boundary; it is inside 'м' (bytes 0..2) of `манджа_with_гр`', src/lib.rs:98:34

---- solution_test::fib_split_n_zero stdout ----
thread 'solution_test::fib_split_n_zero' panicked at 'byte index 1 is out of bounds of ``', src/lib.rs:98:34

---- solution_test::fib_split_n_symmetric_zero stdout ----
thread 'solution_test::fib_split_n_symmetric_zero' panicked at 'byte index 1 is out of bounds of ``', src/lib.rs:98:34


failures:
    solution_test::fib_split_cyrillic
    solution_test::fib_split_empty
    solution_test::fib_split_n_ascii_panic
    solution_test::fib_split_n_cyrillic
    solution_test::fib_split_n_cyrillic_exact
    solution_test::fib_split_n_symmetric_cyrillic
    solution_test::fib_split_n_symmetric_cyrillic_exact
    solution_test::fib_split_n_symmetric_zero
    solution_test::fib_split_n_zero

test result: FAILED. 11 passed; 9 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

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

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

Даниел качи първо решение на 30.10.2024 22:05 (преди 11 месеца)

Даниел качи решение на 30.10.2024 22:24 (преди 11 месеца)

pub struct FibIter {
fb_prev: u32,
fb_curr: u32,
fb_next: u32,
}
impl FibIter {
pub fn new() -> FibIter {
let fb_prev: u32 = 1;
let fb_curr: u32 = 1;
let fb_next: u32 = 2;
FibIter {
fb_prev,
fb_curr,
fb_next,
}
}
pub fn next(&mut self) -> u32 {
let result = self.fb_prev;
let next_el = self.fb_curr + self.fb_next;
self.fb_prev = self.fb_curr;
self.fb_curr = self.fb_next;
self.fb_next = next_el;
result
}
}
impl FibIter {
pub fn rev(self) -> RevFibIter {
let rev_fib_prev: Option<u32> = Option::Some(self.fb_curr);
let rev_fib_curr: Option<u32> = Option::Some(self.fb_prev);
- let rev_fib_next = match (rev_fib_prev, rev_fib_curr) {
- (Some(first), Some(second)) if first == 1 && second == 1 => None,
- (Some(first), Some(second)) => Some(first - second),
- (None, _) => None,
- (_, None) => None,
+ let rev_fib_next = if self.fb_curr == 1 && self.fb_prev == 1 {
+ None
+ } else {
+ Some(self.fb_curr - self.fb_prev)
};
RevFibIter {
rev_fib_prev,
rev_fib_curr,
rev_fib_next,
}
}
}
pub struct RevFibIter {
rev_fib_prev: Option<u32>,
rev_fib_curr: Option<u32>,
rev_fib_next: Option<u32>,
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
let result = self.rev_fib_next;
match (self.rev_fib_curr, self.rev_fib_next) {
(Some(first), Some(second)) if first == 1 && second == 1 => self.rev_fib_next = None,
(Some(first), Some(second)) => {
self.rev_fib_next = Some(first - second);
self.rev_fib_curr = Some(second);
self.rev_fib_prev = Some(first);
}
(None, _) => self.rev_fib_prev = None,
(_, None) => self.rev_fib_curr = None,
}
result
}
}
pub fn words_concat_len(words: &Vec<String>) -> usize {
let mut sum = 0;
for word in words {
sum = sum + word.len();
}
sum
}
pub fn fib_split(text: &str) -> Vec<String> {
let mut words: Vec<String> = Vec::new();
let mut fib_series = FibIter::new();
let mut index = 0;
let mut word_length = fib_series.next() as usize;
let text_length = text.len();
loop {
let start = index;
let end = word_length as usize + index;
words.push(String::from(&text[start..end]));
index = word_length + index;
word_length = fib_series.next() as usize;
if index + word_length > text_length {
if index != text_length {
words.push(String::from(&text[index..]));
}
break words;
}
}
}
pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) {
let words: Vec<String> = fib_split(&text);
let amount = n as usize;
if words.len() < amount {
panic!("Insufficent words!");
}
let result_words: Vec<String> = words[0..amount].to_vec();
let rest = words_concat_len(&result_words);
(result_words, &text[rest..])
}
pub fn reverse_iterator(amount: usize) -> RevFibIter {
let mut dummy_fib_iter = FibIter::new();
let mut index = 0;
while index < amount {
dummy_fib_iter.next();
index = index + 1;
}
dummy_fib_iter.rev()
}
pub fn fib_split_rev(text: &str, mut rev_fib_series: RevFibIter) -> Vec<String> {
let mut words: Vec<String> = Vec::new();
let mut index = 0;
while let Some(word_length) = rev_fib_series.next() {
let start = index;
let end = word_length as usize + index;
if end > text.len() {
panic!("Insufficent words!");
}
words.push(String::from(&text[start..end]));
index = word_length as usize + index;
}
words
}
pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) {
let split = fib_split_n(&text, n);
let amount = n as usize;
let mut result: Vec<String> = split.0[0..amount].to_vec();
let rev_fib_iter = reverse_iterator(amount);
let mut rev_words: Vec<String> = fib_split_rev(split.1, rev_fib_iter);
let rest = words_concat_len(&result) + words_concat_len(&rev_words);
result.append(&mut rev_words);
(result, &text[rest..])
}