diff options
| Commit | dbdad31081f5fa27f0e1970b7b351d57700ebfc0 (patch) | |
|---|---|---|
| Author | 2024-10-28 23:33:14 +0200 | |
| Committer | 2024-10-28 23:33:14 +0200 | |
| Files | a72996ffb41fdc48e40ea032a304386da72d2dfb | |
| uni-projects-dbdad31081f5fa27f0e1970b7b351d57700ebfc0.tar uni-projects-dbdad31081f5fa27f0e1970b7b351d57700ebfc0.tar.gz uni-projects-dbdad31081f5fa27f0e1970b7b351d57700ebfc0.zip | ||
| Parent | a26fd61957a9b77b5dc3649a768c8625ef27b907 (diff) | |
[Rust] Add an almost complete implementation
3 files changed, 146 insertions, 0 deletions
| -rw-r--r-- | Year 4/Programming with Rust/hw01/Cargo.lock | 7 | ||||
| -rw-r--r-- | Year 4/Programming with Rust/hw01/Cargo.toml | 8 | ||||
| -rw-r--r-- | Year 4/Programming with Rust/hw01/src/lib.rs | 131 |
diff --git a/Year 4/Programming with Rust/hw01/Cargo.lock b/Year 4/Programming with Rust/hw01/Cargo.lock new file mode 100644 index 0000000..c9e400e --- /dev/null +++ b/Year 4/Programming with Rust/hw01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hw01" +version = "0.1.0" diff --git a/Year 4/Programming with Rust/hw01/Cargo.toml b/Year 4/Programming with Rust/hw01/Cargo.toml new file mode 100644 index 0000000..a91917d --- /dev/null +++ b/Year 4/Programming with Rust/hw01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "hw01" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/Year 4/Programming with Rust/hw01/src/lib.rs b/Year 4/Programming with Rust/hw01/src/lib.rs new file mode 100644 index 0000000..97e921b --- /dev/null +++ b/Year 4/Programming with Rust/hw01/src/lib.rs @@ -0,0 +1,131 @@ +pub struct FibIter { + f0: u32, + f1: u32, +} + +impl FibIter { + pub fn new() -> FibIter { + FibIter { f0: 1, f1: 1, } + } + + pub fn next(&mut self) -> u32 { + let f0 = self.f0; + // 1 2 |-> 2 3 + self.f0 = self.f1; + self.f1 += f0; + f0 + } + + pub fn rev(self) -> RevFibIter { + RevFibIter { f0: self.f1 - self.f0, f1: self.f0 } + } + + fn inc(&mut self) -> Option<usize> { + Option::Some(self.next() as usize) + } +} + +struct Split { + start: usize, + end: usize, + count: u32, + fib: Option<FibIter>, + rfib: Option<RevFibIter>, + n: i64, + symmetric: bool, +} + +impl Split { + fn new(n: u32, symmetric: bool) -> Split { + let mut fib = FibIter::new(); + Split { start: 0, end: fib.inc().unwrap(), count: 0, fib: Option::Some(fib), rfib: Option::None, n: n as i64, symmetric: symmetric } + } + + fn inc(&mut self) { + if self.n == 0 { + self.fib = Option::None; + self.rfib = Option::Some(self.fib.unwrap().rev()); + self.n -= 1; + } + + self.start = self.end; + self.end += (if self.n > 0 { self.fib.as_mut().unwrap().inc() } else { self.rfib.as_mut().unwrap().inc() }).unwrap(); + self.count += 1; + + self.n -= self.symmetric as i64; + } +} + +fn _fib_split(text: &str, n: u32, panic: bool, symmetric: bool) -> (Vec<String>, &str) { + let mut out: Vec<String> = Vec::new(); + + let mut sp = Split::new(n, symmetric); + // panic -> sp.count < n + while sp.end <= text.len() && (!panic || sp.count < n) { + out.push(text[sp.start..sp.end].to_string()); + sp.inc(); + } + + if panic && sp.count < n { + panic!("Split into {} our of {} words!", sp.count, n); + } + + (out, &text[sp.start..]) +} + +pub fn fib_split(text: &str) -> Vec<String> { + _fib_split(text, 0, false, false).0 +} + +pub fn fib_split_n(text: &str, n: u32) -> (Vec<String>, &str) { + _fib_split(text, n, true, false) +} + +pub struct RevFibIter { + f0: u32, + f1: u32, +} + +impl RevFibIter { + pub fn next(&mut self) -> Option<u32> { + if self.f1 == 1 { + return Option::None; + } + + let f0 = self.f0; + self.f0 = self.f1 - self.f0; + self.f1 = f0; + + Option::Some(self.f1) + } + + fn inc(&mut self) -> Option<usize> { + let val = self.next(); + match val { + Option::None => Option::None, + Option::Some(_) => Option::Some(val.unwrap() as usize), + } + } +} + +pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) { + _fib_split(text, n, true, true) +} + + +#[test] +fn test_basic() { + let mut fib_iter = FibIter::new(); + fib_iter.next(); + + let mut rev_fib_iter: RevFibIter = fib_iter.rev(); + rev_fib_iter.next(); + + let _words: Vec<String> = fib_split("Fibonacci words!"); + + let (_words, _rest): (Vec<String>, &str) = + fib_split_n("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 1); + + let (_words, _rest): (Vec<String>, &str) = + fib_split_n_symmetric("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 1); +} |
