From afe2c7b2d2a305b38ffd39b2281ab349934e60b7 Mon Sep 17 00:00:00 2001 From: "jungin.rhee" Date: Tue, 22 Aug 2023 05:16:34 +0000 Subject: [PATCH] polish --- src/assignments/assignment01/mod.rs | 6 +- .../assignment01/small_exercises.rs | 5 + src/assignments/assignment02/mod.rs | 2 +- src/assignments/assignment02/vec_and_mat.rs | 5 +- src/assignments/assignment03/mod.rs | 2 +- src/assignments/assignment04/mod.rs | 2 +- src/assignments/assignment06/mod.rs | 4 +- src/assignments/assignment06/semiring.rs | 2 +- .../assignment06/symbolic_differentiation.rs | 4 +- src/assignments/assignment07/generator.rs | 1 - src/assignments/assignment07/mod.rs | 2 +- .../assignment07/small_exercises.rs | 4 +- src/assignments/assignment08/mod.rs | 4 +- src/assignments/assignment09/bigint.rs | 5 +- src/assignments/assignment09/mod.rs | 2 +- src/assignments/assignment10/labyrinth.rs | 6 +- src/assignments/assignment10/mod.rs | 3 +- .../assignment10/small_exercises.rs | 6 +- .../assignment11/doubly_linked_list.rs | 101 ------------- .../assignment11/doubly_linked_list_grade.rs | 43 ------ src/assignments/assignment11/graph.rs | 4 + src/assignments/assignment11/linked_list.rs | 140 ++++++++++++++++++ .../assignment11/linked_list_grade.rs | 122 +++++++++++++++ src/assignments/assignment11/mod.rs | 16 +- src/assignments/assignment12/card.rs | 2 +- src/assignments/assignment12/mod.rs | 9 +- src/assignments/assignment13/mod.rs | 2 +- 27 files changed, 312 insertions(+), 192 deletions(-) delete mode 100644 src/assignments/assignment11/doubly_linked_list.rs delete mode 100644 src/assignments/assignment11/doubly_linked_list_grade.rs create mode 100644 src/assignments/assignment11/linked_list.rs create mode 100644 src/assignments/assignment11/linked_list_grade.rs diff --git a/src/assignments/assignment01/mod.rs b/src/assignments/assignment01/mod.rs index 5c279c8..dfcc07f 100644 --- a/src/assignments/assignment01/mod.rs +++ b/src/assignments/assignment01/mod.rs @@ -3,10 +3,8 @@ //! The primary goal of this assignment is bringing up SSH, VSCode, and all the other necessary tools to develop Rust programs. //! Please make sure you're comfortable with developing Rust programs before moving on to the next assignments. //! -//! You should fill out `add()` and `sub()` function bodies in such a way that `/scripts/grade-01.sh` works fine. -//! See `small_problems_grade.rs` and `/scripts/grade-01.sh` for the test script. -//! -//! Hint: +//! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-01.sh` works fine. +//! See `assigment01/small_exercises_grade.rs` and `/scripts/grade-01.sh` for the test script. pub mod small_exercises; mod small_exercises_grade; diff --git a/src/assignments/assignment01/small_exercises.rs b/src/assignments/assignment01/small_exercises.rs index aaf3727..a85884d 100644 --- a/src/assignments/assignment01/small_exercises.rs +++ b/src/assignments/assignment01/small_exercises.rs @@ -1,5 +1,10 @@ //! Assignment 1: Preparing Rust Development Environment. //! Welcome to the CS220 course! +//! +//! You should fill out `add()` and `sub()` function bodies in such a way that `/scripts/grade-01.sh` works fine. +//! See `small_problems_grade.rs` and `/scripts/grade-01.sh` for the test script. +//! +//! Hint: /// Adds two unsigned words. If overflow happens, just wrap around. pub fn add(lhs: usize, rhs: usize) -> usize { diff --git a/src/assignments/assignment02/mod.rs b/src/assignments/assignment02/mod.rs index 90b3f2c..282c04c 100644 --- a/src/assignments/assignment02/mod.rs +++ b/src/assignments/assignment02/mod.rs @@ -4,7 +4,7 @@ //! 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 `*_grade.rs` and `/scripts/grade-02.sh` for the test script. +//! See `assigment02/*_grade.rs` and `/scripts/grade-02.sh` for the test script. pub mod small_exercises; mod small_exercises_grade; diff --git a/src/assignments/assignment02/vec_and_mat.rs b/src/assignments/assignment02/vec_and_mat.rs index df969b3..0054fff 100644 --- a/src/assignments/assignment02/vec_and_mat.rs +++ b/src/assignments/assignment02/vec_and_mat.rs @@ -41,6 +41,7 @@ impl Mat2 { impl Mul for Mat2 { type Output = Mat2; + /// Consult fn mul(self, rhs: Mat2) -> Self::Output { todo!() } @@ -50,6 +51,8 @@ impl Mul for Mat2 { type Output = Vec2; /// Multiplies the matrix by the vector. + /// + /// Consult fn mul(self, rhs: Vec2) -> Self::Output { todo!() } @@ -105,7 +108,7 @@ pub struct FMat2 { impl FMat2 { /// Returns the inverse of the given matrix. (We assume the given matrix is always invertible.) - /// HINT: https://www.mathcentre.ac.uk/resources/uploaded/sigma-matrices7-2009-1.pdf + /// HINT: /// /// # Example /// diff --git a/src/assignments/assignment03/mod.rs b/src/assignments/assignment03/mod.rs index 76a311c..1c950e0 100644 --- a/src/assignments/assignment03/mod.rs +++ b/src/assignments/assignment03/mod.rs @@ -1,7 +1,7 @@ //! Assignment 3: Mastering common programming concepts (2/2) //! //! 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. +//! See `assignment03/*_grade.rs` and `/scripts/grade-03.sh` for the test script. pub mod small_exercises; mod small_exercises_grade; diff --git a/src/assignments/assignment04/mod.rs b/src/assignments/assignment04/mod.rs index 4b927d8..ceb0a8c 100644 --- a/src/assignments/assignment04/mod.rs +++ b/src/assignments/assignment04/mod.rs @@ -12,7 +12,7 @@ //! For calculator, just reading `syntax.rs` would suffice for you to understand what to do. //! //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-04.sh` works fine. -//! See `assignment04_grade.rs` and `/scripts/grade-04.sh` for the test script. +//! See `assignment04/grade.rs` and `/scripts/grade-04.sh` for the test script. //! Run `/scripts/prepare-submissions.sh` and submit `/target/assignment04.zip` to . pub mod context; diff --git a/src/assignments/assignment06/mod.rs b/src/assignments/assignment06/mod.rs index e809694..8727bc1 100644 --- a/src/assignments/assignment06/mod.rs +++ b/src/assignments/assignment06/mod.rs @@ -3,9 +3,7 @@ //! The primary goal of this assignment is to understand generics, traits, and lifetimes. //! //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-06.sh` works fine. -//! See `assignment06_grade.rs` and `/scripts/grade-06.sh` for the test script. - -use std::{collections::HashMap, fmt::Debug}; +//! See `assignment06/*_grade.rs` and `/scripts/grade-06.sh` for the test script. pub mod semiring; pub mod symbolic_differentiation; diff --git a/src/assignments/assignment06/semiring.rs b/src/assignments/assignment06/semiring.rs index 1add902..57cb5bb 100644 --- a/src/assignments/assignment06/semiring.rs +++ b/src/assignments/assignment06/semiring.rs @@ -156,7 +156,7 @@ impl From for Polynomial { /// - In `x^n` and `ax^n`, it is guaranteed that `n >= 2`. /// - All terms have unique degrees. /// -/// Consult `assignment06_jaemin_choi_grade.rs` for example valid strings. +/// Consult `assignment06/grade.rs` for example valid strings. /// /// Hint: `.split`, `.parse`, and `Polynomial::term` impl std::str::FromStr for Polynomial { diff --git a/src/assignments/assignment06/symbolic_differentiation.rs b/src/assignments/assignment06/symbolic_differentiation.rs index 9e33d2b..2269d13 100644 --- a/src/assignments/assignment06/symbolic_differentiation.rs +++ b/src/assignments/assignment06/symbolic_differentiation.rs @@ -79,8 +79,8 @@ pub trait Differentiable: Clone { fn diff(&self) -> Self; } -/// HINT: Consult impl Differentiable for Rational { + /// HINT: Consult fn diff(&self) -> Self { todo!() } @@ -122,7 +122,7 @@ impl Differentiable for SingletonPolynomial { } } -/// Expoential function. +/// Expoential function.(`e^x`) #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Exp; diff --git a/src/assignments/assignment07/generator.rs b/src/assignments/assignment07/generator.rs index e95eb6a..57f8172 100644 --- a/src/assignments/assignment07/generator.rs +++ b/src/assignments/assignment07/generator.rs @@ -9,7 +9,6 @@ enum Yielded { } /// Generator -/// - kk /// - You can call `next()` method to get the next value. /// - The generator should stop when it yields `Yielded::Stop`. /// diff --git a/src/assignments/assignment07/mod.rs b/src/assignments/assignment07/mod.rs index 06232aa..0dbfcd7 100644 --- a/src/assignments/assignment07/mod.rs +++ b/src/assignments/assignment07/mod.rs @@ -3,7 +3,7 @@ //! The primary goal of this assignment is to understand generics, traits, and lifetimes. //! //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-07.sh` works fine. -//! See `assignment07_grade.rs` and `/scripts/grade-07.sh` for the test script. +//! See `assignment07/*_grade.rs` and `/scripts/grade-07.sh` for the test script. pub mod generator; pub mod my_itertools; diff --git a/src/assignments/assignment07/small_exercises.rs b/src/assignments/assignment07/small_exercises.rs index 7c210bb..ece7f41 100644 --- a/src/assignments/assignment07/small_exercises.rs +++ b/src/assignments/assignment07/small_exercises.rs @@ -52,7 +52,7 @@ pub fn fib(first: T, second: T) -> impl Iterator where T: std::ops::Add + Copy, { - todo!("remove below"); + todo!("replace `std::iter::empty() with your owm implementation`"); std::iter::empty() } @@ -86,7 +86,7 @@ impl Iterator for RangeIter { /// Returns an iterator over the range [left, right) with the given step. pub fn range(left: Endpoint, right: Endpoint, step: isize) -> impl Iterator { - todo!("remove below"); + todo!("replace `std::iter::empty() with your owm implementation`"); std::iter::empty() } diff --git a/src/assignments/assignment08/mod.rs b/src/assignments/assignment08/mod.rs index 260a00a..c601b9a 100644 --- a/src/assignments/assignment08/mod.rs +++ b/src/assignments/assignment08/mod.rs @@ -3,7 +3,9 @@ //! The primary goal of this assignment is to get used to first-class functions. //! //! 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. +//! See `assignment08/*_grade.rs` and `/scripts/grade-08.sh` for the test script. pub mod small_exercises; mod small_exercises_grade; + +// TODO: add kyeongmin's church encoding asignment diff --git a/src/assignments/assignment09/bigint.rs b/src/assignments/assignment09/bigint.rs index 1c1db51..33a96e5 100644 --- a/src/assignments/assignment09/bigint.rs +++ b/src/assignments/assignment09/bigint.rs @@ -39,11 +39,8 @@ impl BigInt { } /// Creates a new `BigInt` from a `Vec`. - /// - /// # Panic - /// - /// Panics if `carrier` is empty. pub fn new_large(carrier: Vec) -> Self { + assert!(!carrier.is_empty()); todo!() } } diff --git a/src/assignments/assignment09/mod.rs b/src/assignments/assignment09/mod.rs index 9601827..9fa8940 100644 --- a/src/assignments/assignment09/mod.rs +++ b/src/assignments/assignment09/mod.rs @@ -3,7 +3,7 @@ //! The primary goal of this assignment is to get used to iterators. //! //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-09.sh` works fine. -//! See `assignment09_grade.rs` and `/scripts/grade-09.sh` for the test script. +//! See `assignment09/*_grade.rs` and `/scripts/grade-09.sh` for the test script. pub mod bigint; pub mod matmul; diff --git a/src/assignments/assignment10/labyrinth.rs b/src/assignments/assignment10/labyrinth.rs index 001b522..0b9e8e1 100644 --- a/src/assignments/assignment10/labyrinth.rs +++ b/src/assignments/assignment10/labyrinth.rs @@ -1,14 +1,12 @@ //! Labyrinth //! -//! Look at the [test code](labyrinth_grade.rs) below before you start. -//! HINT: https://en.wikipedia.org/wiki/100_prisoners_problem +//! Look at the `labyrinth_grade.rs` below before you start. +//! HINT: //! //! NOTE: You will have to implement a probabilistic algorithm, which means, the algorithm can fail //! even if you have implemented the solution. We recommend running multiple times (at least 5 times) to check your //! solution works well. -#![allow(missing_docs)] - use std::cell::RefCell; /// Husband diff --git a/src/assignments/assignment10/mod.rs b/src/assignments/assignment10/mod.rs index 5b7390a..7fc988f 100644 --- a/src/assignments/assignment10/mod.rs +++ b/src/assignments/assignment10/mod.rs @@ -1,9 +1,8 @@ -//! //! Assignment 10: Iterators (2/2). //! The primary goal of this assignment is to get used to iterators. //! //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-10.sh` works fine. -//! See `assignment10_grade.rs` and `/scripts/grade-10.sh` for the test script. +//! See `assignment10/*_grade.rs` and `/scripts/grade-10.sh` for the test script. pub mod labyrinth; pub mod small_exercises; diff --git a/src/assignments/assignment10/small_exercises.rs b/src/assignments/assignment10/small_exercises.rs index e4d59cb..4cb8f74 100644 --- a/src/assignments/assignment10/small_exercises.rs +++ b/src/assignments/assignment10/small_exercises.rs @@ -1,4 +1,5 @@ //! Small exercises. + use itertools::*; /// Returns the pairs of `(i, j)` where `i < j` and `inner[i] > inner[j]` in increasing order. @@ -185,9 +186,8 @@ impl Iterator for Pythagorean { } } -/// Generates sequence of unique [primitive Pythagorean -/// triples](https://en.wikipedia.org/wiki/Pythagorean_triple), i.e. (a,b,c) such that a² + b² = -/// c², a and b are coprimes, and a < b. Generate in the increasing order of c. +/// Generates sequence of unique [primitive Pythagorean triples](https://en.wikipedia.org/wiki/Pythagorean_triple), +/// i.e. (a,b,c) such that a² + b² = c², a and b are coprimes, and a < b. Generate in the increasing order of c. pub fn pythagorean() -> impl Iterator { Pythagorean::new() } diff --git a/src/assignments/assignment11/doubly_linked_list.rs b/src/assignments/assignment11/doubly_linked_list.rs deleted file mode 100644 index d3cc7bf..0000000 --- a/src/assignments/assignment11/doubly_linked_list.rs +++ /dev/null @@ -1,101 +0,0 @@ -//! Doubly Linked List. -//! -//! Refer `doubly_linked_list_grade.rs` for test cases. - -use std::{cell::RefCell, fmt::Debug, rc::Rc}; - -type Link = Option>>>; - -/// Node of a doubly-linked list. -#[derive(Debug)] -pub struct Node { - /// Value of current node. - value: RefCell, - - /// Pointer to the next node. If it is `None`, there is no next node. - next: Link, - - /// Pointer to the previous node. If it is `None`, there is no previous node. - prev: Link, -} - -impl Node { - /// Creates a new node. - fn new(value: T) -> Self { - Self { - value: RefCell::new(value), - next: None, - prev: None, - } - } - - /// Fetch the value contained in node - pub fn get(&self) -> T { - self.value.borrow().clone() - } - - /// Replace the data contained in the node - pub fn replace(&self, new_value: T) -> T { - self.value.replace(new_value) - } - - /// Fetch previous node - pub fn prev(&self) -> Link { - self.prev.clone() - } - - /// Fetch next node - pub fn next(&self) -> Link { - self.next.clone() - } -} - -/// A doubly-linked list. -#[derive(Default, Debug)] -pub struct DoublyLinkedList { - /// Head node of the list. If it is `None`, the list is empty. - head: Link, - - /// Tail node of the list. If it is `None`, the list is empty. - tail: Link, -} - -impl DoublyLinkedList { - /// Creates a new list. - pub fn new() -> Self { - Self { - head: None, - tail: None, - } - } - - /// Adds the given node to the front of the list. - pub fn push_front(&mut self, value: T) { - todo!() - } - - /// Adds the given node to the back of the list. - pub fn push_back(&mut self, value: T) { - todo!() - } - - /// Removes and returns the node at the front of the list. - pub fn pop_front(&mut self) -> Option { - todo!() - } - - /// Removes and returns the node at the back of the list. - pub fn pop_back(&mut self) -> Option { - todo!() - } -} - -impl Drop for DoublyLinkedList { - fn drop(&mut self) { - while let Some(node) = self.head.take() { - let _ = node.borrow_mut().prev.take(); - self.head = node.borrow_mut().next.take(); - } - let _unused = self.tail.take(); - } -} diff --git a/src/assignments/assignment11/doubly_linked_list_grade.rs b/src/assignments/assignment11/doubly_linked_list_grade.rs deleted file mode 100644 index ebdc19a..0000000 --- a/src/assignments/assignment11/doubly_linked_list_grade.rs +++ /dev/null @@ -1,43 +0,0 @@ -//! Test cases for assignment11/doubly_linked_list.rs - -#[cfg(test)] -mod test_doubly_linked_list { - use crate::assignments::assignment11::doubly_linked_list::*; - - #[test] - fn test_works() { - let mut list = DoublyLinkedList::new(); - - list.push_back(3); - list.push_back(4); - assert_eq!(list.pop_front(), Some(3)); - - list.push_front(5); - assert_eq!(list.pop_back(), Some(4)); - assert_eq!(list.pop_back(), Some(5)); - assert_eq!(list.pop_back(), None); - assert_eq!(list.pop_front(), None); - } - - #[test] - fn test_can_push_back() { - let mut list = DoublyLinkedList::new(); - assert_eq!(list.pop_back(), None); - - list.push_back(3); - list.push_back(4); - list.push_back(5); - assert_eq!(list.pop_back(), Some(5)); - - list.push_back(6); - list.push_back(7); - assert_eq!(list.pop_back(), Some(7)); - assert_eq!(list.pop_back(), Some(6)); - assert_eq!(list.pop_back(), Some(4)); - assert_eq!(list.pop_back(), Some(3)); - - list.push_back(2); - assert_eq!(list.pop_back(), Some(2)); - assert_eq!(list.pop_back(), None); - } -} diff --git a/src/assignments/assignment11/graph.rs b/src/assignments/assignment11/graph.rs index 65a92f3..7c3ca6f 100644 --- a/src/assignments/assignment11/graph.rs +++ b/src/assignments/assignment11/graph.rs @@ -23,6 +23,8 @@ enum VisitStatus { /// `NodeHandle` should implement `Clone`, which clones the handle without cloning the underlying /// node. That is, there can be multiple handles to the same node. /// The user can access the node through a handle if it does not violate Rust's aliasing rules. +/// +/// TODO: You can freely add fields to this struct. #[derive(Debug, Clone)] pub struct NodeHandle; @@ -31,6 +33,8 @@ pub struct NodeHandle; pub struct GraphError; /// Subgraph +/// +/// TODO: You can freely add fields to this struct. #[derive(Debug)] pub struct SubGraph; diff --git a/src/assignments/assignment11/linked_list.rs b/src/assignments/assignment11/linked_list.rs new file mode 100644 index 0000000..161a7a4 --- /dev/null +++ b/src/assignments/assignment11/linked_list.rs @@ -0,0 +1,140 @@ +//! Singly linked list. +//! +//! Consult . + +use std::fmt::Debug; + +/// Node of the list. +#[derive(Debug)] +pub struct Node { + /// Value of current node. + pub value: T, + + /// Pointer to the next node. If it is `None`, there is no next node. + pub next: Option>>, +} + +impl Node { + /// Creates a new node. + pub fn new(value: T) -> Self { + Self { value, next: None } + } +} + +/// A singly-linked list. +#[derive(Debug)] +pub struct SinglyLinkedList { + /// Head node of the list. If it is `None`, the list is empty. + head: Option>, +} + +impl Default for SinglyLinkedList { + fn default() -> Self { + Self::new() + } +} + +impl SinglyLinkedList { + /// Creates a new list. + pub fn new() -> Self { + Self { head: None } + } + + /// Adds the given node to the front of the list. + pub fn push_front(&mut self, value: T) { + todo!() + } + + /// Adds the given node to the back of the list. + pub fn push_back(&mut self, value: T) { + todo!() + } + + /// Removes and returns the node at the front of the list. + pub fn pop_front(&mut self) -> Option { + todo!() + } + + /// Removes and returns the node at the back of the list. + pub fn pop_back(&mut self) -> Option { + todo!() + } + + /// Create a new list from the given vector `vec`. + pub fn from_vec(vec: Vec) -> Self { + todo!() + } + + /// Convert the current list into a vector. + pub fn as_vec(&self) -> Vec { + todo!() + } + + /// Return the length (i.e., number of nodes) of the list. + pub fn length(&self) -> usize { + todo!() + } + + /// Apply function `f` on every element of the list. + /// + /// # Examples + /// + /// `self`: `[1, 2]`, `f`: `|x| x + 1` ==> `[2, 3]` + pub fn map T>(&mut self, f: F) { + todo!() + } + + /// Insert given list `another` at the specified index `idx`. + /// If `idx` is out-of-bound of `self`, append `another` at the end of `self`. + /// + /// # Examples + /// + /// `self`: `[1, 2]`, `another`: `[3, 4]`, `idx`: `1` ==> `[1, 3, 4, 2]` + /// `self`: `[1, 2]`, `another`: `[3, 4]`, `idx`: `5` ==> `[1, 2, 3, 4]` + pub fn insert(&mut self, another: &Self, idx: usize) { + todo!() + } + + /// Reverse the list in a chunk of size `n`. + /// If `n == 0`, do nothing. + /// + /// # Examples + /// + /// `self`: `[1, 2, 3, 4, 5, 6, 7, 8, 9]`, `n`: `3` + /// // each chunk of size `3`: `[1, 2, 3]`, `[4, 5, 6]`, `[7, 8, 9]` + /// // reversed sequence of chunks: `[7, 8, 9]`, `[4, 5, 6]`, `[1, 2, 3]` + /// ==> `[7, 8, 9, 4, 5, 6, 1, 2, 3]`, + /// + /// `self`: `[1, 2, 3, 4, 5, 6, 7, 8, 9]`, `n`: `4` + /// // each chunk of size `4`: `[1, 2, 3, 4]`, `[5, 6, 7, 8]`, `[9]` + /// // reversed sequence of chunks: `[9]`, `[5, 6, 7, 8]`, `[1, 2, 3, 4]` + /// ==> `[9, 5, 6, 7, 8, 1, 2, 3, 4]` + pub fn chunk_reverse(&mut self, n: usize) { + todo!() + } + + /// Apply given function `f` for each adjacent pair of elements in the list. + /// If `self.length() < 2`, do nothing. + /// + /// # Examples + /// + /// `self`: `[1, 2, 3, 4]`, `f`: `|x, y| x + y` + /// // each adjacent pair of elements: `(1, 2)`, `(2, 3)`, `(3, 4)` + /// // apply `f` to each pair: `f(1, 2) == 3`, `f(2, 3) == 5`, `f(3, 4) == 7` + /// ==> `[3, 5, 7]` + pub fn pair_map T>(&mut self, f: F) { + todo!() + } +} + +// A list of lists. +impl SinglyLinkedList> { + /// Flatten the list of lists into a single list. + /// + /// # Examples + /// `self`: `[[1, 2, 3], [4, 5, 6], [7, 8]]` + /// ==> `[1, 2, 3, 4, 5, 6, 7, 8]` + pub fn flatten(self) -> SinglyLinkedList { + todo!() + } +} diff --git a/src/assignments/assignment11/linked_list_grade.rs b/src/assignments/assignment11/linked_list_grade.rs new file mode 100644 index 0000000..6805972 --- /dev/null +++ b/src/assignments/assignment11/linked_list_grade.rs @@ -0,0 +1,122 @@ +#[cfg(test)] +mod test_linked_list { + use crate::assignments::assignment11::linked_list::*; + + #[derive(Debug, PartialEq, Eq)] + struct V(usize); + + #[test] + fn test_push_pop() { + let mut list = SinglyLinkedList::new(); + list.push_back(V(3)); + list.push_front(V(2)); + list.push_back(V(4)); + list.push_front(V(1)); + list.push_back(V(5)); + + assert_eq!(list.pop_front(), Some(V(1))); + assert_eq!(list.pop_back(), Some(V(5))); + assert_eq!(list.pop_front(), Some(V(2))); + assert_eq!(list.pop_back(), Some(V(4))); + assert_eq!(list.pop_front(), Some(V(3))); + assert_eq!(list.pop_back(), None); + assert_eq!(list.pop_front(), None); + } + + #[test] + fn test_from_as_vec() { + assert_eq!(SinglyLinkedList::::new().as_vec(), vec![]); + assert_eq!( + SinglyLinkedList::from_vec(vec![1, 2, 3]).as_vec(), + vec![1, 2, 3] + ); + } + + #[test] + fn test_length() { + let list = SinglyLinkedList::from_vec(vec![1, 2, 3]); + assert_eq!(list.length(), 3); + } + + #[test] + fn test_map() { + let mut list = SinglyLinkedList::from_vec(vec![1, 2, 3]); + let incr = |x: i32| x + 1; + list.map(incr); + assert_eq!(list.as_vec(), vec![2, 3, 4]); + } + + #[test] + fn test_insert() { + let mut list1 = SinglyLinkedList::from_vec(vec![1, 2, 3]); + let mut list2 = SinglyLinkedList::from_vec(vec![1, 2, 3]); + let mut list3 = SinglyLinkedList::from_vec(vec![1, 2, 3]); + let list4 = SinglyLinkedList::from_vec(vec![4, 5, 6]); + + list1.insert(&list4, 0); + assert_eq!(list1.as_vec(), vec![4, 5, 6, 1, 2, 3]); + + list2.insert(&list4, 1); + assert_eq!(list2.as_vec(), vec![1, 4, 5, 6, 2, 3]); + + list3.insert(&list4, 4); + assert_eq!(list3.as_vec(), vec![1, 2, 3, 4, 5, 6]); + } + + #[test] + fn test_chunk_reverse() { + let mut list1 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]); + list1.chunk_reverse(3); + assert_eq!(list1.as_vec(), vec![7, 8, 9, 3, 4, 5, 1, 2, 3]); + + let mut list2 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8]); + list2.chunk_reverse(3); + assert_eq!(list2.as_vec(), vec![7, 8, 4, 5, 6, 1, 2, 3]); + + let mut list3 = SinglyLinkedList::from_vec(vec![1, 2, 3]); + list3.chunk_reverse(4); + assert_eq!(list3.as_vec(), vec![1, 2, 3]); + + let mut list4 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4]); + list4.chunk_reverse(1); + assert_eq!(list4.as_vec(), vec![4, 3, 2, 1]); + + let mut list5 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4]); + list4.chunk_reverse(0); + assert_eq!(list4.as_vec(), vec![1, 2, 3, 4]); + } + + #[test] + fn test_pair_map() { + let mut list = SinglyLinkedList::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]); + let add = |x: i32, y: i32| x + y; + + list.pair_map(add); + assert_eq!(list.as_vec(), vec![3, 5, 7, 9, 11, 13, 15, 17]); + + list.pair_map(add); + assert_eq!(list.as_vec(), vec![8, 12, 16, 20, 24, 28, 32]); + + list.pair_map(add); + assert_eq!(list.as_vec(), vec![20, 28, 36, 44, 52, 60]); + + list.pair_map(add); + assert_eq!(list.as_vec(), vec![48, 64, 80, 96, 112]); + } + + #[test] + fn test_flatten() { + let list1 = SinglyLinkedList::from_vec(vec![1, 2]); + let list2 = SinglyLinkedList::from_vec(vec![3]); + let list3 = SinglyLinkedList::from_vec(vec![4, 5, 6, 7]); + let list4 = SinglyLinkedList::::new(); + let list5 = SinglyLinkedList::from_vec(vec![8, 9, 10]); + + let list_list = SinglyLinkedList::from_vec(vec![list1, list2, list3, list4, list5]); + + assert_eq!( + list_list.flatten().as_vec(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + ); + } +} diff --git a/src/assignments/assignment11/mod.rs b/src/assignments/assignment11/mod.rs index be8f452..1802da6 100644 --- a/src/assignments/assignment11/mod.rs +++ b/src/assignments/assignment11/mod.rs @@ -1,17 +1,15 @@ //! Assignment 11: Familiarizing with smart pointers. //! //! You should fill out `todo!()` placeholders in such a way that `/scripts/grade-11.sh` works fine. -//! See `assignment11_grade.rs` and `/scripts/grade-11.sh` for the test script. +//! See `assignment11/*_grade.rs` and `/scripts/grade-11.sh` for the test script. //! Run `/scripts/prepare-submissions.sh` and submit `/target/assignment11.zip` to . -pub mod doubly_linked_list; -mod doubly_linked_list_grade; - pub mod graph; -mod graph_grade; - +pub mod linked_list; pub mod mock_storage; -pub mod mock_storage_grade; - pub mod tv_room; -pub mod tv_room_grade; + +mod graph_grade; +mod linked_list_grade; +mod mock_storage_grade; +mod tv_room_grade; diff --git a/src/assignments/assignment12/card.rs b/src/assignments/assignment12/card.rs index 1a00da9..c7f6e51 100644 --- a/src/assignments/assignment12/card.rs +++ b/src/assignments/assignment12/card.rs @@ -1,6 +1,6 @@ //! Flipping card game. //! -//! For this assignment, you have to see `play` function in `card_grade.rs` file. +//! HINT: For this assignment, you have to see `play` function in `card_grade.rs` file. //! Multiple threads will be created and they will run as enemy bots(`bot_threads`). //! Strategy of the enemy bots is implemented in the closure of the `thread::spawn` function. //! Your goal is to beat them so that there are more white cards than blue cards in the ground. diff --git a/src/assignments/assignment12/mod.rs b/src/assignments/assignment12/mod.rs index 7b82d6e..3dd2a96 100644 --- a/src/assignments/assignment12/mod.rs +++ b/src/assignments/assignment12/mod.rs @@ -3,13 +3,14 @@ //! The primary goal of this assignment is to get used to concurrency. //! //! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade-12.sh` works fine. -//! See `assignment12_grade.rs` and `/scripts/grade-12.sh` for the test script. +//! See `assignment12/*_grade.rs` and `/scripts/grade-12.sh` for the test script. pub mod card; -mod card_grade; pub mod demux; -mod demux_grade; pub mod funnel; -mod funnel_grade; pub mod small_exercises; + +mod card_grade; +mod demux_grade; +mod funnel_grade; mod small_exercises_grade; diff --git a/src/assignments/assignment13/mod.rs b/src/assignments/assignment13/mod.rs index 504c1e3..3ea15d4 100644 --- a/src/assignments/assignment13/mod.rs +++ b/src/assignments/assignment13/mod.rs @@ -4,7 +4,7 @@ //! //! Refer to your solution for assignment 09. You will implement the parallelized version of assignment 09. //! 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. +//! See `assignment13/small_exercises_grade.rs` and `/scripts/grade-13.sh` for the test script. pub mod small_exercises; mod small_exercises_grade;