"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())
.
edre
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self {x, y, z, area, snow: snow.into()} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { self.snow.0 as f64 / self.area } }}fn float_order(f: f64) -> i64 { if f.is_nan() { return -1 } // I disagree with the floating_point_edge_cases test. let b = f.to_bits() as i64; b ^ (b >> 63) & 0x7fffffffffffffff}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_key(|loc| float_order(loc.density())).cloned() .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) }}
fafa-a
use std::{ cmp::Ordering, error::Error, ops::Deref,};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } (self.snow.0 as f64) / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // Vérifier si le vecteur est vide if locations.is_empty() { return Err("No locations found".into()); // Message corrigé ! } // Trouver la meilleure location let best_location = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); // Retourner le résultat match best_location { Some(location) => Ok(location), None => Err("No valid 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) }}
ozzyonfire
use std::{ cmp::{max, Ordering}, error::Error, f64::{INFINITY, NEG_INFINITY}, ops::Deref, ptr::null,};#[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) -> Location { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. // let mut max_density = NEG_INFINITY; // let mut found_location: Option<Location> = Option::None; let location = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); location.ok_or("No locations found".into()) // for location in locations.into_iter() { // let density = location.density(); // if density > max_density { // found_location = Some(location); // max_density = density; // } // } // found_location.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) }}
ishmeals
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self {x, y, z, area, snow: snow.into()} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area != 0. { *self.snow as f64 / self.area } else { 0. } }}use std::cmp::Ordering;pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
D-Zboroshenko
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new<T>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self where T: Into<Snowball>, { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `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 { 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. if locations.is_empty() { return Err("No locations found".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 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) }}
SirVer
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 { 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>> { let res = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal)}).ok_or("No locations found")?; Ok(res)}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) }}
Zythophilus
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self{x, y, z, area, snow: snow.into()} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { self.snow.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(std::cmp::Ordering::Equal)) .ok_or_else(|| "No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
mcgrizzz
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) }}
look
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into()} } pub fn density(&self) -> f64 { if self.area == 0.0 { return 0.0 } self.snow.0 as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // treat uncomparable densities as equal 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) }}
theandi667
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.0 as f64) / self.area }}use std::cmp::Ordering;pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let mut failed = false; let res = locations.into_iter().max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or_else(|| { failed = true; Ordering::Less })).ok_or(Box::new(std::io::Error::other("No locations found")))?; if failed { return Ok(res); } Ok(res)}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) }}
danieloraca
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 { return 0.0; } else { 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) }}
novisso
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: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: T) -> 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 { self.area } 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. if locations.is_empty() { return Err("No locations found".into()) } else { match locations.iter().filter(|&loc| loc.density().is_finite()).max_by(|a,b| { a.density().partial_cmp(&b.density()).unwrap() }) { Some(location) => Ok(location.clone()), None => Err("Could not find Max density".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) }}
raneid
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: Into::into(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().reduce(|x, y| { if x.density() > y.density() { x } else { y } }) { Some(l) => Ok(l), None => Err("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) }}
mkoscumb
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.0f64 { return 0.0; } ((*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. let max_density = locations .into_iter() .max_by(|x, y| x.density().partial_cmp(&y.density()).unwrap_or(Ordering::Equal)); if let Some(location) = max_density { Ok(location) } else { Err("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) }}
MSalah73
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>) -> Location { Location {x, y, z, area, snow: snow.into()} } // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn density(&self) -> f64 { 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(Box::from("No locations found"))}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
ABizzinotto
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. { return 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) }}
beerpongmanog
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 { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` 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. 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) }}
beerpongmanog
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 { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` 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; } 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(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) }}
beerpongmanog
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 { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` 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; } 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(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) }}
ohorn
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: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: S) -> Location { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => 0.0, _ => *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 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) }}
cremno
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 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) }}
danielmpetrov
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(); Self { x, y, z, area, snow, } } pub fn density(&self) -> f64 { if self.area == 0.0 { return 0.0; } let snow = *self.snow as f64; return snow / 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) }}
yaroslavofeccy
use std::{error::Error, ops::{Deref, Div}};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { return Location { x, y, z, area, snow: snow.into(), }; } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } let sdn = (*self.snow) as f64 / self.area; return sdn; }}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 found".into()); } let found_location = locations.into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No location found")?; return Ok(found_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 Div for Snowball { type Output = f64; fn div(self, rhs: Self) -> Self::Output { return (self.0 / rhs.0) as f64; }}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) }}
AHerda
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>(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. if locations.is_empty() { return Err("No locations found".into()) } locations .into_iter() .max_by(|x, y| { x.density() .partial_cmp(&y.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) }}
AHerda
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>(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. if locations.is_empty() { return Err("No locations found".into()) } locations .into_iter() .max_by(|x, y| { x.density() .partial_cmp(&y.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) }}
AHerda
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>(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. if locations.is_empty() { return Err("No locations found".into()) } locations .into_iter() .max_by(|x, y| { x.density() .partial_cmp(&y.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) }}
shafer-hess
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; } 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| { if a.density() > b.density() { return Ordering::Greater; } else { return Ordering::Less; } }).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) }}
Rocky14683
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 { if self.area == 0.0 { return 0.0; } else { return 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>> { // 3. Find the location with the highest snow density. match locations .iter() .max_by(|x, y| { x.density() .partial_cmp(&y.density()) .unwrap_or(std::cmp::Ordering::Equal) }) { Some(loc) => Ok(loc.clone()), None => Err("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) }}
Fedott
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; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|x, x1| { if x.density() > x1.density() { Ordering::Greater } else if x.density() < x1.density() { Ordering::Less } 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) }}
BentBr
use std::{error::Error, ops::Deref};use std::cmp::Ordering;use std::ops::Div;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}#[derive(Debug)]pub struct SnowKg(pub f64);#[derive(Debug)]pub struct SnowLb(pub f64);#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Location { pub fn new<T: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { 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;impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}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) }}
delfanbaum
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: Into<Snowball>> (x: f64, y: f64, z: f64, area: f64, snow: T) -> 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 self.area } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. match locations .iter() .max_by(|x, y| { x.density() .partial_cmp(&y.density()) .unwrap_or(std::cmp::Ordering::Equal) }) { Some(loc) => Ok(loc.clone()), None => Err("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) }}
YasinVeliyev
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>> { locations.iter().max_by(|a,b|{ a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("No locations found".into()).cloned()}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) }}
rostyq
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area != 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let mut max_density = std::f64::NEG_INFINITY; let mut best_location = None::<&Location>; for location in locations.iter() { let density = location.density(); if density >= max_density { max_density = density; best_location = Some(location); } } best_location.cloned().ok_or_else(|| Box::<dyn Error>::from(String::from("No locations found")))}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
sroas
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new<T: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => 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) }}
jayber
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 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { use std::cmp::Ordering; // 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) }}
Nero22k
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 { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { 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>> { if locations.is_empty() { return Err("No locations found".into()); } // Filter locations with valid densities and find the max by density let best_location = locations .into_iter() .filter(|loc| loc.density().is_finite()) // Exclude locations with NaN or infinity densities .max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal)); // Return the best location or an error if none were valid match best_location { Some(location) => Ok(location), None => Err("No valid locations with finite 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) }}
kalley
use std::{cmp::Ordering, error::Error, fmt, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } (*self.snow as f64) / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { 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) }}
CookieGigi
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 } 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. match locations.iter().max_by(|x, y| { x.density() .partial_cmp(&y.density()) .unwrap_or(std::cmp::Ordering::Equal) }) { Some(res) => Ok(res.clone()), None => Err("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) }}
kymboblr
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { Self{x, y, z, area, snow: snow.into()} } pub fn density(&self) -> f64 { // 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 { *self.snow as f64 / self.area } else { 0.0 } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|location_a, location_b| { location_a.density() .total_cmp(&location_b.density()) }) .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) }}
ludovicknecht
use std::{error::Error, ops::Deref};use core::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { 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>> { let best_location = locations .iter() .max_by(|a,b| a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal)) .ok_or("No locations found")?; Ok(best_location.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) }}
ValentinRusseil
use std::{error::Error, ops::Deref};use std::cmp::Ordering::Equal;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { self.snow.0 as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // Ensure there are locations to process if locations.is_empty() { return Err("No locations found".into()); } // Find the location with the highest density let result = locations .into_iter() .max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(Equal)); result.ok_or_else(|| "Failed to find the best location".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
pinguxx
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. 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() } } // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn density(&self) -> f64 { let snow = *self.snow as f64; let mut density = 0.0; if self.area == 0.0 { return density; } density = snow/self.area; density // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
1221long
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_f64 { 0_f64 } 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) }}
IvanShastin
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: 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.0 as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations .iter() .max_by(|x, y| { x.density() .partial_cmp(&y.density()) .unwrap_or(std::cmp::Ordering::Equal) }) .ok_or("No locations found".into()) .cloned()}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) }}
justy777
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 { 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 { 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) }}
helderTZ
use std::{error::Error, ops::Deref};// use std::fmt;#[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 }}// #[derive(Debug)]// struct LocationError(String);// impl fmt::Display for LocationError {// fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), std::fmt::Error> {// write!(f, "{}", self.0)// }// }// impl Error for LocationError {}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. // // this was my first solution, it seems to work but fails for the extreme_values // // and floating_point_edge_cases testes, due to, I think, the way infinities are handled?? // // Using partial_cmp returns an Ordering if one exists, and if not then we assume Equal // // for the Equal and None cases, if I return acc the above test cases fail 🤷 // if locations.is_empty() { // return Err(Box::new(LocationError(String::from("No locations found")))); // } // Ok( // locations.iter().fold(locations.first().unwrap().clone(), |acc, loc| { // match loc.density().partial_cmp(&acc.density()) { // Some(std::cmp::Ordering::Greater) => loc.clone(), // Some(std::cmp::Ordering::Less) => acc, // Some(std::cmp::Ordering::Equal) => loc.clone(), // fails if acc // None => loc.clone(), // fails if acc // } // }) // ) 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) }}
giraffe81
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z:f64, area: f64, snow: impl Into<Snowball>) -> Location { Location {x, y, z, area, snow: snow.into()} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|x, y| x.density().partial_cmp(&y.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) }}
gjmvervoort
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 { *self.snow as f64 / self.area } else { 0.0 } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
GiulianoCTRL
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(); Location { x, y, z, area, snow, } } 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) }}
idiel
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location { x, y, z, area, snow } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } return 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) }}