From bba6cd99797b507b0fa04690bace4ad1304b72bd Mon Sep 17 00:00:00 2001 From: "jungin.rhee" Date: Mon, 21 Aug 2023 11:52:05 +0000 Subject: [PATCH] polish --- src/assignments/assignment01/mod.rs | 4 +- .../{small_problems.rs => small_exercises.rs} | 0 ...lems_grade.rs => small_exercises_grade.rs} | 2 +- src/assignments/assignment02/mod.rs | 4 +- .../{small_problems.rs => small_exercises.rs} | 0 ...lems_grade.rs => small_exercises_grade.rs} | 2 +- src/assignments/assignment03/mod.rs | 4 +- .../{small_problems.rs => small_exercises.rs} | 0 ...lems_grade.rs => small_exercises_grade.rs} | 2 +- src/assignments/assignment06/mod.rs | 2 - src/assignments/assignment06/square_matrix.rs | 88 ------------------- .../assignment06/square_matrix_grade.rs | 72 --------------- .../assignment06/symbolic_differentiation.rs | 5 ++ src/assignments/assignment07/generator.rs | 10 ++- src/assignments/assignment07/hubo.rs | 84 ------------------ src/assignments/assignment07/hubo_grade.rs | 40 --------- src/assignments/assignment07/mod.rs | 2 - .../assignment07/small_exercises.rs | 2 +- src/assignments/assignment08/mod.rs | 4 +- .../{small_problems.rs => small_exercises.rs} | 0 ...lems_grade.rs => small_exercises_grade.rs} | 2 +- src/assignments/assignment11/graph_grade.rs | 4 +- src/assignments/assignment13/mod.rs | 4 +- .../{small_problems.rs => small_exercises.rs} | 0 ...lems_grade.rs => small_exercises_grade.rs} | 2 +- 25 files changed, 32 insertions(+), 307 deletions(-) rename src/assignments/assignment01/{small_problems.rs => small_exercises.rs} (100%) rename src/assignments/assignment01/{small_problems_grade.rs => small_exercises_grade.rs} (86%) rename src/assignments/assignment02/{small_problems.rs => small_exercises.rs} (100%) rename src/assignments/assignment02/{small_problems_grade.rs => small_exercises_grade.rs} (98%) rename src/assignments/assignment03/{small_problems.rs => small_exercises.rs} (100%) rename src/assignments/assignment03/{small_problems_grade.rs => small_exercises_grade.rs} (99%) delete mode 100644 src/assignments/assignment06/square_matrix.rs delete mode 100644 src/assignments/assignment06/square_matrix_grade.rs delete mode 100644 src/assignments/assignment07/hubo.rs delete mode 100644 src/assignments/assignment07/hubo_grade.rs rename src/assignments/assignment08/{small_problems.rs => small_exercises.rs} (100%) rename src/assignments/assignment08/{small_problems_grade.rs => small_exercises_grade.rs} (95%) rename src/assignments/assignment13/{small_problems.rs => small_exercises.rs} (100%) rename src/assignments/assignment13/{small_problems_grade.rs => small_exercises_grade.rs} (99%) diff --git a/src/assignments/assignment01/mod.rs b/src/assignments/assignment01/mod.rs index 9a022d3..5c279c8 100644 --- a/src/assignments/assignment01/mod.rs +++ b/src/assignments/assignment01/mod.rs @@ -8,5 +8,5 @@ //! //! Hint: -pub mod small_problems; -mod small_problems_grade; +pub mod small_exercises; +mod small_exercises_grade; diff --git a/src/assignments/assignment01/small_problems.rs b/src/assignments/assignment01/small_exercises.rs similarity index 100% rename from src/assignments/assignment01/small_problems.rs rename to src/assignments/assignment01/small_exercises.rs diff --git a/src/assignments/assignment01/small_problems_grade.rs b/src/assignments/assignment01/small_exercises_grade.rs similarity index 86% rename from src/assignments/assignment01/small_problems_grade.rs rename to src/assignments/assignment01/small_exercises_grade.rs index dbdd99e..3039dee 100644 --- a/src/assignments/assignment01/small_problems_grade.rs +++ b/src/assignments/assignment01/small_exercises_grade.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod test { - use crate::assignments::assignment01::small_problems::*; + use crate::assignments::assignment01::small_exercises::*; #[test] fn test_add_7_3() { diff --git a/src/assignments/assignment02/mod.rs b/src/assignments/assignment02/mod.rs index 86a15c7..90b3f2c 100644 --- a/src/assignments/assignment02/mod.rs +++ b/src/assignments/assignment02/mod.rs @@ -6,8 +6,8 @@ //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-02.sh` works fine. //! See `*_grade.rs` and `/scripts/grade-02.sh` for the test script. -pub mod small_problems; -mod small_problems_grade; +pub mod small_exercises; +mod small_exercises_grade; pub mod vec_and_mat; mod vec_and_mat_grade; diff --git a/src/assignments/assignment02/small_problems.rs b/src/assignments/assignment02/small_exercises.rs similarity index 100% rename from src/assignments/assignment02/small_problems.rs rename to src/assignments/assignment02/small_exercises.rs diff --git a/src/assignments/assignment02/small_problems_grade.rs b/src/assignments/assignment02/small_exercises_grade.rs similarity index 98% rename from src/assignments/assignment02/small_problems_grade.rs rename to src/assignments/assignment02/small_exercises_grade.rs index ae33ff1..cca1426 100644 --- a/src/assignments/assignment02/small_problems_grade.rs +++ b/src/assignments/assignment02/small_exercises_grade.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod test { - use crate::assignments::assignment02::small_problems::*; + use crate::assignments::assignment02::small_exercises::*; #[test] fn test_fahrenheit() { diff --git a/src/assignments/assignment03/mod.rs b/src/assignments/assignment03/mod.rs index be8a24c..76a311c 100644 --- a/src/assignments/assignment03/mod.rs +++ b/src/assignments/assignment03/mod.rs @@ -3,8 +3,8 @@ //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-03.sh` works fine. //! See `*_grade.rs` and `/scripts/grade-03.sh` for the test script. -pub mod small_problems; -mod small_problems_grade; +pub mod small_exercises; +mod small_exercises_grade; pub mod parse_shell; mod parse_shell_grade; diff --git a/src/assignments/assignment03/small_problems.rs b/src/assignments/assignment03/small_exercises.rs similarity index 100% rename from src/assignments/assignment03/small_problems.rs rename to src/assignments/assignment03/small_exercises.rs diff --git a/src/assignments/assignment03/small_problems_grade.rs b/src/assignments/assignment03/small_exercises_grade.rs similarity index 99% rename from src/assignments/assignment03/small_problems_grade.rs rename to src/assignments/assignment03/small_exercises_grade.rs index 21390ec..45c4c57 100644 --- a/src/assignments/assignment03/small_problems_grade.rs +++ b/src/assignments/assignment03/small_exercises_grade.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod test { - use crate::assignments::assignment03::small_problems::*; + use crate::assignments::assignment03::small_exercises::*; #[test] fn test_next_weekday() { diff --git a/src/assignments/assignment06/mod.rs b/src/assignments/assignment06/mod.rs index da14417..e809694 100644 --- a/src/assignments/assignment06/mod.rs +++ b/src/assignments/assignment06/mod.rs @@ -8,9 +8,7 @@ use std::{collections::HashMap, fmt::Debug}; pub mod semiring; -pub mod square_matrix; pub mod symbolic_differentiation; mod semiring_grade; -mod square_matrix_grade; mod symbolic_differentiation_grade; diff --git a/src/assignments/assignment06/square_matrix.rs b/src/assignments/assignment06/square_matrix.rs deleted file mode 100644 index f81209e..0000000 --- a/src/assignments/assignment06/square_matrix.rs +++ /dev/null @@ -1,88 +0,0 @@ -//! Square matrix - -/// Square matrix -pub trait SquareMatrix { - /// The type of the submatrix of this square matrix. - /// For example, the submatrix of a 3 x 3 matrix is a 2 x 2 matrix. - /// https://en.wikipedia.org/wiki/Matrix_(mathematics)#Submatrix - type Submatrix; - - /// Returns the submatrix obtained by removing the `row`th row and `col`th column - /// from the original matrix. - /// https://en.wikipedia.org/wiki/Matrix_(mathematics)#Submatrix - fn sub_matrix(&self, row: usize, col: usize) -> Self::Submatrix; - - /// Returns the determinant of the matrix. - fn det(&self) -> i64; - - /// Returns the determinant of ab, where a is self, b is given, and ab is the matrix product of them. - /// Note that the size of a and b are the same. - /// Hint: Use the fact that det(ab) = det(a) * det(b) - /// https://en.wikipedia.org/wiki/Determinant#Multiplicativity_and_matrix_groups - fn det_ab(&self, b: &Self) -> i64 { - todo!() - } -} - -/// 2 x 2 matrix -#[derive(Debug, PartialEq)] -pub struct Mat2 { - /// inner is a 2 dimensional array (size: 2 x 2) - pub inner: [[i64; 2]; 2], -} - -impl SquareMatrix for Mat2 { - type Submatrix = i64; - - fn sub_matrix(&self, row: usize, col: usize) -> Self::Submatrix { - // Hint: The submatrix of a 2 x 2 matrix is simply a single number. - todo!() - } - - // Hint: https://en.wikipedia.org/wiki/Determinant - fn det(&self) -> i64 { - todo!() - } -} - -/// 3 x 3 matrix -#[derive(Debug, PartialEq)] -pub struct Mat3 { - /// inner is a 2 dimensional array (size: 3 x 3) - pub inner: [[i64; 3]; 3], -} - -impl SquareMatrix for Mat3 { - type Submatrix = Mat2; - - fn sub_matrix(&self, row: usize, col: usize) -> Self::Submatrix { - todo!() - } - - // Hint: Use the determinant of the sub-matrices. - // https://semath.info/src/determinant-three-by-three.html - fn det(&self) -> i64 { - todo!() - } -} - -/// 4 x 4 matrix -#[derive(Debug, PartialEq)] -pub struct Mat4 { - /// inner is a 2 dimensional array (size: 4 x 4) - pub inner: [[i64; 4]; 4], -} - -impl SquareMatrix for Mat4 { - type Submatrix = Mat3; - - fn sub_matrix(&self, row: usize, col: usize) -> Self::Submatrix { - todo!() - } - - // Hint: Use the determinant of the sub-matrices. - // https://semath.info/src/determinant-four-by-four.html - fn det(&self) -> i64 { - todo!() - } -} diff --git a/src/assignments/assignment06/square_matrix_grade.rs b/src/assignments/assignment06/square_matrix_grade.rs deleted file mode 100644 index 2ca244c..0000000 --- a/src/assignments/assignment06/square_matrix_grade.rs +++ /dev/null @@ -1,72 +0,0 @@ -#[cfg(test)] -mod test { - use crate::assignments::assignment06::square_matrix::*; - use ntest::assert_about_eq; - - #[test] - fn test_mat2() { - let mat = Mat2 { - inner: [[1, 2], [3, 4]], - }; - assert_eq!(mat.sub_matrix(1, 1), 4); - assert_eq!(mat.sub_matrix(1, 2), 3); - assert_eq!(mat.sub_matrix(2, 1), 2); - assert_eq!(mat.sub_matrix(2, 2), 1); - assert_eq!(mat.det(), -2); - - let mat2 = Mat2 { - inner: [[2, 3], [5, 7]], - }; - assert_eq!(mat.det_ab(&mat2), mat.det() * mat2.det()); - } - - #[test] - fn test_mat3() { - let mat = Mat3 { - inner: [[1, 2, 3], [5, 5, 6], [7, 8, 10]], - }; - assert_eq!( - mat.sub_matrix(1, 2), - Mat2 { - inner: [[5, 6], [7, 10]] - } - ); - assert_eq!(mat.det(), 1); - - let mat2 = Mat3 { - inner: [[2, 3, 5], [7, 10, 11], [12, 14, 20]], - }; - assert_eq!(mat2.det(), -42); - assert_eq!(mat.det_ab(&mat2), mat.det() * mat2.det()); - } - - #[test] - fn test_mat4() { - let mat = Mat4 { - inner: [ - [1, 11, 3, 4], - [5, 6, 7, 9], - [25, 10, 11, 20], - [36, 14, 15, 30], - ], - }; - assert_eq!( - mat.sub_matrix(2, 3), - Mat3 { - inner: [[1, 11, 4], [25, 10, 20], [36, 14, 30]] - } - ); - assert_eq!(mat.det(), 2089); - - let mat2 = Mat4 { - inner: [ - [2, 3, 5, 5], - [7, 10, 11, 20], - [12, 14, 20, 30], - [1, 2, 5, 10], - ], - }; - assert_eq!(mat2.det(), -340); - assert_eq!(mat.det_ab(&mat2), mat.det() * mat2.det()); - } -} diff --git a/src/assignments/assignment06/symbolic_differentiation.rs b/src/assignments/assignment06/symbolic_differentiation.rs index 28d3b4a..9e33d2b 100644 --- a/src/assignments/assignment06/symbolic_differentiation.rs +++ b/src/assignments/assignment06/symbolic_differentiation.rs @@ -79,6 +79,7 @@ pub trait Differentiable: Clone { fn diff(&self) -> Self; } +/// HINT: Consult impl Differentiable for Rational { fn diff(&self) -> Self { todo!() @@ -115,6 +116,7 @@ impl SingletonPolynomial { } impl Differentiable for SingletonPolynomial { + /// HINT: Consult fn diff(&self) -> Self { todo!() } @@ -138,6 +140,7 @@ impl Default for Exp { } impl Differentiable for Exp { + /// HINT: Consult fn diff(&self) -> Self { todo!() } @@ -173,6 +176,7 @@ impl Trignometric { } impl Differentiable for Trignometric { + /// HINT: Consult fn diff(&self) -> Self { todo!() } @@ -221,6 +225,7 @@ impl Differentiable for Box { } impl Differentiable for ComplexFuncs { + /// HINT: Consult fn diff(&self) -> Self { todo!() } diff --git a/src/assignments/assignment07/generator.rs b/src/assignments/assignment07/generator.rs index fb0243b..e95eb6a 100644 --- a/src/assignments/assignment07/generator.rs +++ b/src/assignments/assignment07/generator.rs @@ -1,11 +1,17 @@ //! Generators +//! +//! HINT: Look at the `generator_grade.rs` file to see how the generator is used. +/// Yielded value. It can be either a value or a stop signal. enum Yielded { Value(T), Stop, } /// Generator +/// - kk +/// - You can call `next()` method to get the next value. +/// - The generator should stop when it yields `Yielded::Stop`. /// /// Reference: /// - [Python generator](https://python-reference.readthedocs.io/en/latest/docs/generator/) @@ -24,13 +30,15 @@ impl Iterator for Generator { } /// Returns a generator that yields fibonacci numbers. +/// +/// HINT: Consult pub fn fib_generator(first: usize, second: usize) -> Generator { todo!() } /// Returns a generator that yields collatz numbers. /// -/// The generator stops when it reaches to 1. +/// HINT: Consult pub fn collatz_conjecture(start: usize) -> Generator { todo!() } diff --git a/src/assignments/assignment07/hubo.rs b/src/assignments/assignment07/hubo.rs deleted file mode 100644 index 581cea9..0000000 --- a/src/assignments/assignment07/hubo.rs +++ /dev/null @@ -1,84 +0,0 @@ -//! Hubo is back! - -/// Types that represent a direction. -pub trait Direction { - /// Get the direction in the form of a 2-dimensional vector. - /// The resulting value doesn't have to be normalized. - fn get_vector(&self) -> (f32, f32); -} - -/// 4-way enum to indicate directions. -#[derive(Debug)] -pub enum Dir4 { - /// +x direction - Right, - /// -x direction - Left, - /// +y direction - Up, - /// -y direction - Down, -} - -impl Direction for Dir4 { - fn get_vector(&self) -> (f32, f32) { - todo!() - } -} - -impl Direction for (f32, f32) { - fn get_vector(&self) -> (f32, f32) { - todo!() - } -} - -/// Hubo. -/// It's direction can be represented by an arbitrary type. -/// -/// It can be controlled by [HuboController] only if the direction type implements the [Direction] trait. -#[derive(Debug)] -pub struct Hubo { - direction: TDir, - x: f32, - y: f32, -} - -/// Controller of the Hubo -#[derive(Debug)] -pub struct HuboController<'s, TDir> { - hubo: &'s mut Hubo, -} - -impl Hubo { - /// Create a Hubo. - pub fn new(direction: TDir, x: f32, y: f32) -> Self { - Self { direction, x, y } - } - - /// Return the current position of Hubo. - pub fn get_position(&self) -> (f32, f32) { - (self.x, self.y) - } -} - -impl<'s, TDir: Direction> HuboController<'s, TDir> { - /// Return the controller of the given Hubo. - /// Note that the lifetime of hubo's mutable reference \['s\] is repeated in the return type. - /// - /// This represents that the controller cannot live longer than the mutable reference, - /// since the controller takes and stores the reference. - pub fn new(hubo: &'s mut Hubo) -> HuboController<'s, TDir> { - todo!() - } - - /// Make Hubo move forward by the given distance. You might need to normalize the vector - /// acquired from `Direction::get_move_vector`. - pub fn move_hubo_forward(&mut self, distance: f32) { - todo!() - } - - /// Make Hubo turn to the given direction. - pub fn set_hubo_direction(&mut self, dir: TDir) { - todo!() - } -} diff --git a/src/assignments/assignment07/hubo_grade.rs b/src/assignments/assignment07/hubo_grade.rs deleted file mode 100644 index e67fec3..0000000 --- a/src/assignments/assignment07/hubo_grade.rs +++ /dev/null @@ -1,40 +0,0 @@ -#[cfg(test)] -mod test { - use itertools::Itertools; - use ntest::assert_about_eq; - - use crate::assignments::assignment07::hubo::*; - - #[test] - fn test_hubo_dir4_movement() { - let mut hubo = Hubo::new(Dir4::Right, 0.0, 0.0); - let mut controller = HuboController::new(&mut hubo); - - // Test moving forward - controller.move_hubo_forward(5.0); - - controller.set_hubo_direction(Dir4::Up); - controller.move_hubo_forward(3.0); - - controller.set_hubo_direction(Dir4::Left); - controller.move_hubo_forward(2.0); - - assert_eq!(hubo.get_position(), (3.0, 3.0)); - } - - #[test] - fn test_hubo_tuple_movement() { - let mut hubo = Hubo::new((1., 0.), 0.0, 0.0); - let mut controller = HuboController::new(&mut hubo); - - // Test moving forward - controller.move_hubo_forward(5.0); - - controller.set_hubo_direction((3., 4.)); - controller.move_hubo_forward(5.0); - - controller.set_hubo_direction((-8., -6.)); - controller.move_hubo_forward(15.0); - assert_eq!(hubo.get_position(), (-4., -5.)); - } -} diff --git a/src/assignments/assignment07/mod.rs b/src/assignments/assignment07/mod.rs index 87c48ed..06232aa 100644 --- a/src/assignments/assignment07/mod.rs +++ b/src/assignments/assignment07/mod.rs @@ -6,13 +6,11 @@ //! See `assignment07_grade.rs` and `/scripts/grade-07.sh` for the test script. pub mod generator; -pub mod hubo; pub mod my_itertools; pub mod small_exercises; pub mod transform; mod generator_grade; -mod hubo_grade; mod my_itertools_grade; mod small_exercises_grade; mod transform_grade; diff --git a/src/assignments/assignment07/small_exercises.rs b/src/assignments/assignment07/small_exercises.rs index 86c5661..7c210bb 100644 --- a/src/assignments/assignment07/small_exercises.rs +++ b/src/assignments/assignment07/small_exercises.rs @@ -23,7 +23,7 @@ pub fn find<'s, T: Eq>(query: &'s [T], base: &'s [T]) -> impl 's + Iterator { // TODO: remove `_marker` and add necessary fields as you want _marker: std::marker::PhantomData, diff --git a/src/assignments/assignment08/mod.rs b/src/assignments/assignment08/mod.rs index 8bf6f8c..260a00a 100644 --- a/src/assignments/assignment08/mod.rs +++ b/src/assignments/assignment08/mod.rs @@ -5,5 +5,5 @@ //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-08.sh` works fine. //! See `small_problems_grade.rs` and `/scripts/grade-08.sh` for the test script. -pub mod small_problems; -mod small_problems_grade; +pub mod small_exercises; +mod small_exercises_grade; diff --git a/src/assignments/assignment08/small_problems.rs b/src/assignments/assignment08/small_exercises.rs similarity index 100% rename from src/assignments/assignment08/small_problems.rs rename to src/assignments/assignment08/small_exercises.rs diff --git a/src/assignments/assignment08/small_problems_grade.rs b/src/assignments/assignment08/small_exercises_grade.rs similarity index 95% rename from src/assignments/assignment08/small_problems_grade.rs rename to src/assignments/assignment08/small_exercises_grade.rs index 2549b0c..dfcb550 100644 --- a/src/assignments/assignment08/small_problems_grade.rs +++ b/src/assignments/assignment08/small_exercises_grade.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod test { - use crate::assignments::assignment08::small_problems::*; + use crate::assignments::assignment08::small_exercises::*; #[test] fn test_repeat() { diff --git a/src/assignments/assignment11/graph_grade.rs b/src/assignments/assignment11/graph_grade.rs index 4949ea4..a7ea493 100644 --- a/src/assignments/assignment11/graph_grade.rs +++ b/src/assignments/assignment11/graph_grade.rs @@ -23,9 +23,9 @@ mod test_graph { } let mut graph1 = SubGraph::new(); - for n in 0..6 { + (0..6).for_each(|n| { assert!(graph1.add_node(nodes[n].clone())); - } + }); assert!(graph1.detect_cycle()); assert!(!graph1.add_node(nodes[0].clone())); diff --git a/src/assignments/assignment13/mod.rs b/src/assignments/assignment13/mod.rs index 34b3587..504c1e3 100644 --- a/src/assignments/assignment13/mod.rs +++ b/src/assignments/assignment13/mod.rs @@ -6,5 +6,5 @@ //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-13.sh` works fine. //! See `assignment13_grade.rs` and `/scripts/grade-13.sh` for the test script. -pub mod small_problems; -mod small_problems_grade; +pub mod small_exercises; +mod small_exercises_grade; diff --git a/src/assignments/assignment13/small_problems.rs b/src/assignments/assignment13/small_exercises.rs similarity index 100% rename from src/assignments/assignment13/small_problems.rs rename to src/assignments/assignment13/small_exercises.rs diff --git a/src/assignments/assignment13/small_problems_grade.rs b/src/assignments/assignment13/small_exercises_grade.rs similarity index 99% rename from src/assignments/assignment13/small_problems_grade.rs rename to src/assignments/assignment13/small_exercises_grade.rs index 8d315ee..494acd9 100644 --- a/src/assignments/assignment13/small_problems_grade.rs +++ b/src/assignments/assignment13/small_exercises_grade.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod test { use crate::assignments::assignment09::matmul::*; - use crate::assignments::assignment13::small_problems::*; + use crate::assignments::assignment13::small_exercises::*; use approx::*; use itertools::Itertools; use ndarray::prelude::*;