Решение на Думи на Фибоначи от Ясен Узунов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 20 успешни тест(а)
- 0 неуспешни тест(а)
Код
pub struct FibIter {
first: u32,
second: u32,
}
impl FibIter {
pub fn new() -> FibIter {
FibIter {first: 1, second: 1}
}
pub fn next(&mut self) -> u32 {
let first_copy = self.first;
self.first = self.second;
self.second = first_copy + self.second;
first_copy
}
}
pub fn fib_split(text: &str) -> Vec<String> {
let mut vec: Vec<String> = Vec::new();
let mut fib: FibIter = FibIter::new();
let mut len: usize = fib.next() as usize;
let mut start_index: usize = 0;
let chars: Vec<char> = text.chars().collect();
while start_index < chars.len() {
let end_index: usize = (start_index + len).min(chars.len());
let slice: String = chars[start_index..end_index].iter().collect();
vec.push(slice);
start_index += len;
len = fib.next() as usize;
}
vec
}
pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) {
let mut vec: Vec<String> = Vec::new();
let mut fib: FibIter = FibIter::new();
let mut len: usize = fib.next() as usize;
let mut start_index: usize = 0;
let chars: Vec<char> = text.chars().collect();
let mut bytes: usize = 0;
for _ in 0..n {
let end_index: usize = start_index + len;
if end_index > chars.len() {
panic!("don't know what to do!")
}
let slice: String = chars[start_index..end_index].iter().collect();
vec.push(slice);
for c in chars[start_index..end_index].iter() {
bytes += c.len_utf8();
}
start_index += len;
len = fib.next() as usize;
}
(vec, &text[bytes..])
}
impl FibIter {
pub fn rev(self) -> RevFibIter {
let before_first = self.second - self.first;
RevFibIter {second: self.second - self.first, first: self.first - before_first}
}
}
pub struct RevFibIter {
second: u32,
first: u32,
}
impl RevFibIter {
pub fn next(&mut self) -> Option<u32> {
if self.second == 0 {
return None;
}
let current: u32 = self.second;
let next_fib: u32 = if self.first > self.second { 0 } else {self.second - self.first};
self.second = self.first;
self.first = next_fib;
Some(current)
}
}
pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) {
let mut vec: Vec<String> = Vec::new();
let mut fib: FibIter = FibIter::new();
let mut len: usize = fib.next() as usize;
let mut start_index: usize = 0;
let mut end_index: usize = start_index + len;
let chars: Vec<char> = text.chars().collect();
let mut bytes: usize = 0;
for i in 0..n {
end_index = start_index + len;
if end_index > chars.len() {
panic!("don't know what to do!")
}
let slice: String = chars[start_index..end_index].iter().collect();
vec.push(slice);
for c in chars[start_index..end_index].iter() {
bytes += c.len_utf8();
}
start_index += len;
if i != n - 1 {
len = fib.next() as usize;
}
}
let mut fib_rev: RevFibIter = fib.rev();
let mut len_rev: usize = match fib_rev.next() {
Some(value) => value as usize,
None => 0,
};
for _ in (0..n).rev() {
end_index = start_index + len_rev;
if end_index > chars.len() {
panic!("don't know what to do!")
}
let slice: String = chars[start_index..end_index].iter().collect();
vec.push(slice);
for c in chars[start_index..end_index].iter() {
bytes += c.len_utf8();
}
start_index += len_rev;
len_rev = match fib_rev.next() {
Some(value) => value as usize,
None => 0,
} as usize;
}
(vec, &text[bytes..])
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241104-1739405-1rs3ad/solution) warning: value assigned to `end_index` is never read --> src/lib.rs:105:13 | 105 | let mut end_index: usize = start_index + len; | ^^^^^^^^^ | = help: maybe it is overwritten before being read? = note: `#[warn(unused_assignments)]` on by default warning: `solution` (lib) generated 1 warning Finished test [unoptimized + debuginfo] target(s) in 0.93s 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 ... ok 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_panic ... ok test solution_test::fib_split_n_cyrillic_exact ... 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 ... ok test solution_test::fib_split_n_zero ... ok test solution_test::rev_fib_iter ... ok test solution_test::rev_fib_iter_empty ... ok test result: ok. 20 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s