"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.
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())
.
jgpaiva
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.0 { return 0.0; } if self.snow.0 == 0 { return 0.0; } //assert!(self.snow.0 <= (1 << 53), "{} {}", self.snow.0, self.area); let snow = self.snow.0 as f64; return snow / 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. if locations.len() == 0 { return Err("lol".to_string().into()); } Ok(locations.into_iter().filter(|v| !v.density().is_nan()).max_by(|a,b| a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal)).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) }}
mei28
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 { 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. let max = locations.iter().max_by(|&a, &b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }); let max = max.ok_or("")?; Ok(max.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) }}
josephcopenhaver
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>> { let mut it = locations.iter().filter(|x| !x.density().is_nan()).peekable(); let mut it2 = it.clone().cloned(); // 3. Find the location with the highest snow density. Ok(if it.peek().is_none() { if locations.len() > 0 { locations[0].clone() } else { return Err(Box::from("locations empty: no best location")) } } else if let Some(v) = it.max_by(|a,b| a.density().partial_cmp(&b.density()).unwrap()).cloned() { v } else { it2.nth(0).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) }}
kapaseker
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 { 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. let max = locations.iter().max_by(|&a, &b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }); let max = max.ok_or("")?; Ok(max.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) }}
franlopezm
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 self.area; } *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) }}
Ferdinanddb
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, 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) }}
MGehrmann
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, 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 // Avoid division by zero } else { (*self.snow as f64 * SNOWBALL_WEIGHT_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. if locations.is_empty() { return Err("No locations provided".into()); } let best_location = locations .into_iter() .max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal)); best_location.ok_or_else(|| "No valid 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) }}
doroshtapgh
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<S: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: S) -> Location { 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(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) }}
MaoX-Yu
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 snowball = snow.into(); Self { x, y, z, area, snow: 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 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".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) }}
Ankit8848
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, 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 0.0 == self.area { 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("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) }}
habu1010
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 { 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. locations.into_iter().max_by(|a, b| { let a = a.density(); let b = b.density(); if a < b { Ordering::Less } else if a > b { Ordering::Greater } else { 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) }}
rjensen
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, 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 0.0 == self.area { 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) }}
arm01846
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 { 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) }}
ankeetparikh
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 { 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 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) }}
T4D4-IU
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(); 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. use std::cmp::Ordering; let best_location = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into()); return best_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) }}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) }}
eguefif
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 { 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>> { 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) }}
gmvar
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. 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. 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. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::Ordering::Equal) }) .ok_or("No locations given".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) }}
Nismirno
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 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) }}
tvdu29
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<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 { 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>> { 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) }}
gsspdev
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(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { let snow = snow.into(); Location {x, y, z, area, snow} } // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` -- Handled by using the impl Into<Snowball> type definition which includes all types that implement From and therefore Into automatically pub fn density(&self) -> f64 { if self.area == 0.0 { return 0.0 } self.snow.0 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>> { let best = locations .into_iter() .max_by(|a,b| { a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("Highest density location not found"); return Ok(best?) // 3. Find the location with the highest snow density.}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) }}
KushnerykPavel
use std::{error::Error, ops::Deref, cmp::Ordering, num::FpCategory};#[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, 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. if let Some(location) = locations .into_iter() .max_by(|a, b| { let (lhs, rhs) = (a.density(), b.density()); match (lhs.classify(), rhs.classify()) { (FpCategory::Infinite, _) => Ordering::Greater, (_, FpCategory::Infinite) => Ordering::Less, (FpCategory::Normal, FpCategory::Normal) => match (lhs - rhs).signum() { 1.0 => Ordering::Greater, 0.0 => Ordering::Equal, _ => Ordering::Less, }, (FpCategory::Subnormal, FpCategory::Normal) => if rhs.is_sign_negative() { Ordering::Greater } else { Ordering::Less }, (FpCategory::Normal, FpCategory::Subnormal) => if lhs.is_sign_positive() { Ordering::Greater } else { Ordering::Less }, _ => Ordering::Equal } }) { Ok(location) } else { Err("List of Locations is empty")? }}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) }}
hafihaf123
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 where Snowball: From<T> { let snow = Snowball::from(snow); 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 { 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. Ok( locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::Ordering::Equal) }) .ok_or("Highest density location not 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) }}
Muhammad-Dennis
use std::{error::Error, ops::Deref, cmp::Ordering, num::FpCategory};#[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 { 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. if let Some(location) = locations .into_iter() .max_by(|a, b| { let (lhs, rhs) = (a.density(), b.density()); match (lhs.classify(), rhs.classify()) { (FpCategory::Infinite, _) => Ordering::Greater, (_, FpCategory::Infinite) => Ordering::Less, (FpCategory::Normal, FpCategory::Normal) => match (lhs - rhs).signum() { 1.0 => Ordering::Greater, 0.0 => Ordering::Equal, _ => Ordering::Less, }, (FpCategory::Subnormal, FpCategory::Normal) => if rhs.is_sign_negative() { Ordering::Greater } else { Ordering::Less }, (FpCategory::Normal, FpCategory::Subnormal) => if lhs.is_sign_positive() { Ordering::Greater } else { Ordering::Less }, _ => Ordering::Equal } }) { Ok(location) } else { Err("List of Locations is empty")? }}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) }}
joanne-cmd
use std::{error::Error, ops::Deref, cmp::Ordering, num::FpCategory};#[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 { 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.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. if let Some(location) = locations .into_iter() .max_by(|a, b| { let (lhs, rhs) = (a.density(), b.density()); match (lhs.classify(), rhs.classify()) { (FpCategory::Infinite, _) => Ordering::Greater, (_, FpCategory::Infinite) => Ordering::Less, (FpCategory::Normal, FpCategory::Normal) => match (lhs - rhs).signum() { 1.0 => Ordering::Greater, 0.0 => Ordering::Equal, _ => Ordering::Less, }, (FpCategory::Subnormal, FpCategory::Normal) => if rhs.is_sign_negative() { Ordering::Greater } else { Ordering::Less }, (FpCategory::Normal, FpCategory::Subnormal) => if lhs.is_sign_positive() { Ordering::Greater } else { Ordering::Less }, _ => Ordering::Equal } }) { Ok(location) } else { Err("List of Locations is empty")? }}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) }}
Burnus
use std::{error::Error, ops::Deref, cmp::Ordering, num::FpCategory};#[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 { 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.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. if let Some(location) = locations .into_iter() .max_by(|a, b| { let (lhs, rhs) = (a.density(), b.density()); match (lhs.classify(), rhs.classify()) { (FpCategory::Infinite, _) => Ordering::Greater, (_, FpCategory::Infinite) => Ordering::Less, (FpCategory::Normal, FpCategory::Normal) => match (lhs - rhs).signum() { 1.0 => Ordering::Greater, 0.0 => Ordering::Equal, _ => Ordering::Less, }, (FpCategory::Subnormal, FpCategory::Normal) => if rhs.is_sign_negative() { Ordering::Greater } else { Ordering::Less }, (FpCategory::Normal, FpCategory::Subnormal) => if lhs.is_sign_positive() { Ordering::Greater } else { Ordering::Less }, _ => Ordering::Equal } }) { Ok(location) } else { Err("List of Locations is empty")? }}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) }}
felipesaruhashi
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(); 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 { 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. match locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) { Some(l) => Ok(l), None => Err("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) }}
KLcpb
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(); 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 { 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. match locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) { Some(l) => Ok(l), None => Err("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) }}
tamanishi
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: 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>> { // 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(l) => Ok(l), None => Err("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) }}
Moloson
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 { 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(std::cmp::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) }}
rirze
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 => 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) }}
sashaaKr
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 where T:Into<Snowball> { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { match self.area { 0.0 => 0.0, _ => *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 found".into()); } // Use `max_by` with a partial_cmp on density() let best = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) // If density() is NaN (shouldn't be in normal usage), // treat it as equal or handle however you like. .unwrap_or(std::cmp::Ordering::Equal) }) .unwrap(); Ok(best)}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) }}
MacGurk
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 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. locations.iter().max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal)) .cloned() .ok_or_else(|| "Nooooooooooooooooooooo".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) }}
vaclav0411
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, y, z, area, snow, } } 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("error".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) }}
iamsahu
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 where T:Into<Snowball> { 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 } else { 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(std::cmp::Ordering::Equal)) .ok_or_else(|| "No locations provided".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) }}
kanakshilledar
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 where T: Into<Snowball>, { 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(std::cmp::Ordering::Equal)) .ok_or_else(|| "No locations provided".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) }}
martynovs
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 { 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>> { locations .into_iter() .max_by(|l1, l2| l1.density().partial_cmp(&l2.density()).unwrap_or(std::cmp::Ordering::Equal)) .ok_or("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) }}
phate45
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(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 { 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::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) }}
sarub0b0
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, 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>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::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) }}
wlabranche
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 } 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) }}
RedNapPanda
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 } 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 max 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) }}
strachan
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(); 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 { 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) }}
galenseilis
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: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 { 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. return 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) }}
wendko
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 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 new( x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball> ) -> Self { Self { x, y, z, area, snow: snow.into() } }}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("Failed to find locations".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) }}
lulingar
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. let maxl = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or_else(|| "boo")?; Ok(maxl)}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) }}
Cameo007
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: 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 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.iter().max_by(|location_a, location_b| location_a.density().partial_cmp(&location_b.density()).unwrap_or(Ordering::Equal)).ok_or("locatations is empty!".into()).cloned() // 3. Find the location with the highest snow density.}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) }}
Orrimp
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 { let snow = snow.into(); return Location{x, y, z, area, snow}; } // 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_f64 { return *self.snow as f64 / self.area; } return 0.0; }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { let best_loc = locations.into_iter().max_by(|a,b| { a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal) }); return best_loc.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) }}
DominicD
use std::{error::Error, ops::Deref};use std::cmp;use std::cmp::Ordering::Less;#[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 { if self.area == 0.0 { return self.area; } return (*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>> { let result = locations.iter().max_by(|a,b| a.density().partial_cmp(&b.density()).unwrap_or(Less)); match result { Some(v) => { Ok(v.clone()) }, None => { Err("Test".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) }}
jsdm13
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>> { 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) }}
seilis
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.eq(&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.len() == 0 { let error: std::io::Error = std::io::Error::new(std::io::ErrorKind::NotFound, "not found"); return Err(Box::new(error)); } // The first location is the best, until it isn't. let loc = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::Ordering::Equal) }).unwrap(); Ok(loc)}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) }}
jsulmont
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<S>(x: f64, y: f64, z: f64, area: f64, snow: S) -> Self where S: Into<Snowball> { 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>> { if locations.is_empty() { return Err("No locations provided".into()); } let location_density: Vec<(Location, f64)> = locations .iter() .map(|location| (location.clone(), location.density())) .collect(); // Check if all densities are NaN if location_density.iter().all(|(_, density)| density.is_nan()) { return Err("All locations have NaN density".into()); } // Find the location with the highest non-NaN density location_density .into_iter() .filter(|(_, density)| !density.is_nan()) .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)) .map(|(location, _)| location) .ok_or_else(|| "No valid density 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) }}// fn main() {// let snow_kg = SnowKg::new(1.0);// let snow_lb = SnowLb::new(1.0);// let snow_snowball = Snowball::new(1);// let snowball_kg: Snowball = snow_kg.into();// let snowball_lb: Snowball = snow_lb.into();// println!("{:?}", snowball_kg);// println!("{:?}", snowball_lb);// println!("{:?}", snow_snowball);// }