Решение на Думи на Фибоначи от Петър Христов

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

Към профила на Петър Христов

Резултати

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

Код

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 temp = self.current;
self.current = self.next;
self.next = self.current + temp;
temp
}
pub fn rev(self) -> RevFibIter {
RevFibIter {
current: self.next,
next: self.current,
}
}
}
pub struct RevFibIter {
current: u32,
next: u32,
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
if self.current > self.next {
let temp = self.current;
self.current = self.next;
self.next = temp - self.current;
return Some(self.current);
}
None
}
}
pub fn fib_split(t: &str) -> Vec<String> {
let mut text = t;
let mut fibo_seq = FibIter::new();
let mut result: Vec<String> = vec![];
while !(text.is_empty()) {
let curr_num: usize = fibo_seq.next() as usize;
if curr_num > text.len() {
result.push(text.to_string());
break;
}
let till_fib = &text[..curr_num];
result.push(till_fib.to_string());
text = &text[curr_num..];
}
result
}
pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) {
let mut fibo_seq = FibIter::new();
let mut result_till_n = vec![];
let mut text = text;
for _ in 1..(n + 1) {
let curr: usize = fibo_seq.current as usize;
if curr > text.len() {
result_till_n.push(text.to_string());
break;
}
result_till_n.push(text[..curr].to_string());
text = &text[curr..];
fibo_seq.next();
}
(result_till_n, text)
}
pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) {
let mut fibo_seq = FibIter::new();
let mut result_till_n = vec![];
let mut text = text;
for _ in 0..n {
let curr: usize = fibo_seq.current as usize;
if curr > text.len() {
result_till_n.push(text.to_string());
break;
}
result_till_n.push(text[..curr].to_string());
text = &text[curr..];
fibo_seq.next();
}
let mut fibo_seq_rev = fibo_seq.rev();
fibo_seq_rev.next();
fibo_seq_rev.next();
for _ in 0..n {
let curr: usize = fibo_seq_rev.current as usize;
if curr > text.len() {
result_till_n.push(text.to_string());
break;
}
result_till_n.push(text[..curr].to_string());
text = &text[curr..];
fibo_seq_rev.next();
}
(result_till_n, text)
}

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

Compiling solution v0.1.0 (/tmp/d20241104-1739405-j3e9ls/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 ... 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_symmetric_ascii ... ok
test solution_test::fib_split_n_cyrillic_panic ... ok
test solution_test::fib_split_n_symmetric_ascii_exact ... ok
test solution_test::fib_split_n_symmetric_ascii_panic ... FAILED
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 ... 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:58:25
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:77: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:77:28

---- solution_test::fib_split_n_symmetric_ascii_panic stdout ----
thread 'solution_test::fib_split_n_symmetric_ascii_panic' panicked at 'assertion failed: catch_unwind(|| fib_split_n_symmetric(\"\", 1)).is_err()', tests/solution_test.rs:153:5

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

---- solution_test::rev_fib_iter stdout ----
thread 'solution_test::rev_fib_iter' panicked at 'assertion failed: `(left == right)`
  left: `Some(21)`,
 right: `Some(13)`', tests/solution_test.rs:28:5


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_panic
    solution_test::fib_split_n_symmetric_cyrillic
    solution_test::fib_split_n_symmetric_cyrillic_exact
    solution_test::rev_fib_iter

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

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

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

Петър качи първо решение на 27.10.2024 23:32 (преди 11 месеца)

Внимавай със сигнатурите на функциите. FibIter::rev се очаква да приема self, а не &mut self. Трябва да го оправиш, защото в момента кода ти не се компилира заедно с пълния тест и получаваш 0 точки.

Петър качи решение на 28.10.2024 19:59 (преди 11 месеца)

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 temp = self.current;
self.current = self.next;
self.next = self.current + temp;
temp
}
- pub fn rev(&mut self) -> RevFibIter {
- if self.next > self.current {
- let temp = self.next;
- self.next = self.current;
- self.current = temp - self.current;
- }
-
+ pub fn rev(self) -> RevFibIter {
RevFibIter {
- current: self.current,
- next: self.next,
+ current: self.next,
+ next: self.current,
}
}
}
pub struct RevFibIter {
current: u32,
next: u32,
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
- if self.next > self.current {
- let temp = self.next;
- self.next = self.current;
- self.current = temp - self.next;
+ if self.current > self.next {
+ let temp = self.current;
+ self.current = self.next;
+ self.next = temp - self.current;
return Some(self.current);
}
None
}
}
-pub fn fib_split(mut text: &str) -> Vec<String> {
+pub fn fib_split(t: &str) -> Vec<String> {
+ let mut text = t;
let mut fibo_seq = FibIter::new();
+
let mut result: Vec<String> = vec![];
while !(text.is_empty()) {
let curr_num: usize = fibo_seq.next() as usize;
if curr_num > text.len() {
result.push(text.to_string());
break;
}
let till_fib = &text[..curr_num];
result.push(till_fib.to_string());
text = &text[curr_num..];
}
result
}
-pub fn fib_split_n(mut text: &str, n: u32) -> (Vec<String>, &str) {
+pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) {
let mut fibo_seq = FibIter::new();
let mut result_till_n = vec![];
+ let mut text = text;
for _ in 1..(n + 1) {
let curr: usize = fibo_seq.current as usize;
+ if curr > text.len() {
+ result_till_n.push(text.to_string());
+ break;
+ }
+
result_till_n.push(text[..curr].to_string());
text = &text[curr..];
fibo_seq.next();
}
(result_till_n, text)
}
-pub fn fib_split_n_symmetric(mut text: &str, n: u32) -> (Vec<String>, &str) {
+pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) {
let mut fibo_seq = FibIter::new();
let mut result_till_n = vec![];
- for _ in 1..(n + 1) {
+ let mut text = text;
+ for _ in 0..n {
let curr: usize = fibo_seq.current as usize;
+ if curr > text.len() {
+ result_till_n.push(text.to_string());
+ break;
+ }
+
result_till_n.push(text[..curr].to_string());
text = &text[curr..];
fibo_seq.next();
}
let mut fibo_seq_rev = fibo_seq.rev();
+ fibo_seq_rev.next();
+ fibo_seq_rev.next();
for _ in 0..n {
let curr: usize = fibo_seq_rev.current as usize;
+ if curr > text.len() {
+ result_till_n.push(text.to_string());
+ break;
+ }
+
result_till_n.push(text[..curr].to_string());
text = &text[curr..];
fibo_seq_rev.next();
}
(result_till_n, text)
}