diff --git a/src/assignments/assignment02/small_exercises.rs b/src/assignments/assignment02/small_exercises.rs index bef4c5f..6c8bc6e 100644 --- a/src/assignments/assignment02/small_exercises.rs +++ b/src/assignments/assignment02/small_exercises.rs @@ -1,23 +1,29 @@ //! Small problems. -use std::iter; +use std::{cmp::min, iter}; const FAHRENHEIT_OFFSET: f64 = 32.0; const FAHRENHEIT_SCALE: f64 = 5.0 / 9.0; /// Converts Fahrenheit to Celsius temperature degree. pub fn fahrenheit_to_celsius(degree: f64) -> f64 { - todo!() + (degree - 32.0) * 5.0 / 9.0 } /// Capitalizes English alphabets (leaving the other characters intact). pub fn capitalize(input: String) -> String { - todo!() + input.to_ascii_uppercase() } /// Returns the sum of the given array. (We assume the absence of integer overflow.) pub fn sum_array(input: &[u64]) -> u64 { - todo!() + let mut result = 0; + + for i in input { + result += i; + } + + result } /// Given a non-negative integer, say `n`, return the smallest integer of the form `3^m` that's @@ -25,13 +31,27 @@ pub fn sum_array(input: &[u64]) -> u64 { /// /// For instance, up3(6) = 9, up3(9) = 9, up3(10) = 27. (We assume the absence of integer overflow.) pub fn up3(n: u64) -> u64 { - todo!() + let mut value = 1; + + while value < n { + value *= 3; + } + + value } /// Returns the greatest common divisor (GCD) of two non-negative integers. (We assume the absence /// of integer overflow.) pub fn gcd(lhs: u64, rhs: u64) -> u64 { - todo!() + let (mut a, mut b) = if lhs >= rhs { (lhs, rhs) } else { (rhs, lhs) }; + + while b != 0 { + let r = a % b; + a = b; + b = r; + } + + a } /// Returns the array of nC0, nC1, nC2, ..., nCn, where nCk = n! / (k! * (n-k)!). (We assume the @@ -40,7 +60,24 @@ pub fn gcd(lhs: u64, rhs: u64) -> u64 { /// Consult for computation of binomial /// coefficients without integer overflow. pub fn chooses(n: u64) -> Vec { - todo!() + let mut result = vec![1]; + + for k in 1..=n { + let last = *result.last().unwrap(); + let numerator = n + 1 - k; + + let new_value = if numerator % k == 0 { + last * ((numerator) / k) + } else if last % k == 0 { + (last / k) * numerator + } else { + let gcd_last = gcd(last, k); + (last / gcd_last) * (numerator / (k / gcd_last)) + }; + result.push(new_value); + } + + result } /// Returns the "zip" of two vectors. @@ -48,5 +85,12 @@ pub fn chooses(n: u64) -> Vec { /// For instance, `zip(vec![1, 2, 3], vec![4, 5])` equals to `vec![(1, 4), (2, 5)]`. Here, `3` is /// ignored because it doesn't have a partner. pub fn zip(lhs: Vec, rhs: Vec) -> Vec<(u64, u64)> { - todo!() + let length = min(lhs.len(), rhs.len()); + let mut result: Vec<(u64, u64)> = Vec::new(); + + for i in 0..length { + result.push((lhs[i], rhs[i])); + } + + result } diff --git a/src/assignments/assignment02/vec_and_mat.rs b/src/assignments/assignment02/vec_and_mat.rs index b2b6321..c3dc9a7 100644 --- a/src/assignments/assignment02/vec_and_mat.rs +++ b/src/assignments/assignment02/vec_and_mat.rs @@ -44,7 +44,12 @@ impl Mul for Mat2 { /// Consult fn mul(self, rhs: Mat2) -> Self::Output { - todo!() + Mat2 { + a: self.a * rhs.a + self.b * rhs.c, + b: self.a * rhs.b + self.b * rhs.d, + c: self.c * rhs.a + self.d * rhs.c, + d: self.c * rhs.b + self.d * rhs.d, + } } } @@ -55,21 +60,30 @@ impl Mul for Mat2 { /// /// Consult fn mul(self, rhs: Vec2) -> Self::Output { - todo!() + Vec2 { + a: self.a * rhs.a + self.b * rhs.b, + b: self.c * rhs.a + self.d * rhs.b, + } } } impl Mat2 { /// Calculates the power of matrix. fn power(self, power: u64) -> Mat2 { - todo!() + let mut result = Mat2::new(); + + for i in 0..power { + result = result * self; + } + + result } } impl Vec2 { /// Gets the upper value of vector. fn get_upper(self) -> u64 { - todo!() + self.a } } @@ -120,7 +134,14 @@ impl FMat2 { /// ); /// ``` pub fn inverse(self) -> Self { - todo!() + let det = self.a * self.d - self.b * self.c; + + FMat2 { + a: self.d / det, + b: -self.b / det, + c: -self.c / det, + d: self.a / det, + } } }