Решение на Думи на Фибоначи от Константин Кондов
Към профила на Константин Кондов
Резултати
- 16 точки от тестове
- 0 бонус точки
- 16 точки общо
- 16 успешни тест(а)
- 4 неуспешни тест(а)
Код
#[derive(Debug)]
pub struct FibIter {
base: u32,
base_next: u32,
}
impl FibIter {
pub fn new() -> FibIter {
FibIter {base: 0, base_next: 0}
}
}
impl FibIter {
pub fn next(&mut self ) -> u32 {
//I have set those two to zero at the start, so I can achieve F0 = 1 and F1 = 1
if self.base == 0 && self.base_next == 0 {
self.base = 0;
self.base_next = 1;
1
}
else {
let fib_next: u32 = self.base + self.base_next;
self.base = self.base_next;
self.base_next = fib_next;
fib_next
}
}
}
impl FibIter {
pub fn rev(self) -> RevFibIter {
//setting to the next fib number, so that the RevFibIter::next() returns Fn-1 as well, in order to achieve symetry
let fib_next: u32 = self.base + self.base_next;
RevFibIter{base: self.base_next, base_next: fib_next}
}
}
#[derive(Debug)]
pub struct RevFibIter {
base: u32,
base_next: u32,
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
if self.base == 0 && self.base_next == 0 {
return None;
}
if self.base == 0 && self.base_next == 1 {
self.base = 0;
self.base_next = 0;
return Some(1 as u32);
}
let new_base = self.base_next - self.base;
self.base_next = self.base;
self.base = new_base;
return Some(self.base_next)
}
}
pub fn fib_split(text: &str) -> Vec<String> {
let chars: Vec<char> = text.chars().collect();
let text_size: usize = chars.len();
let mut index: usize = 0;
let mut vec: Vec<String> = Vec::new();
let mut fib_iter: FibIter = FibIter::new();
loop {
//each loop we will get the next number in fib sequence
let mut fib_next: u32 = fib_iter.next();
let mut fib_str: String = String::new();
//if we can create a string from index + fib_next position
if index + (fib_next as usize) <= (text_size - 1) {
//implemented with upside counter, when fib_next reaches 0 we have created the word
while fib_next != 0 {
fib_str.push(chars[index]);
index += 1;
fib_next -= 1;
}
vec.push(fib_str);
}
//when we dont have enough characters to create the next word, we just add the rest
else if index + (fib_next as usize) > (text_size - 1) && (index <= (text_size - 1)){
while index <= (text_size - 1) {
fib_str.push(chars[index]);
index += 1;
}
vec.push(fib_str);
break;
}
}
return vec;
}
pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, String) {
let chars: Vec<char> = text.chars().collect();
let text_size: usize = chars.len();
let mut fib_iter_check = FibIter::new();
let mut fib_n: u32 = 0;
//couting how many chars we need for every fib_str combined
let mut i: u32 = 0;
while i != n {
fib_n += fib_iter_check.next();
i+= 1;
}
//check
if fib_n > (text_size as u32) {
panic!("Not enough letters to achieve the operation");
}
let mut index: usize = 0;
let mut iterated_fibs: u32 = 0; // the number of fib string we have processed
let mut vec: Vec<String> = Vec::new();
let mut fib_iter: FibIter = FibIter::new();
while iterated_fibs != n {
let mut fib_next: u32 = fib_iter.next();
let mut fib_str: String = String::new();
//follows the same algoritm as in fib_split
while fib_next != 0 {
fib_str.push(chars[index]);
index += 1;
fib_next -= 1;
}
iterated_fibs += 1;
vec.push(fib_str);
}
//adding the rest of the string to the tuple
let mut fib_str_final: String = String::new();
while index <= text_size - 1{
fib_str_final.push(chars[index]);
index += 1;
}
(vec, fib_str_final)
}
pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, String) {
let chars: Vec<char> = text.chars().collect();
let text_size: usize = chars.len();
let mut fib_iter_check = FibIter::new();
let mut fib_n: u32 = 0;
//couting how many chars we need for every fib_str combined
let mut i: u32 = 0;
while i != n {
fib_n += fib_iter_check.next();
i+= 1;
}
//check
if (fib_n * 2) > (text_size as u32) {
panic!("Not enough letters to achieve the operation");
}
let mut index: usize = 0;
let mut iterated_fibs_normal: u32 = 0;
let mut vec: Vec<String> = Vec::new();
let mut fib_iter: FibIter = FibIter::new();
while iterated_fibs_normal != n {
let mut fib_next: u32 = fib_iter.next();
let mut fib_str: String = String::new();
while fib_next != 0 {
fib_str.push(chars[index]);
index += 1;
fib_next -= 1;
}
iterated_fibs_normal+= 1;
vec.push(fib_str);
}
iterated_fibs_normal = 0;
let mut fib_iter_rev: RevFibIter = fib_iter.rev();
while iterated_fibs_normal != n {
let mut fib_next: Option<u32> = fib_iter_rev.next();
let mut fib_str: String = String::new();
while fib_next != Some(0) {
fib_str.push(chars[index]);
match fib_next {
Some(ref mut num) => {
*num -= 1;
}
None => {
()
}
}
index += 1;
}
iterated_fibs_normal += 1;
vec.push(fib_str)
}
let mut fib_str_final: String = String::new();
while index <= text_size - 1{
fib_str_final.push(chars[index]);
index += 1;
}
(vec, fib_str_final)
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241104-1739405-5c3l0s/solution) Finished test [unoptimized + debuginfo] target(s) in 0.90s 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_cyrillic ... ok test solution_test::fib_split_empty ... FAILED test solution_test::fib_split_n_ascii ... ok test solution_test::fib_split_n_ascii_exact ... ok test solution_test::fib_split_n_cyrillic ... ok test solution_test::fib_split_n_ascii_panic ... ok test solution_test::fib_split_n_cyrillic_exact ... ok 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 ... ok test solution_test::fib_split_n_symmetric_cyrillic_exact ... ok 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 ... FAILED test solution_test::fib_split_n_zero ... FAILED test solution_test::rev_fib_iter_empty ... ok failures: ---- solution_test::fib_split_empty stdout ---- thread 'solution_test::fib_split_empty' panicked at 'attempt to subtract with overflow', src/lib.rs:92:43 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::fib_split_n_symmetric_zero stdout ---- thread 'solution_test::fib_split_n_symmetric_zero' panicked at 'attempt to subtract with overflow', src/lib.rs:249:20 ---- solution_test::rev_fib_iter stdout ---- thread 'solution_test::rev_fib_iter' panicked at 'assertion failed: `(left == right)` left: `Some(1)`, right: `None`', tests/solution_test.rs:35:5 ---- solution_test::fib_split_n_zero stdout ---- thread 'solution_test::fib_split_n_zero' panicked at 'attempt to subtract with overflow', src/lib.rs:167:20 failures: solution_test::fib_split_empty solution_test::fib_split_n_symmetric_zero solution_test::fib_split_n_zero solution_test::rev_fib_iter test result: FAILED. 16 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`