Решение на Думи на Фибоначи от Илиян Георгиев

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

Към профила на Илиян Георгиев

Резултати

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

Код

use core::panic;
pub struct FibIter {
first: u32,
second: u32,
counter: u8
}
impl FibIter {
pub fn new() -> FibIter {
FibIter{first : 1, second : 1, counter : 0}
}
pub fn next(&mut self) -> u32 {
if self.counter < 2 {
self.counter += 1;
return 1;
}
else {
let next = self.first + self.second;
self.first = self.second;
self.second = next;
return next;
}
}
pub fn rev(self) -> RevFibIter {
RevFibIter{last: self.second, previous: self.first, counter: self.counter}
}
}
pub struct RevFibIter {
last: u32,
previous: u32,
counter: u8
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
if self.counter == 0 {
return None
}
if self.counter <= 2 && self.counter > 0 && self.last == 1 {
self.counter -= 1;
return Some(1);
}
let to_return = self.last;
self.last = self.previous;
self.previous = to_return - self.last;
Some(to_return)
}
}
pub fn fib_split(text: &str) -> Vec<String> {
let mut iter = FibIter::new();
let mut result : Vec<String> = Vec::new();
let mut counter: usize = 0;
let mut next_index: usize = iter.next() as usize;
loop {
if next_index < text.len() {
let substring = &text[counter..next_index as usize];
result.push(substring.to_string());
counter = next_index as usize;
next_index = counter + (iter.next() as usize);
} else {
result.push(text[counter..].to_string());
break;
}
}
result
}
pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) {
let fib_split_result = fib_split(text);
let mut vec_result : Vec<String> = Vec::new();
let mut counter = 0;
let mut strings_len = 0;
for i in 0..n as usize {
if i < fib_split_result.len() {
if i > 1 {
if fib_split_result[i-2].len() + fib_split_result[i-1].len() == fib_split_result[i].len() {
vec_result.push(fib_split_result[i].clone());
counter += 1;
strings_len += fib_split_result[i].len();
} else {
break;
}
} else {
vec_result.push(fib_split_result[i].clone());
counter += 1;
strings_len += fib_split_result[i].len();
}
}
}
if counter < n as usize {
panic!("Not long enough");
}
(vec_result, &text[strings_len..])
}
pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) {
let mut iter = FibIter::new();
let mut result : Vec<String> = Vec::new();
let mut counter: usize = 0;
let mut next_index: usize = iter.next() as usize;
for i in 0..n {
if next_index < text.len() && i < n {
let substring = &text[counter..next_index as usize];
result.push(substring.to_string());
counter = next_index as usize;
if i + 1 == n {
break;
}
next_index = counter + (iter.next() as usize);
} else {
break;
}
}
let mut rev_iter = iter.rev();
next_index += rev_iter.next().unwrap() as usize;
for i in 0..n {
if next_index < text.len() && i < n {
let substring = &text[counter..next_index as usize];
result.push(substring.to_string());
counter = next_index as usize;
match rev_iter.next() {
Some(next) => { next_index = counter + next as usize },
None => {break}
}
} else {
break;
}
}
if result.len() != 2*n as usize {
panic!("Not enough symbols");
}
(result, &text[next_index..])
}

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

Compiling solution v0.1.0 (/tmp/d20241104-1739405-1ejr5tx/solution)
    Finished test [unoptimized + debuginfo] target(s) in 0.89s
     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 ... ok
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 ... FAILED
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_symmetric_zero ... FAILED
test solution_test::rev_fib_iter ... ok
test solution_test::fib_split_n_zero ... FAILED
test solution_test::rev_fib_iter_empty ... ok

failures:

---- 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:67:30
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::fib_split_n_ascii_panic stdout ----
thread 'solution_test::fib_split_n_ascii_panic' panicked at 'assertion failed: catch_unwind(|| fib_split_n(\"\", 1)).is_err()', tests/solution_test.rs:95: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:67:30

---- 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:67:30

---- solution_test::fib_split_n_symmetric_ascii_exact stdout ----
thread 'solution_test::fib_split_n_symmetric_ascii_exact' panicked at 'Not enough symbols', src/lib.rs:151:9

---- 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:121:30

---- 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:121:30

---- solution_test::fib_split_n_symmetric_zero stdout ----
thread 'solution_test::fib_split_n_symmetric_zero' panicked at 'byte index 2 is out of bounds of ``', src/lib.rs:154:15

---- solution_test::fib_split_n_zero stdout ----
thread 'solution_test::fib_split_n_zero' panicked at 'byte index 7 is not a char boundary; it is inside 'р' (bytes 6..8) of `abcdгрозде`', src/lib.rs:67:30


failures:
    solution_test::fib_split_cyrillic
    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_ascii_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`

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

Илиян качи първо решение на 29.10.2024 10:55 (преди 11 месеца)