From 064b0392f814481599399e543cb4459a300d4789 Mon Sep 17 00:00:00 2001 From: Christian Nieves Date: Mon, 26 Dec 2022 15:46:09 -0600 Subject: [PATCH] Start day 2 exercises --- Cargo.toml | 8 ++ src/day2/exercises/health-statistics.rs | 53 +++++++++ src/day2/exercises/points-polygons.rs | 148 ++++++++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 src/day2/exercises/health-statistics.rs create mode 100644 src/day2/exercises/points-polygons.rs diff --git a/Cargo.toml b/Cargo.toml index c6c7d9a..d113dfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,3 +82,11 @@ path = "src/day2/destructure-arrays.rs" [[bin]] name = "match-guards" path = "src/day2/match-guards.rs" + +[[bin]] +name = "health-statistics" +path = "src/day2/exercises/health-statistics.rs" + +[[bin]] +name = "points-polygons" +path = "src/day2/exercises/points-polygons.rs" diff --git a/src/day2/exercises/health-statistics.rs b/src/day2/exercises/health-statistics.rs new file mode 100644 index 0000000..0bce65e --- /dev/null +++ b/src/day2/exercises/health-statistics.rs @@ -0,0 +1,53 @@ +// TODO: remove this when you're done with your implementation. +#![allow(unused_variables, dead_code)] + +struct User { + name: String, + age: u32, + weight: f32, +} + +impl User { + pub fn new(name: String, age: u32, weight: f32) -> Self { + User { name, age, weight } + } + + pub fn name(&self) -> &str { + self.name.as_str() + } + + pub fn age(&self) -> u32 { + self.age + } + + pub fn weight(&self) -> f32 { + self.weight + } + + pub fn set_age(&mut self, new_age: u32) { + self.age = new_age + } + + pub fn set_weight(&mut self, new_weight: f32) { + self.weight = new_weight + } +} + +fn main() { + let bob = User::new(String::from("Bob"), 32, 155.2); + println!("I'm {} and my age is {}", bob.name(), bob.age()); +} + +#[test] +fn test_weight() { + let bob = User::new(String::from("Bob"), 32, 155.2); + assert_eq!(bob.weight(), 155.2); +} + +#[test] +fn test_set_age() { + let mut bob = User::new(String::from("Bob"), 32, 155.2); + assert_eq!(bob.age(), 32); + bob.set_age(33); + assert_eq!(bob.age(), 33); +} diff --git a/src/day2/exercises/points-polygons.rs b/src/day2/exercises/points-polygons.rs new file mode 100644 index 0000000..ecdfcbc --- /dev/null +++ b/src/day2/exercises/points-polygons.rs @@ -0,0 +1,148 @@ +// TODO: remove this when you're done with your implementation. +#![allow(unused_variables, dead_code)] + +use std::ops::Add; + +#[derive(Debug)] +pub struct Point { + x: u32, + y: u32 +} + +impl Point { + fn new(x: u32, y: u32) -> Point { + Point{x, y} + } + + fn dist(&self, p2: &Point) -> f64 { + f64::sqrt( + ( + (p2.x - self.x).pow(2) + + (p2.y - self.y).pow(2) + ) as f64) + } +} + +impl Add for Point { + type Output = Self; + + fn add(self, other: Self) -> Self { + Self{ + x: self.x + other.x, + y: self.y + other.y, + } + } + +} + +#[derive(Debug)] +pub struct Polygon { + points: Vec +} + +impl Polygon { + fn new() -> Polygon { + Polygon { points: vec![] } + } + + fn add_point(&mut self, p: Point) { + self.points.push(p) + } + + fn left_most_point(&self) -> Option<&Point> { + let mut smallest_x = None; + for p in self.points.iter() { + if smallest_x.is_none() { + smallest_x = Option::from(p) + } else if smallest_x.unwrap().x > p.x { + smallest_x = Option::from(p) + } + } + smallest_x + } +} + +#[derive(Debug)] +pub struct Circle { + center: Point, + radius: f64, +} + +impl Circle { + // add methods + fn new(center: Point, radius: f64) -> Circle { + Circle{center, radius}; + } +} + +pub enum Shape { + Polygon(Polygon), + Circle(Circle), +} + +#[cfg(test)] +mod tests { + use super::*; + + fn round_two_digits(x: f64) -> f64 { + (x * 100.0).round() / 100.0 + } + + #[test] + fn test_point_dist() { + let p1 = Point::new(10, 10); + let p2 = Point::new(14, 13); + assert_eq!(round_two_digits(p1.dist(p2)), 5.00); + } + + #[test] + fn test_point_add() { + let p1 = Point::new(16, 16); + let p2 = p1 + Point::new(-4, 3); + assert_eq!(p2, Point::new(12, 19)); + } + + #[test] + fn test_polygon_left_most_point() { + let p1 = Point::new(12, 13); + let p2 = Point::new(16, 16); + + let mut poly = Polygon::new(); + poly.add_point(p1); + poly.add_point(p2); + assert_eq!(poly.left_most_point(), Some(p1)); + } + + #[test] + fn test_polygon_iter() { + let p1 = Point::new(12, 13); + let p2 = Point::new(16, 16); + + let mut poly = Polygon::new(); + poly.add_point(p1); + poly.add_point(p2); + + let points = poly.iter().cloned().collect::>(); + assert_eq!(points, vec![Point::new(12, 13), Point::new(16, 16)]); + } + + // #[test] + // fn test_shape_circumferences() { + // let mut poly = Polygon::new(); + // poly.add_point(Point::new(12, 13)); + // poly.add_point(Point::new(16, 16)); + // let shapes = vec![ + // Shape::from(poly), + // Shape::from(Circle::new(Point::new(10, 20), 5)), + // ]; + // let circumferences = shapes + // .iter() + // .map(Shape::circumference) + // .map(round_two_digits) + // .collect::>(); + // assert_eq!(circumferences, vec![10.0, 31.42]); + // } +} + +#[allow(dead_code)] +fn main() {}