Решение на Думи на Фибоначи от Александър Терезов

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

Към профила на Александър Терезов

Резултати

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

Код

pub struct FibIter {
current: u32,
next: u32,
}
impl FibIter {
pub fn new() -> FibIter {
FibIter {
current: 1,
next: 1,
}
}
pub fn next(&mut self) -> u32 {
let to_return = self.current;
self.current = self.next;
self.next = to_return + self.current;
to_return
}
pub fn rev(self) -> RevFibIter {
RevFibIter {
current: self.next - self.current,
next: self.current - (self.next - self.current),
}
}
}
pub struct RevFibIter {
current: u32,
next: u32,
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
let to_return = self.current;
if self.current == 0 && self.next == 1 {
return None;
}
self.current = self.next;
self.next = to_return - self.current;
Some(to_return)
}
}
pub fn fib_split(text: &str) -> Vec<String> {
let mut vec = Vec::new();
let mut fib_iter = FibIter::new();
let mut current: u32 = 0;
let mut next: u32 = fib_iter.next();
while current + next < text.len().try_into().unwrap() {
let slice: &str = &text[current as usize..(current as usize + next as usize)];
vec.push(slice.to_string());
current += next;
next = fib_iter.next();
}
let slice: &str = &text[current as usize..text.len()];
vec.push(slice.to_string());
vec
}
pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) {
let mut vec = Vec::new();
let mut fib_iter = FibIter::new();
let mut current: u32 = 0;
let mut next: u32 = fib_iter.next();
for _ in 0..n {
if current + next >= text.len().try_into().unwrap() {
panic!("Text not long enough!");
}
let slice: &str = &text[current as usize..(current as usize + next as usize)];
vec.push(slice.to_string());
current += next;
next = fib_iter.next();
}
(vec, &text[current as usize..text.len()])
}
pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) {
let mut vec = Vec::new();
let mut fib_iter = FibIter::new();
let mut current: u32 = 0;
let mut next: u32 = fib_iter.next();
for _ in 0..n {
if current + next >= text.len().try_into().unwrap() {
panic!("Text not long enough!");
}
let slice: &str = &text[current as usize..(current as usize + next as usize)];
vec.push(slice.to_string());
current += next;
next = fib_iter.next();
}
let mut rev_iter = fib_iter.rev();
rev_iter.next();
for _ in 0..n {
next = rev_iter.next().expect("Expected number.");
println!("{}, {}", current, next);
if current + next >= text.len().try_into().unwrap() {
panic!("Text not long enough!");
}
let slice: &str = &text[current as usize..(current as usize + next as usize)];
vec.push(slice.to_string());
current += next;
//next = rev_iter.next().expect("Expected number.");
}
(vec, &text[current as usize..text.len()])
}

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

Compiling solution v0.1.0 (/tmp/d20241104-1739405-1okc81f/solution)
    Finished test [unoptimized + debuginfo] target(s) in 0.99s
     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 ... FAILED
test solution_test::fib_split_n_ascii_panic ... ok
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 ... ok
test solution_test::fib_split_n_zero ... ok
test solution_test::rev_fib_iter ... ok
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:59:28
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::fib_split_n_ascii_exact stdout ----
thread 'solution_test::fib_split_n_ascii_exact' panicked at 'Text not long enough!', src/lib.rs:81:13

---- 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:84:28

---- 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:84:28

---- solution_test::fib_split_n_symmetric_ascii_exact stdout ----
4, 2
6, 1
7, 1
thread 'solution_test::fib_split_n_symmetric_ascii_exact' panicked at 'Text not long enough!', src/lib.rs:120:13

---- 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:106:28

---- 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:106:28


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

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

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

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

Александър качи първо решение на 29.10.2024 18:03 (преди 11 месеца)

Александър качи решение на 31.10.2024 17:33 (преди 11 месеца)

-struct FibIter {
+pub struct FibIter {
current: u32,
next: u32,
}
impl FibIter {
pub fn new() -> FibIter {
FibIter {
current: 1,
next: 1,
}
}
pub fn next(&mut self) -> u32 {
let to_return = self.current;
self.current = self.next;
self.next = to_return + self.current;
to_return
}
pub fn rev(self) -> RevFibIter {
RevFibIter {
current: self.next - self.current,
next: self.current - (self.next - self.current),
}
}
}
pub struct RevFibIter {
current: u32,
next: u32,
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
let to_return = self.current;
if self.current == 0 && self.next == 1 {
return None;
}
self.current = self.next;
self.next = to_return - self.current;
Some(to_return)
}
}
pub fn fib_split(text: &str) -> Vec<String> {
let mut vec = Vec::new();
let mut fib_iter = FibIter::new();
let mut current: u32 = 0;
let mut next: u32 = fib_iter.next();
while current + next < text.len().try_into().unwrap() {
let slice: &str = &text[current as usize..(current as usize + next as usize)];
vec.push(slice.to_string());
current += next;
next = fib_iter.next();
}
let slice: &str = &text[current as usize..text.len()];
vec.push(slice.to_string());
vec
}
pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) {
let mut vec = Vec::new();
let mut fib_iter = FibIter::new();
let mut current: u32 = 0;
let mut next: u32 = fib_iter.next();
for _ in 0..n {
if current + next >= text.len().try_into().unwrap() {
panic!("Text not long enough!");
}
let slice: &str = &text[current as usize..(current as usize + next as usize)];
vec.push(slice.to_string());
current += next;
next = fib_iter.next();
}
(vec, &text[current as usize..text.len()])
}
pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) {
let mut vec = Vec::new();
let mut fib_iter = FibIter::new();
let mut current: u32 = 0;
let mut next: u32 = fib_iter.next();
for _ in 0..n {
if current + next >= text.len().try_into().unwrap() {
panic!("Text not long enough!");
}
let slice: &str = &text[current as usize..(current as usize + next as usize)];
vec.push(slice.to_string());
current += next;
next = fib_iter.next();
}
let mut rev_iter = fib_iter.rev();
rev_iter.next();
for _ in 0..n {
next = rev_iter.next().expect("Expected number.");
println!("{}, {}", current, next);
if current + next >= text.len().try_into().unwrap() {
panic!("Text not long enough!");
}
let slice: &str = &text[current as usize..(current as usize + next as usize)];
vec.push(slice.to_string());
current += next;
//next = rev_iter.next().expect("Expected number.");
}
(vec, &text[current as usize..text.len()])
}