Merge branch 'jungin' into 'main'

Polish

See merge request kaist-cp-class/cs220-private!20
This commit is contained in:
Marge
2023-08-21 11:54:54 +00:00
25 changed files with 32 additions and 307 deletions

View File

@@ -8,5 +8,5 @@
//!
//! Hint: <https://doc.rust-lang.org/std/primitive.usize.html>
pub mod small_problems;
mod small_problems_grade;
pub mod small_exercises;
mod small_exercises_grade;

View File

@@ -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() {

View File

@@ -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;

View File

@@ -1,6 +1,6 @@
#[cfg(test)]
mod test {
use crate::assignments::assignment02::small_problems::*;
use crate::assignments::assignment02::small_exercises::*;
#[test]
fn test_fahrenheit() {

View File

@@ -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;

View File

@@ -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() {

View File

@@ -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;

View File

@@ -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!()
}
}

View File

@@ -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());
}
}

View File

@@ -79,6 +79,7 @@ pub trait Differentiable: Clone {
fn diff(&self) -> Self;
}
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Constant_term_rule>
impl Differentiable for Rational {
fn diff(&self) -> Self {
todo!()
@@ -115,6 +116,7 @@ impl SingletonPolynomial {
}
impl Differentiable for SingletonPolynomial {
/// HINT: Consult <https://en.wikipedia.org/wiki/Power_rule>
fn diff(&self) -> Self {
todo!()
}
@@ -138,6 +140,7 @@ impl Default for Exp {
}
impl Differentiable for Exp {
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Derivatives_of_exponential_and_logarithmic_functions>
fn diff(&self) -> Self {
todo!()
}
@@ -173,6 +176,7 @@ impl Trignometric {
}
impl Differentiable for Trignometric {
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Derivatives_of_trigonometric_functions>
fn diff(&self) -> Self {
todo!()
}
@@ -221,6 +225,7 @@ impl<F: Differentiable> Differentiable for Box<F> {
}
impl<F: Differentiable> Differentiable for ComplexFuncs<F> {
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Elementary_rules_of_differentiation>
fn diff(&self) -> Self {
todo!()
}

View File

@@ -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<T> {
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<T, S> Iterator for Generator<T, S> {
}
/// Returns a generator that yields fibonacci numbers.
///
/// HINT: Consult <https://en.wikipedia.org/wiki/Fibonacci_sequence>
pub fn fib_generator(first: usize, second: usize) -> Generator<usize, (usize, usize)> {
todo!()
}
/// Returns a generator that yields collatz numbers.
///
/// The generator stops when it reaches to 1.
/// HINT: Consult <https://en.wikipedia.org/wiki/Collatz_conjecture>
pub fn collatz_conjecture(start: usize) -> Generator<usize, usize> {
todo!()
}

View File

@@ -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<TDir> {
direction: TDir,
x: f32,
y: f32,
}
/// Controller of the Hubo
#[derive(Debug)]
pub struct HuboController<'s, TDir> {
hubo: &'s mut Hubo<TDir>,
}
impl<TDir> Hubo<TDir> {
/// 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<TDir>) -> 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!()
}
}

View File

@@ -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.));
}
}

View File

@@ -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;

View File

@@ -23,7 +23,7 @@ pub fn find<'s, T: Eq>(query: &'s [T], base: &'s [T]) -> impl 's + Iterator<Item
}
}
/// Implement fibonacci iterator
/// Implement generic fibonacci iterator
struct FibIter<T> {
// TODO: remove `_marker` and add necessary fields as you want
_marker: std::marker::PhantomData<T>,

View File

@@ -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;

View File

@@ -1,6 +1,6 @@
#[cfg(test)]
mod test {
use crate::assignments::assignment08::small_problems::*;
use crate::assignments::assignment08::small_exercises::*;
#[test]
fn test_repeat() {

View File

@@ -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()));

View File

@@ -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;

View File

@@ -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::*;