From 2e60eea75324932fa8b6a267a5bec3b79f61487f Mon Sep 17 00:00:00 2001 From: Seungmin Jeon Date: Wed, 31 Aug 2022 10:48:07 +0900 Subject: [PATCH] Add assignment 2 --- src/assignments/assignment02.rs | 140 +++++++++++++++++++++- src/assignments/assignment02_grade.rs | 164 ++++++++++++++++++++++++++ src/assignments/mod.rs | 2 + 3 files changed, 305 insertions(+), 1 deletion(-) diff --git a/src/assignments/assignment02.rs b/src/assignments/assignment02.rs index d0f8c8d..99d36f1 100644 --- a/src/assignments/assignment02.rs +++ b/src/assignments/assignment02.rs @@ -1,3 +1,141 @@ //! Assignment 2: Mastering common programming concepts (1/2). +//! +//! The primary goal of this assignment is to re-learn the common programming concepts in Rust, especially those in the Rust Book chapters 3 and 5. +//! Please make sure you're comfortable with the concepts to proceed on to the next assignments. +//! +//! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-02.sh` works fine. +//! See `assignment02_grade.rs` and `/scripts/grade-02.sh` for the test script. -// TODO: use exercises in +use std::ops::Mul; + +const FARHENHEIT_OFFSET: f64 = 32.0; +const FARHENHEIT_SCALE: f64 = 5.0 / 9.0; + +/// Converts Farenheit to Celcius temperature degree. +pub(crate) fn farhenheit_to_celcius(degree: f64) -> f64 { + todo!() +} + +/// Captializes English alphabets (leaving the other characters intact). +pub(crate) fn capitalize(input: String) -> String { + todo!() +} + +/// Returns the sume of the given array. (We assume the absence of integer overflow.) +pub(crate) fn sum_array(input: &[u64]) -> u64 { + todo!() +} + +/// Given a non-negative integer, say `n`, return the smallest integer of the form `3^m` that's greater than or equal to `n`. +/// +/// For instance, up3(6) = 9, up3(9) = 9, up3(10) = 27. (We assume the absence of integer overflow.) +pub(crate) fn up3(n: u64) -> u64 { + todo!() +} + +/// Returns the greatest common divisor (GCD) of two non-negative integers. (We assume the absence of integer overflow.) +pub(crate) fn gcd(lhs: u64, rhs: u64) -> u64 { + todo!() +} + +/// Returns the array of nC0, nC1, nC2, ..., nCn, where nCk = n! / (k! * (n-k)!). +pub(crate) fn chooses(n: u64) -> Vec { + todo!() +} + +/// Returns the "zip" of two vectors. +/// +/// 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(crate) fn zip(lhs: Vec, rhs: Vec) -> Vec<(u64, u64)> { + todo!() +} + +/// 2x2 matrix of the following configuration: +/// +/// a, b +/// c, d +#[derive(Debug, Clone, Copy)] +struct Mat2 { + a: u64, + b: u64, + c: u64, + d: u64, +} + +/// 2x1 matrix of the following configuration: +/// +/// a +/// b +#[derive(Debug, Clone, Copy)] +struct Vec2 { + a: u64, + b: u64, +} + +impl Mat2 { + /// Creates an identity matrix. + fn new() -> Self { + Self { + a: 1, + b: 0, + c: 0, + d: 1, + } + } +} + +impl Mul for Mat2 { + type Output = Mat2; + + fn mul(self, rhs: Mat2) -> Self::Output { + todo!() + } +} + +impl Mul for Mat2 { + type Output = Vec2; + + fn mul(self, rhs: Vec2) -> Self::Output { + todo!() + } +} + +impl Mat2 { + /// Calculates the power of matrix. + fn power(self, power: u64) -> Mat2 { + todo!() + } +} + +impl Vec2 { + /// Gets the upper value of vector. + fn get_upper(self) -> u64 { + todo!() + } +} + +/// The matrix used for calculating Fibonacci numbers. +const FIBONACCI_MAT: Mat2 = Mat2 { + a: 1, + b: 1, + c: 1, + d: 0, +}; + +/// The vector used for calculating Fibonacci numbers. +const FIBONACCI_VEC: Vec2 = Vec2 { a: 1, b: 0 }; + +/// Calculates the Fibonacci number. +/// +/// Consult for matrix computation of Fibonacci numbers. +pub(crate) fn fibonacci(n: u64) -> u64 { + (FIBONACCI_MAT.power(n) * FIBONACCI_VEC).get_upper() +} + +/// Writes down the lyrics of "twelve days of christmas". +/// +/// Hint: Google the song title for lyrics and look at the test code for the expected result. +pub(crate) fn twelve_days_of_christmas_lyrics() -> String { + todo!() +} diff --git a/src/assignments/assignment02_grade.rs b/src/assignments/assignment02_grade.rs index 631b61d..a9af1e4 100644 --- a/src/assignments/assignment02_grade.rs +++ b/src/assignments/assignment02_grade.rs @@ -1,4 +1,168 @@ #[cfg(test)] mod test { use super::super::assignment02::*; + + #[test] + fn test_farenheit() { + assert_eq!(farhenheit_to_celcius(32.0), 0.0); + assert_eq!(farhenheit_to_celcius(212.0), 100.0); + } + + #[test] + fn test_capitalize() { + assert_eq!( + capitalize(String::from("aAbbBcccCddddD❤한글과✓")), + String::from("AABBBCCCCDDDDD❤한글과✓"), + ); + } + + #[test] + fn test_up3() { + assert_eq!(up3(6), 9); + assert_eq!(up3(9), 9); + assert_eq!(up3(10), 27); + assert_eq!(up3(1_000_000), 1_594_323); + } + + #[test] + fn test_sum_array() { + assert_eq!(sum_array(&[1, 2, 3, 4, 5, 100]), 115); + } + + #[test] + fn test_chooses() { + assert_eq!(chooses(5), vec![1, 5, 10, 10, 5, 1]); + assert_eq!(chooses(6), vec![1, 6, 15, 20, 15, 6, 1]); + } + + #[test] + fn test_zip() { + assert_eq!(zip(vec![1, 2], vec![4, 5]), vec![(1, 4), (2, 5)]); + assert_eq!(zip(vec![1, 2, 3], vec![4, 5]), vec![(1, 4), (2, 5)]); + assert_eq!(zip(vec![1, 2], vec![4, 5, 6]), vec![(1, 4), (2, 5)]); + assert_eq!(zip(vec![], vec![4, 5]), vec![]); + } + + #[test] + fn test_fibonacci() { + assert_eq!(fibonacci(0), 1); + assert_eq!(fibonacci(1), 1); + assert_eq!(fibonacci(2), 2); + assert_eq!(fibonacci(3), 3); + assert_eq!(fibonacci(4), 5); + assert_eq!(fibonacci(5), 8); + assert_eq!(fibonacci(6), 13); + assert_eq!(fibonacci(7), 21); + assert_eq!(fibonacci(50), 20365011074); + } + + #[test] + fn test_lyrics() { + assert_eq!(twelve_days_of_christmas_lyrics(), LYRICS) + } + + const LYRICS: &str = r#"On the first day of Christmas, my true love sent to me +A partridge in a pear tree + +On the second day of Christmas, my true love sent to me +Two turtledoves +And a partridge in a pear tree + +On the third day of Christmas, my true love sent to me +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the fourth day of Christmas, my true love sent to me +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the fifth day of Christmas, my true love sent to me +Five gold rings (five golden rings) +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the sixth day of Christmas, my true love sent to me +Six geese a-laying +Five gold rings (five golden rings) +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the seventh day of Christmas, my true love sent to me +Seven swans a-swimming +Six geese a-laying +Five gold rings (five golden rings) +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the eighth day of Christmas, my true love sent to me +Eight maids a-milking +Seven swans a-swimming +Six geese a-laying +Five gold rings (five golden rings) +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the ninth day of Christmas, my true love sent to me +Nine ladies dancing +Eight maids a-milking +Seven swans a-swimming +Six geese a-laying +Five gold rings (five golden rings) +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the tenth day of Christmas, my true love sent to me +Ten lords a-leaping +Nine ladies dancing +Eight maids a-milking +Seven swans a-swimming +Six geese a-laying +Five gold rings (five golden rings) +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the eleventh day of Christmas, my true love sent to me +I sent eleven pipers piping +Ten lords a-leaping +Nine ladies dancing +Eight maids a-milking +Seven swans a-swimming +Six geese a-laying +Five gold rings (five golden rings) +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +On the twelfth day of Christmas, my true love sent to me +Twelve drummers drumming +Eleven pipers piping +Ten lords a-leaping +Nine ladies dancing +Eight maids a-milking +Seven swans a-swimming +Six geese a-laying +Five gold rings (five golden rings) +Four calling birds +Three French hens +Two turtledoves +And a partridge in a pear tree + +And a partridge in a pear tree +"#; } diff --git a/src/assignments/mod.rs b/src/assignments/mod.rs index 1fdfbd8..ce815f0 100644 --- a/src/assignments/mod.rs +++ b/src/assignments/mod.rs @@ -3,3 +3,5 @@ mod assignment01; mod assignment01_grade; +mod assignment02; +mod assignment02_grade;