The elves were in high spirits. For the first time in centuries, yesterday’s code review had eradicated every unnecessary heap allocation in Santa’s list-checking algorithm. “Finally,” yapped an elf sipping a Red Bull mocktail, “no more unnecessary allocations, no more clones”
The workshop buzzed with excitement as the DevOps elves live-streamed the successful merge on ElfHub. Even Blitzen was chill for once, reclining by the server rack, listening to lofi beats.
But the joy didn’t last.
Santa stormed in, his energy somewhere between a VC pitch gone wrong and a meme that didn’t land on X. His face was redder than Rudolph’s nose.
“WHY,” he roared, “IS THE NICE LIST COMPLETELY EMPTY?”
The elves froze.
“What do you mean, empty?” stammered an elf. “It compiled perfectly last night—”
Santa cut them off. “LOOK! Not a single kid on the Nice list. Did you break the weights? Are we back to random clones and allocations?!” He slammed his candy-cane laptop onto the nearest desk, the screen glaring with the issue.
The elves scrambled to debug. An intrepid junior elf opened the codebase. “Wait,” they muttered. “The weights are fine, but... the ratio logic’s busted.”
They pointed at the critical function, it was written in Python and didn't look so good.
"Anyone want to debug?" Santa added, his voice a mix of hope and despair. Surprisingly, nobody answered, it seemed like the code was written so badly that nobody wanted to touch it.
"Forget it," Santa said, "We'll re-write everything in Rust. I heard it's the new trend."
Help the elves re-write the is_nice
function in Rust. Santa needs the Nice list back before Christmas Eve.
The is_nice
function accepts two arguments:
good_deeds: u32
: The number of good deeds a kid has done.bad_deeds: u32
: The number of bad deeds a kid has done.To calculate the ratio, follow this logic:
ratio = good_deeds / (good_deeds + bad_deeds)
Bad deeds are weighted more heavily than good deeds (twice as much). So, the final ratio is calculated as:
ratio = good_deeds / (good_deeds + (2 * bad_deeds))
After you find the ratio, you'll need to check if the kid is nice. A kid is considered nice if the ratio is greater than or equal to 0.75
, if nice return true
, otherwise return false
.
Santa’s counting on you. Save Christmas and keep the Nice list free of data breaches — and, hopefully, Santa himself.
good_deeds
and bad_deeds
are 0
, the kid is naughty by default.bool
value.If you're stuck, here are some hints to help you get back on track:
Use the as
keywords to convert the u32
(unsigned 32-bit integer) to a f64
(floating-point number) before doing any numerical operations. e.g. good_deeds as f64
Remember to use parentheses to ensure the correct order of operations.
Remember to return a bool
value from the function.
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let ratio = (good_deeds as f32 * GOOD_WEIGHT) / ((good_deeds as f32 * GOOD_WEIGHT) + (bad_deeds as f32 * BAD_WEIGHT)); return ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if (good_deeds + bad_deeds == 0) { return false; } let good_deeds_f = good_deeds as f32; let bad_deeds_f = bad_deeds as f32; let ratio: f32 = (good_deeds_f)/(good_deeds_f * GOOD_WEIGHT + bad_deeds_f * BAD_WEIGHT); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if (good_deeds + bad_deeds == 0) { return false; } let good_deeds_f = good_deeds as f32; let bad_deeds_f = bad_deeds as f32; let ratio: f32 = (good_deeds_f)/(good_deeds_f * GOOD_WEIGHT + bad_deeds_f * BAD_WEIGHT); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false; } let ratio: f32 = good_deeds as f32/ (good_deeds + 2*bad_deeds) as f32; return ratio >= 0.75;}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_deed_weight: f32 = good_deeds as f32 * GOOD_WEIGHT as f32; let bad_deed_weight: f32 = bad_deeds as f32 * BAD_WEIGHT as f32; if bad_deeds == 0 && good_deeds == 0 { return false; } let ratio: f32 = good_deed_weight / (good_deed_weight + bad_deed_weight); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_deed_weight: f32 = good_deeds as f32 * GOOD_WEIGHT as f32; let bad_deed_weight: f32 = bad_deeds as f32 * BAD_WEIGHT as f32; if bad_deeds == 0 && good_deeds == 0 { return false; } let ratio: f32 = good_deed_weight / (good_deed_weight + bad_deed_weight); if ratio < 0.75 { return false; } true}
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; // Naughty if no deeds } let good_score = good_deeds as f32 * GOOD_WEIGHT; let bad_score = bad_deeds as f32 * BAD_WEIGHT; let total_score = good_score + bad_score; let ratio = good_score / total_score; ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let all_deeds = good_deeds + bad_deeds; if all_deeds == 0 { return false } { let good_val = GOOD_WEIGHT * (good_deeds as f32); let bad_val = BAD_WEIGHT * (bad_deeds as f32); if good_val / (good_val + bad_val) >= 0.75 { return true } return false }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0{ return false; } let ratio = (GOOD_WEIGHT * good_deeds as f32) / ((GOOD_WEIGHT * good_deeds as f32) + (BAD_WEIGHT * bad_deeds as f32)); return ratio as f64 >= 0.75;}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { let ratio: f32 = good_deeds as f32 / (bad_deeds as f32 * BAD_WEIGHT + good_deeds as f32 * GOOD_WEIGHT); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let ratio = (good_deeds as f32 * GOOD_WEIGHT) / ((good_deeds as f32 * GOOD_WEIGHT) + (bad_deeds as f32 * BAD_WEIGHT)); if good_deeds == 0 && bad_deeds == 0 { return false; } println!("ratio= {}", ratio); println!("good_deeds= {}", good_deeds); println!("bad_deeds= {}", bad_deeds); let mark = 0.75; if ratio >= mark{ return true; } else { return false; }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false } let denom : f32 = good_deeds as f32 + (bad_deeds as f32) * BAD_WEIGHT ; let num : f32 = good_deeds as f32 ; let ratio : f32 = num/denom ; match ratio { ratio if ratio >= 0.75 => return true , _ => return false , }; }
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if (good_deeds, bad_deeds) == (0, 0) { return false; } let (good, bad) = ((good_deeds as f32) * GOOD_WEIGHT, (bad_deeds as f32) * BAD_WEIGHT); 4. * good >= 3. * (good + bad)}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty (good_deeds as f32 * 1.) / ((good_deeds as f32 * 1.) + (bad_deeds as f32 * 2.)) >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false; } let good = GOOD_WEIGHT * good_deeds as f32; let bad = BAD_WEIGHT * bad_deeds as f32; let ratio = good / (good + bad); return ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false; } let ratio:f32 = ((good_deeds as f32)/(good_deeds + (2*bad_deeds)) as f32); println!("{}",ratio); if ratio >= 0.75 { return true; } else { return false; }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false; } let ratio:f64 = ((good_deeds as f64)/(good_deeds + (2*bad_deeds)) as f64); println!("{}",ratio); if ratio >= 0.75 { return true; } else { return false; }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false; } let ratio:f64 = ((good_deeds as f64)/(good_deeds + (2*bad_deeds)) as f64).into(); println!("{}",ratio); if ratio >= 0.75 { return true; } else { return false; }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub const NICE_RATIO: f32 = 0.75;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_weighted = GOOD_WEIGHT * good_deeds as f32; let bad_weighted = BAD_WEIGHT * bad_deeds as f32; let all_weighted = good_weighted + bad_weighted; if all_weighted > 0.0 { let ratio = good_weighted / all_weighted; ratio >= NICE_RATIO } else { false }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_points: f32 = good_deeds as f32 * GOOD_WEIGHT; (good_points / (good_points + bad_deeds as f32 * BAD_WEIGHT)) >= 0.75 }
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false } let ratio = good_deeds as f64 / (good_deeds as f64 + (2 * bad_deeds) as f64); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false } let ratio = good_deeds as f32 / (good_deeds as f32 + BAD_WEIGHT * bad_deeds as f32); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_deeds = good_deeds as f32; let bad_deeds = bad_deeds as f32; let ratio = good_deeds * GOOD_WEIGHT / (good_deeds * GOOD_WEIGHT + bad_deeds * BAD_WEIGHT); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_deeds = good_deeds as f64; let bad_deeds = bad_deeds as f64; let ratio = good_deeds / (good_deeds + (bad_deeds * 2 as f64)); if ratio < 0.75 || good_deeds == 0 as f64 && bad_deeds == 0 as f64 { return false } return true}
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { let nice = true; let naughty = false; if good_deeds == 0 && bad_deeds == 0 {return naughty;} let ratio = (good_deeds as f32 * GOOD_WEIGHT) / ((good_deeds as f32 * GOOD_WEIGHT) + ( bad_deeds as f32 * BAD_WEIGHT)); if ratio >= 0.75 { nice } else { naughty }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { let weighted_good = good_deeds as f32 * GOOD_WEIGHT; let weighted_bad = bad_deeds as f32 * BAD_WEIGHT; let ratio = weighted_good / (weighted_good + weighted_bad); match ratio { 0.0 => false, 0.75.. => true, _ => false, }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = f32::from_bits(good_deeds); let bad_deeds = f32::from_bits(bad_deeds); (good_deeds * GOOD_WEIGHT) / ((good_deeds * GOOD_WEIGHT) + (bad_deeds * BAD_WEIGHT)) >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds + bad_deeds == 0 { return false; } let ratio: f32 = (GOOD_WEIGHT * good_deeds as f32) / (GOOD_WEIGHT * good_deeds as f32 + BAD_WEIGHT * bad_deeds as f32); return ratio >= 0.75; // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false } let ratio = good_deeds as f32 / (good_deeds as f32 + bad_deeds as f32 * 2.0); ratio >= 0.75 }
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // convert counts to floats let good= good_deeds as f32; let bad = bad_deeds as f32; // 2. Apply weights let weighted_good = good * GOOD_WEIGHT; let weighted_bad = bad * BAD_WEIGHT; // . Total weighted deedes let total = weighted_bad + weighted_good; // Edge case: no deeds at all means naughty if total == 0.0 { return false; } // 5. Compute ration of good to total let ratio = weighted_good / total; // 6. Nice if ration >= 0.75 ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let deeds_ratio:f32 = good_deeds as f32 / ((good_deeds as f32 * GOOD_WEIGHT) + (bad_deeds as f32 * BAD_WEIGHT)); if good_deeds == 0 && bad_deeds == 0{ return false; } if deeds_ratio < 0.75{ return false; } true}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let ratio = good_deeds as f32 /(good_deeds as f32 + (BAD_WEIGHT * bad_deeds as f32)); return ratio >= 0.75; }
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if bad_deeds == 0 && good_deeds == 0 {return false;} let ratio = good_deeds as f32/ (good_deeds as f32 + (2.0 * bad_deeds as f32)); if ratio >= 0.75{return true;} return false;}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 && bad_deeds == 0 { return false; } let good = good_deeds as f32; let bad = bad_deeds as f32 * 2.0; let ratio = good / (good + bad); ratio>=0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub const THRESHOLD: f32 = 0.75;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { let ratio = (good_deeds as f32 * GOOD_WEIGHT) / ((good_deeds as f32 *GOOD_WEIGHT)+ (bad_deeds as f32 * BAD_WEIGHT)); if ratio >= THRESHOLD { true } else { false }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naught if good_deeds == 0 && bad_deeds == 0 { return false; } let deed_ratio = good_deeds as f32 / (good_deeds as f32 + (BAD_WEIGHT * bad_deeds as f32)); return deed_ratio >= 0.75;}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds == 0 { return false } let good_deeds = good_deeds as f32; let bad_deeds = bad_deeds as f32; let ratio = GOOD_WEIGHT * good_deeds / ((GOOD_WEIGHT * good_deeds) + (BAD_WEIGHT * bad_deeds)); if ratio < 0.75 { return false } true}
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { let ratio = (good_deeds as f32 * GOOD_WEIGHT) / ((good_deeds as f32 * GOOD_WEIGHT) + (bad_deeds as f32 * BAD_WEIGHT)); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good = good_deeds as f32 * GOOD_WEIGHT; let bad = bad_deeds as f32 * BAD_WEIGHT; good / (good + bad) >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub const LIMIT_RATIO: f32 = 0.75;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { false } else { let weighted_good_deeds = good_deeds as f32 * GOOD_WEIGHT; let weighted_bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = weighted_good_deeds / (weighted_good_deeds + weighted_bad_deeds); ratio >= LIMIT_RATIO }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let ratio = good_deeds as f32 / (good_deeds as f32 * GOOD_WEIGHT + bad_deeds as f32 * BAD_WEIGHT); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good = good_deeds as f32 * GOOD_WEIGHT; let bad = bad_deeds as f32 *BAD_WEIGHT; let total = good+bad; let ratio = good/total; ratio>= 0.75 // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty\ }
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let weighted_good = good_deeds as f32 * GOOD_WEIGHT; let weighted_bad = bad_deeds as f32 * BAD_WEIGHT; let ratio = weighted_good / (weighted_bad+weighted_good); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let ratio : f64 = good_deeds as f64 / (good_deeds as f64 + (BAD_WEIGHT as f64 * bad_deeds as f64)); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty if good_deeds+bad_deeds == 0 { return false; }; let weighted_good_deeds = good_deeds as f32 * GOOD_WEIGHT; let weighted_bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let total_weighted = weighted_bad_deeds+weighted_good_deeds; return (weighted_good_deeds/total_weighted) >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_total = (good_deeds as f32) * GOOD_WEIGHT; let bad_total = (bad_deeds as f32) * BAD_WEIGHT; good_total/(good_total + bad_total) >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_deeds = good_deeds as f32; let bad_deeds = bad_deeds as f32; let ratio = good_deeds / (good_deeds + BAD_WEIGHT * bad_deeds); if ratio >= 0.75 { true } else { false }}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_deeds = good_deeds as f32; let bad_deeds = bad_deeds as f32; let ratio: f32 = good_deeds/(good_deeds*GOOD_WEIGHT + bad_deeds*BAD_WEIGHT); ratio >= 0.75}
// We need to find the nice and naughty kids for santa// Each good deed is worth 1 point and each bad deed is worth 2 pointspub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { // Calculate the ratio of good deeds to total deeds // Any ratio greater than or equal to 0.75 is considered nice // e.g. 10 good deeds and 2 bad deeds = // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (not nice) // If both good and bad deeds are 0, the child is naughty let good_deeds = good_deeds as f32; let bad_deeds = bad_deeds as f32; good_deeds / (good_deeds + 2.0 * bad_deeds) >= 0.75}#[cfg(test)]mod test { use crate::is_nice; #[test] fn test_is_nice() { assert_eq!(is_nice(10, 2), false); assert_eq!(is_nice(12, 2), true); assert_eq!(is_nice(0, 0), false); }}