mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-15 22:48:45 +00:00
polish
This commit is contained in:
@@ -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!()
|
||||
}
|
||||
|
||||
@@ -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!()
|
||||
}
|
||||
}
|
||||
@@ -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.));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user