mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-12 21:08:45 +00:00
Merge branch 'main' into 'main'
polish See merge request kaist-cp-class/cs220-private!22
This commit is contained in:
@@ -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: <https://doc.rust-lang.org/std/primitive.usize.html>
|
||||
//! 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;
|
||||
|
||||
@@ -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: <https://doc.rust-lang.org/std/primitive.usize.html>
|
||||
|
||||
/// Adds two unsigned words. If overflow happens, just wrap around.
|
||||
pub fn add(lhs: usize, rhs: usize) -> usize {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -41,6 +41,7 @@ impl Mat2 {
|
||||
impl Mul<Mat2> for Mat2 {
|
||||
type Output = Mat2;
|
||||
|
||||
/// Consult <https://www.mathsisfun.com/algebra/matrix-multiplying.html>
|
||||
fn mul(self, rhs: Mat2) -> Self::Output {
|
||||
todo!()
|
||||
}
|
||||
@@ -50,6 +51,8 @@ impl Mul<Vec2> for Mat2 {
|
||||
type Output = Vec2;
|
||||
|
||||
/// Multiplies the matrix by the vector.
|
||||
///
|
||||
/// Consult <https://www.mathsisfun.com/algebra/matrix-multiplying.html>
|
||||
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: <https://www.mathcentre.ac.uk/resources/uploaded/sigma-matrices7-2009-1.pdf>
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 <https://gg.kaist.ac.kr>.
|
||||
|
||||
pub mod context;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -156,7 +156,7 @@ impl<C: Semiring> From<C> for Polynomial<C> {
|
||||
/// - 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<C: Semiring> std::str::FromStr for Polynomial<C> {
|
||||
|
||||
@@ -79,8 +79,8 @@ pub trait Differentiable: Clone {
|
||||
fn diff(&self) -> Self;
|
||||
}
|
||||
|
||||
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Constant_term_rule>
|
||||
impl Differentiable for Rational {
|
||||
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Constant_term_rule>
|
||||
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;
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ enum Yielded<T> {
|
||||
}
|
||||
|
||||
/// Generator
|
||||
/// - kk
|
||||
/// - You can call `next()` method to get the next value.
|
||||
/// - The generator should stop when it yields `Yielded::Stop`.
|
||||
///
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -52,7 +52,7 @@ pub fn fib<T>(first: T, second: T) -> impl Iterator<Item = T>
|
||||
where
|
||||
T: std::ops::Add<Output = T> + 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<Item = isize> {
|
||||
todo!("remove below");
|
||||
todo!("replace `std::iter::empty() with your owm implementation`");
|
||||
std::iter::empty()
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -39,11 +39,8 @@ impl BigInt {
|
||||
}
|
||||
|
||||
/// Creates a new `BigInt` from a `Vec<u32>`.
|
||||
///
|
||||
/// # Panic
|
||||
///
|
||||
/// Panics if `carrier` is empty.
|
||||
pub fn new_large(carrier: Vec<u32>) -> Self {
|
||||
assert!(!carrier.is_empty());
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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: <https://en.wikipedia.org/wiki/100_prisoners_problem>
|
||||
//!
|
||||
//! 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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Item = (u64, u64, u64)> {
|
||||
Pythagorean::new()
|
||||
}
|
||||
|
||||
@@ -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<T> = Option<Rc<RefCell<Node<T>>>>;
|
||||
|
||||
/// Node of a doubly-linked list.
|
||||
#[derive(Debug)]
|
||||
pub struct Node<T: Debug + Clone> {
|
||||
/// Value of current node.
|
||||
value: RefCell<T>,
|
||||
|
||||
/// Pointer to the next node. If it is `None`, there is no next node.
|
||||
next: Link<T>,
|
||||
|
||||
/// Pointer to the previous node. If it is `None`, there is no previous node.
|
||||
prev: Link<T>,
|
||||
}
|
||||
|
||||
impl<T: Debug + Clone> Node<T> {
|
||||
/// 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<T> {
|
||||
self.prev.clone()
|
||||
}
|
||||
|
||||
/// Fetch next node
|
||||
pub fn next(&self) -> Link<T> {
|
||||
self.next.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// A doubly-linked list.
|
||||
#[derive(Default, Debug)]
|
||||
pub struct DoublyLinkedList<T: Debug + Clone> {
|
||||
/// Head node of the list. If it is `None`, the list is empty.
|
||||
head: Link<T>,
|
||||
|
||||
/// Tail node of the list. If it is `None`, the list is empty.
|
||||
tail: Link<T>,
|
||||
}
|
||||
|
||||
impl<T: Debug + Clone> DoublyLinkedList<T> {
|
||||
/// 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<T> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Removes and returns the node at the back of the list.
|
||||
pub fn pop_back(&mut self) -> Option<T> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug + Clone> Drop for DoublyLinkedList<T> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
140
src/assignments/assignment11/linked_list.rs
Normal file
140
src/assignments/assignment11/linked_list.rs
Normal file
@@ -0,0 +1,140 @@
|
||||
//! Singly linked list.
|
||||
//!
|
||||
//! Consult <https://doc.rust-lang.org/book/ch15-01-box.html>.
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
/// Node of the list.
|
||||
#[derive(Debug)]
|
||||
pub struct Node<T: Debug> {
|
||||
/// Value of current node.
|
||||
pub value: T,
|
||||
|
||||
/// Pointer to the next node. If it is `None`, there is no next node.
|
||||
pub next: Option<Box<Node<T>>>,
|
||||
}
|
||||
|
||||
impl<T: Debug> Node<T> {
|
||||
/// Creates a new node.
|
||||
pub fn new(value: T) -> Self {
|
||||
Self { value, next: None }
|
||||
}
|
||||
}
|
||||
|
||||
/// A singly-linked list.
|
||||
#[derive(Debug)]
|
||||
pub struct SinglyLinkedList<T: Debug> {
|
||||
/// Head node of the list. If it is `None`, the list is empty.
|
||||
head: Option<Node<T>>,
|
||||
}
|
||||
|
||||
impl<T: Debug> Default for SinglyLinkedList<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug> SinglyLinkedList<T> {
|
||||
/// 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<T> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Removes and returns the node at the back of the list.
|
||||
pub fn pop_back(&mut self) -> Option<T> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Create a new list from the given vector `vec`.
|
||||
pub fn from_vec(vec: Vec<T>) -> Self {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Convert the current list into a vector.
|
||||
pub fn as_vec(&self) -> Vec<T> {
|
||||
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<F: Fn(T) -> 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<F: Fn(T, T) -> T>(&mut self, f: F) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
// A list of lists.
|
||||
impl<T: Debug> SinglyLinkedList<SinglyLinkedList<T>> {
|
||||
/// 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<T> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
122
src/assignments/assignment11/linked_list_grade.rs
Normal file
122
src/assignments/assignment11/linked_list_grade.rs
Normal file
@@ -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::<i32>::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::<i32>::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]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 <https://gg.kaist.ac.kr>.
|
||||
|
||||
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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user