"LISTEN UP, YOU MAGNIFICENT, STRESSED-OUT CODERS!" Santa bellowed. "Tomorrow, we test the sleigh again. And this time, it WILL work. No more Florida nonsense. Palm trees are NOT part of the Christmas aesthetic."
He pointed dramatically at Bernard and Pepper. “You two are coming with me. If the sleigh glitches mid-flight again, I want live debugging happening in real time. No excuses.”
The elves whispered nervously. Everyone still remembered the "Florida Incident"—last week’s failed test landing in a snowless golf course. Santa didn’t appreciate the HOA complaints.
“This time,” Santa continued, pacing, “Snowball compiled every scrap of data we need for each location. Top-notch metrics. But metrics mean nothing without an algorithm. Your job is to write a function to find the snowball-densest landing spot.”
Snowball has provided you with a Vec<Location>
, now the other elves need to write a function to find the most dense area with snow.
Here is what you need to to:
new()
associated function for the Location
struct that takes x: f64
, y: f64
, z: f64
, area: f64
, and snow
.snow
parameter must be able to accept all SnowKg
, SnowLb
and Snowball
types.Location
struct named density()
that gets the density of snow in the location.find_best_location
function which takes a Vec<Location>
and returns a Result<Location, Box<dyn Error>>
.That's it! 🎅
If you’re stuck or need a starting point, here are some hints to help you along the way!
When we implemented the From<T>
trait in the previous challenge, Rust automatically implemented the Into<T>
trait for us. This means that if you implement From<T>
for a type, you can convert that type to T
using the into()
method. e.g. let snow = snow.into();
.
For the snow
parameter, you can accept anything that implements the Into<Snowball>
trait. e.g. new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self
. Here impl Into<Snowball>
means any type that implements the Into<Snowball>
trait.
Convert the snow
parameter to a Snowball
type using the into()
method. e.g. let snow = snow.into();
.
Implement the density()
method for the Location
struct. The density of snow is calculated by dividing the snow weight by the area of the location. Make sure you handle the division by zero case.
Use into_iter
to convert the Vec<Location>
to a consuming iterator. e.g. locations.into_iter()
.
Use the max_by()
method to find the location with the highest density of snow. e.g.
locations
.into_iter()
.max_by(|a, b| {
a.density()
.partial_cmp(&b.density())
.unwrap_or(Ordering::Equal)
})
Import Ordering::Equal
using use std::cmp::Ordering
.
Use the ok_or()
method to convert the Option
returned by max_by()
to a Result
. e.g. ok_or("No locations found".into())
.
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { return Location { x, y, z, area, snow: snow.into(), }; } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. return if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area }; }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}pub fn main() {}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new<T>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self // Generic function with a trait bound. If struct impl's From, it gets Into for free. where T: Into<Snowball> { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area <= 0.0 { return 0.0 } (*self.snow as f64) / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. // Comparison not straightforward, e.g. if the f64 is f64::NAN or infinity // Hence the unwrap_or(Equal). This passes the tests, but maybe it's better to filter out such values? // Treating NAN/inf as "Equal" may give the wrong result depending on the input order. /* let result = locations .into_iter() .max_by(|a, b| a.density().partial_cmp(&(b.density())).unwrap_or(std::cmp::Ordering::Equal)) .ok_or("Error finding best location".into()); if result.as_ref().is_ok_and(|loc| {let d = loc.density(); d.is_normal() || d == 0.0}) { result } else { // Probably don't want to return a best location if it has NAN/infinite density Err("Best location has invalid snowball density".into()) } */ // Not performant to recalculate density, but safer to filter out invalid densities locations .into_iter() // As we want to return a Location and own the Vec (which was moved as a param) .filter(|a| {let d = a.density(); d.is_normal() || d == 0.0}) // Not NAN/inf .max_by(|a, b| a.density().total_cmp(&(b.density()))) // From trait is defined to convert str's to Box<dyn Error>'s, so can do into() // rather than creating a custom "LocationError" type. // https://doc.rust-lang.org/std/primitive.str.html#impl-From%3C%26str%3E-for-Box%3Cdyn+Error%3E // ok_or() transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err). .ok_or("Error finding best location".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } self.snow.0 as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let highest = locations.into_iter().max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal)); highest.ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => 0.0, _ => *self.snow as f64 / self.area, } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::cmp::Ordering;use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { let best = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); best.ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { return Location { x: x, y: y, z: z, area: area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { return 0.0 } return *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new<T>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self where T: Into<Snowball> { Location { x: x, y: y, z: z, area: area, snow: snow.into() } } // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => return 0.0, _ => return *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let mut highscore: f64 = 0.0; let mut best_idx: Option<usize> = None; if locations.len() == 0 { return Err("Empty locations vector".into()); } for (idx, location) in locations.clone().iter().enumerate() { let mut new_density: f64 = location.density(); dbg!(&new_density); if new_density >= highscore { highscore = new_density; best_idx = Some(idx); } } Ok(locations[best_idx.unwrap()].clone())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found.".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } (*self.snow as f64) / self.area }}impl Ord for Location { fn cmp(&self, other: &Self) -> Ordering { let density = self.density(); let other_density = other.density(); if density.is_nan() { return Ordering::Less; } if other_density.is_nan() { return Ordering::Greater; } density.partial_cmp(&other_density).unwrap() }}impl PartialOrd for Location { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }}impl PartialEq for Location { fn eq(&self, other: &Self) -> bool { self.cmp(other) == Ordering::Equal }}impl Eq for Location {}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.iter().max() .cloned() .ok_or("No maximum".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z:f64, area: f64, snow: impl Into<Snowball>) -> Self { Self{ x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let mut result: Option<Location> = None; let mut max_density: f64 = 0.0; let mut locations_iter = locations.into_iter(); while let Some(loc) = locations_iter.next() { let density = loc.density(); if density >= max_density { max_density = density; result = Some(loc); } } result.ok_or_else(|| "No valid location".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x: x, y: y, z: z, area: area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } return self.snow.0 as f64 / self.area; }}use core::cmp::Ordering;pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new<S: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: S) -> Self { Location { x: x, y: y, z: z, area: area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Less) }).ok_or_else(|| "Couldn't".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new <T: Into<Snowball>> (x : f64, y : f64, z : f64, area : f64, snow : T ) -> Self{ Location {x, y, z, area, snow : snow.into()} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { (*self.snow as f64) / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Less) }).ok_or_else(|| "Couldn't".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new<T: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Less) }).ok_or_else(|| "Couldn't".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0f64 || *self.snow == 0 { 0.0f64 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { if locations.is_empty() { return Err("No locations provided".into()); } let best = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::Ordering::Equal) }) .unwrap(); Ok(best.clone())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x:f64, y:f64, z:f64, area:f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); return Location { x, y, z, area, snow } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a,b| { a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { let snow = snow.into(); Location{x, y, z, area, snow} } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { ((*self.snow) as f64)/ self.area } // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal)).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x:f64, y:f64, z:f64, area:f64, snow:impl Into<Snowball>)->Self{ Self{ x:x, y:y, z:z, area:area, snow:snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0{ return 0.0; }else{ ((*self.snow) as f64)/self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter() .max_by(|a, b| a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal)) .ok_or("Cannot compute the maximum. The locations seems empty.".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new<A>(x: f64, y: f64, z: f64, area: f64, snow: A) -> Self where Snowball: From<A> { Self { x, y, z, area, snow: snow.into() } } // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } self.snow.0 as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.iter().max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal)).cloned().ok_or("nope".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Self { x: x, y: y, z: z, area: area, snow: snow } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { let density = *self.snow as f64 / self.area; density } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { let best_location = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); let best_Result = best_location.ok_or("no location found"); Ok(best_Result?)}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { // 1. Implement the `new()` method. // Create a new `Location` instance with the given parameters. // Convert the snow parameter to a `Snowball` using the `Into` trait. Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } let density = (*self.snow as f64) / self.area; density }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. // Return an error if the locations vector is empty. if locations.is_empty() { return Err("No locations provided".into()); } let result = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::Ordering::Equal) }); Ok(result.unwrap())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use core::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x:f64, y:f64, z:f64, area:f64, snow: impl Into<Snowball>)->Self{ let snow = snow.into(); Self{x, y, z, area,snow} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0{ 0.0 }else{ *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. match locations.into_iter().max_by(|a,b|{ a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }){ Some(location) => Result::Ok(location), None => Result::Err(Box::<dyn Error>::from("No locations available")) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use core::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. // let snowiest_location_idx: Option<usize> = locations match locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) { Some(location) => Result::Ok(location), None => Result::Err(Box::<dyn Error>::from("No locations available")) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq, PartialOrd)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, f64, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new<T>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self where T: Into<Snowball>, { Self { x, y, z, area, snow: snow.into(), } } // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. }}// pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> {// if locations.is_empty() {// return Err("No locations available".into());// }// let mut best_location = &locations[0];// let mut highest_density = best_location.density();// for location in &locations[1..] {// let density = location.density();// if density > highest_density {// highest_density = density;// best_location = location;// }// }// Ok(best_location.clone())// }pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { if locations.is_empty() { return Err("No locations available".into()); } locations .iter() .max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal)) .map(|best_location| best_location.clone()) .ok_or_else(|| "Failed to find a location".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0f64 { return 0f64 } return (*self.snow as f64) / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("no locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } return *self.snow as f64 / self.area; }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>)->Self { Location {x,y,z,area, snow: snow.into()} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area==0.0 {0.0} else { (self.snow.0 as f64) / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { Location {x, y, z, area, snow: snow.into()} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => 0.0, x => *self.snow as f64 / x } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let max = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); max.ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow : impl Into<Snowball>) -> Self { Location{ x, y, z, area, snow : snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area==0.0 { true => 0.0 as f64, false => (*self.snow as f64) / self.area, } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::Ordering::Equal) }) .ok_or_else(|| "No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location { x, y, z, area, snow, } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0{return 0.0} (*self.snow as f64) / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let max_object = locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::Ordering::Equal) }) .ok_or("No location found")?; Ok(max_object.clone())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location { x, y, z, area, snow, } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } else { let Snowball(weight) = self.snow; return (weight as f64) /self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location{ x, y, z, area, snow, } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } else { let Snowball(weight) = self.snow; return (weight as f64) / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. let weight = *self.snow as f64; let area = if self.area == 0.0 { return 0.0} else {self.area}; weight/area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::max;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x:f64, y:f64, z:f64, area:f64, snow: impl Into<Snowball>)->Self{ Location{ x, y, z, area, snow:snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0{ return 0.0; }else{ let density = self.snow.0 as f64 / self.area; return density.abs(); } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let mut max_density_loc = Location::new(0.0,0.0,0.0,0.0, Snowball::new(0)); let mut found_max = false; for loc in locations{ if loc.density() >= max_density_loc.density(){ found_max=true; max_density_loc = loc; } } if found_max{ Ok(max_density_loc) }else{ Err("Nothing found".to_string().into()) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location { x, y, z, area, snow } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. let snow = self.snow.abs() as f64; if self.area == 0.0 { return 0.0 } else { return snow / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let mut highest_loc = Location::new(0.0, 0.0, 0.0, 0.0, Snowball::new(0)); let mut found = false; for location in locations { if location.density() >= highest_loc.density() { found = true; highest_loc = location; } } if found == true { Ok(highest_loc) } else { Err("Not found".to_string().into()) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("location not found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("No location found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snowballs: Snowball = snow.into(); Location { x: x, y: y, z: z, area: area, snow: snowballs, } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } let float_snow = *(self.snow) as f64; float_snow / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. if locations.len() == 0 { return Err("No locations in argument vector.")?; } let dense_loc = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); dense_loc.ok_or("No locations found".into()) // return Ok(dense_loc.expect("Iterator should not fail to find max."));}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new<T>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self where T: Into<Snowball> { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => 0.0, _ => *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("Array empty".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::cmp::Ordering;use std::collections::HashMap;use std::hash::Hash;use std::iter::Map;use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. if locations.is_empty() { return Err(Box::from("no locations provided")); } let landing_location = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); landing_location.ok_or_else(|| Box::from("no valid locations found"))}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}fn main() {}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } return self.snow.to_kg() / self.area ; }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let loc = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); if loc.is_none() { return Err(Box::new(std::io::Error::new(std::io::ErrorKind::NotFound, "No locations"))); } Ok(loc.expect("bad location"))}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) } pub fn to_kg(&self) -> f64 { self.0 as f64 * SNOWBALL_WEIGHT_KG }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => 0.0, a => (*self.snow as f64) / a, } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { match self.area { 0.0 => 0.0, a => (*self.snow as f64) / a, } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location { x, y, z, area, snow, } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new( x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball> ) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } (self.snow.0 as f64) / self.area }}pub fn find_best_location( locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a,b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new (x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location{ x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } else { *self.snow as f64/ self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } self.snow.0 as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let result = locations .into_iter() .max_by(|x, y| { x.density() .partial_cmp(&y.density()) .unwrap_or(Ordering::Equal) }) .ok_or("no locations found".into()); result}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { (*self.snow as f64) / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let result = locations .into_iter() .max_by(|x, y| { x.density() .partial_cmp(&y.density()) .unwrap_or(Ordering::Equal) }) .ok_or("no locations found".into()); result}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}