diff --git a/codes/rust_lang/part08/src/big_int.rs b/codes/rust_lang/part08/src/big_int.rs deleted file mode 100644 index d52815b485df22c780b8d2363d7b32da3384521f..0000000000000000000000000000000000000000 --- a/codes/rust_lang/part08/src/big_int.rs +++ /dev/null @@ -1,117 +0,0 @@ -use crate::minimum::Minimum; - -/// Larger ints based on a [Vec] of [u8] to repensent arbitrary lengthy numbers. -/// The number has a sign as well. -pub struct BigInt { - /// The data contains the unsigned integers that are read from right to left - /// The number 1337 is stored as vec![7, 3, 3, 1]. Each number must be in the range [0,9] - /// and no trailing 0s are allowed. - data: Vec<u8>, - /// Contains the sign of the number +/-1; - sign: i8, -} - -impl BigInt { - /// Tries to create a new [BigInt]. If the number is valid it returns - /// an Ok(BigInt) an Error otherwise. - /// - /// # Examples - /// - /// ``` - /// use part08::big_int::BigInt; - /// let num = BigInt::try_new(vec![1, 2, 3, 4], 1); - /// assert!(num.is_ok()); - /// let num = BigInt::try_new(vec![1, 2, 3, 4], -1); - /// assert!(num.is_ok()); - /// let num = BigInt::try_new(vec![1, 2, 3, 4], 10); - /// assert!(num.is_err()); - /// let num = BigInt::try_new(vec![1, 2, 3, 4], -10); - /// assert!(num.is_err()); - /// ``` - /// - pub fn try_new(data: Vec<u8>, sign: i8) -> Result<Self, String> { - // EXERCISE: - // We don't check for trailing 0s but maybe this could be an exercise. - // Also we should check numbers are between 0 and 9. - if sign == 1 || sign == -1 { - Ok(BigInt { data, sign }) - } else { - Err(String::from("Invalid sign.")) - } - } -} - -impl Clone for BigInt { - fn clone(&self) -> Self { - BigInt { - data: self.data.clone(), - sign: self.sign, - } - } -} - -impl Minimum for BigInt { - // EXERCISE: Correct this function by using clippy and let it guide you. - // Get inspiration from Display to compute the Minimum - fn min(self, rhs: Self) -> Self { - if self.sign < rhs.sign { - return self; - } else if self.sign > rhs.sign { - return rhs; - } - if self.data.len() < rhs.data.len() { - return self; - } else if self.data.len() > rhs.data.len() { - return rhs; - } - for (l, r) in self.data.iter().rev().zip(rhs.data.iter().rev()) { - let ls = (*l as i8) * self.sign; - let rs = (*r as i8) * self.sign; - if ls < rs { - return self; - } else if ls > rs { - return rhs; - } - } - self - } -} - -impl PartialEq for BigInt { - fn eq(&self, other: &Self) -> bool { - if self.sign == other.sign && self.data.len() == other.data.len() { - self.data - .iter() - .zip(other.data.iter()) - .try_fold(true, |_, (l, r)| if l == r { Some(true) } else { None }) - .is_some() - } else { - false - } - } -} - -impl std::fmt::Display for BigInt { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - // This could be replaced by an `?` - if self.sign == -1 { - if let Err(e) = write!(f, "-") { - return Err(e); - } - } - - // This could be replaced by an `?` - let res = self - .data - .iter() - .rev() - .try_fold((), |_, t| write!(f, "{}", t)); - res - } -} - -// EXERCISE: write tests -// EXERCISE: Modify Minimum trait to take references? - -#[cfg(test)] -mod tests {} diff --git a/codes/rust_lang/part08/src/main.rs b/codes/rust_lang/part08/src/main.rs index 4a028c00099da36cb7f45343536e6c24076e08ee..4fd0479b6ab6e773f7552e93276b00b6e1e0623c 100644 --- a/codes/rust_lang/part08/src/main.rs +++ b/codes/rust_lang/part08/src/main.rs @@ -6,7 +6,7 @@ fn main() -> Result<(), String> { Ok(tab) => tab, Err(s) => return Err(s), }; - println!("Among the Big Ints in the list:"); + println!("Among the custom ints in the list:"); io::print_tab_CustomInt(&tab); // There are alternatives to access fields of tuples let min = find_min(&tab);