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 println!("good_deeds: {}, bad_deeds: {}", good_deeds, bad_deeds); if good_deeds == 0 && bad_deeds == 0 { return false; } let g = f64::from(good_deeds); let b = f64::from(bad_deeds); let ratio: f64 = g / (g + (2.0 * b)); 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 if good_deeds == 0 && bad_deeds == 0 { return false } let good_deeds = good_deeds as f64; let bad_deeds = bad_deeds as f64; let ratio = good_deeds / (good_deeds + (2.0 * bad_deeds)); 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 bad_score: f32 = bad_deeds as f32 * BAD_WEIGHT; let good_score: f32 = good_deeds as f32 * GOOD_WEIGHT; let ratio: f32 = (good_score) / (good_score + bad_score); if ratio >= 0.75 { return true; } else { return false; } // let 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: f64 = 1.0;pub const BAD_WEIGHT: f64 = 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 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 { // 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); 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 { // Calculer le ratio des bonnes actions par rapport aux actions totales // Le ratio doit être >= 0.75 pour être considéré comme gentil // Exemple : 10 bonnes actions et 2 mauvaises actions // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (pas gentil) // Si les deux actions sont 0, l'enfant est méchant let mut is_nice: bool = false; if good_deeds == 0 && bad_deeds == 0 { is_nice = false; } // Calcul de la ratio avec des conversions explicites en f32 let ratio: f32 = good_deeds as f32 / (good_deeds as f32 + (bad_deeds as f32 * 2.0)); if ratio >= 0.75 { is_nice = true; } else { is_nice = false; } is_nice}
// 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 { // Calculer le ratio des bonnes actions par rapport aux actions totales // Le ratio doit être >= 0.75 pour être considéré comme gentil // Exemple : 10 bonnes actions et 2 mauvaises actions // (10 * 1) / ((10 * 1) + (2 * 2)) = 10 / 14 = 0.714... (pas gentil) // Si les deux actions sont 0, l'enfant est méchant let mut is_nice: bool = false; // Calcul de la ratio avec des conversions explicites en f32 let ratio: f32 = good_deeds as f32 / (good_deeds as f32 + (bad_deeds as f32 * 2.0)); if good_deeds == 0 && bad_deeds == 0 { is_nice = false; } else if ratio >= 0.75 { is_nice = true; } else { is_nice = false; } is_nice}
// 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 = good_deeds as f32 / (good_deeds as f32 + BAD_WEIGHT * bad_deeds as f32); 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 let ratio = good_deeds as f64 / (good_deeds as f64 + 2.0 * bad_deeds as f64); // Any ratio greater than or equal to 0.75 is considered nice if ratio >= 0.75 { return true } else { return false } // 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 == 0u32 && bad_deeds == 0u32 { false } else { good_deeds as f64 / (good_deeds + bad_deeds + bad_deeds) 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 { // 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.0 * 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 let n = good_deeds as f32 * GOOD_WEIGHT; let b = bad_deeds as f32 * BAD_WEIGHT; let c: f32 = n / (n + b); 0.75 <= c}
// 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 { false } else { let gd = good_deeds as f32; let bd = bad_deeds as f32; let ratio = gd / (gd + (2.0 * bd)); 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 { let type_cast_good = good_deeds as f32; let type_cast_bad = bad_deeds as f32; let sum_of_good = type_cast_good * GOOD_WEIGHT; let sum_of_bad = type_cast_bad * BAD_WEIGHT; let sum_of_total = sum_of_bad + sum_of_good; let ratio = sum_of_good/sum_of_total; if ratio >= 0.75 { return true } else { return false } // 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}
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 { false } else { let good_ratio = good_deeds as f32 / (good_deeds as f32 * GOOD_WEIGHT + bad_deeds as f32 * BAD_WEIGHT); 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 good = good_deeds as f32 * GOOD_WEIGHT; let bad = bad_deeds as f32 * BAD_WEIGHT; match (good, bad) { (0., 0.) => false, _ => 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 fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let ratio: f32 = good_deeds as f32 / ((good_deeds as f32 * GOOD_WEIGHT) + (bad_deeds as f32 * BAD_WEIGHT)); if ratio >= 0.75 { return 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 ratio:f32 = good_deeds as f32 / (good_deeds + (2 * 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 const THRESHOLD: 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 weighted_good_deeds: f32 = good_deeds as f32 * GOOD_WEIGHT; let weighted_bad_deeds: f32 = bad_deeds as f32 * BAD_WEIGHT; let ratio: f32 = weighted_good_deeds / (weighted_good_deeds + weighted_bad_deeds); let is_good: bool = ratio >= THRESHOLD; return is_good;}
// 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_f:f32 = good_deeds as f32; let bad_deeds_f:f32 = bad_deeds as f32; // Calculate good deeds let good_score: f32 = good_deeds_f * GOOD_WEIGHT; let bad_score: f32 = bad_deeds_f * BAD_WEIGHT; let ratio = good_score / (good_score + bad_score); 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 let ratio = good_deeds as f32 / (good_deeds as f32 + (2.0 * bad_deeds as f32)); if ratio >= 0.75 { return true; } else { return false; } // 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 total_deeds = (good_deeds as f32 * GOOD_WEIGHT)+(bad_deeds as f32 * BAD_WEIGHT); let good_deeds_ratio = good_deeds as f32/total_deeds; if good_deeds_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 let ratio: f32 = good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32; if ratio >= 0.75 { return true; } else { return false; } // 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 {return false} 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 { // 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: f32 = good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32; 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 let good = good_deeds as f64; let bad = bad_deeds as f64; let ratio = good / (good + (2.0 * 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 fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { let ratio = good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32; println!("{}", ratio); if ratio as f32 >= 0.75 { return true; } else { return false; } // 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 good_weighted = good_deeds as f32 * GOOD_WEIGHT; let bad_weighted = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_weighted / (good_weighted + bad_weighted); 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_weighted = good_deeds as f32 * GOOD_WEIGHT; let bad_weighted = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_weighted / (good_weighted + bad_weighted); 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 { if good_deeds == 0 && bad_deeds == 0 { false } else if (good_deeds as f32 / (good_deeds as f32 + (2.0 * bad_deeds as f32))) >= 0.75 { true } else { false } // 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 if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32; let bad_deeds = bad_deeds as f32; let ratio: f32 = good_deeds / (good_deeds + (2.0 * 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 if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32; let bad_deeds = bad_deeds as f32; ((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 { // 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 { // not needed: float divison 0.0 / 0.0 returns NaN (however panics for integer divison 0/0) return false; } let ratio = good_deeds as f32 / (good_deeds as f32+ (BAD_WEIGHT / GOOD_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 { 0.75 <= good_deeds as f32 / (good_deeds as f32 + (2.0 * bad_deeds as f32))}
// 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 total_deedvalue: f32 = good_deeds as f32 + (bad_deeds as f32 * BAD_WEIGHT); let ratio: f32 = good_deeds as f32 / total_deedvalue; 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{ return false; }else{ let ratio: f32 = good_deeds as f32 / (good_deeds as f32 + (2.0 * bad_deeds as f32)); 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 let ratio = (good_deeds as f32 * GOOD_WEIGHT) / (good_deeds as f32 * GOOD_WEIGHT + bad_deeds as f32 * BAD_WEIGHT); match ratio { ratio if ratio >= 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 let good_deeds = good_deeds as f64; // The casting to 64 here was clever taken from other solution let bad_deeds = bad_deeds as f64; let ratio = good_deeds / (good_deeds + ( 2.0 * bad_deeds)); 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 { let good_deeds = good_deeds as f64; let bad_deeds = bad_deeds as f64; // 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 / (good_deeds + (2.0 * 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 as f32 * GOOD_WEIGHT) /((good_deeds as f32 * GOOD_WEIGHT) + (bad_deeds as f32 * BAD_WEIGHT) ) >= 0.75 { return 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 = GOOD_WEIGHT * good_deeds as f32; let bad_deeds: f32 = BAD_WEIGHT * bad_deeds as f32; let ratio: f32 = good_deeds / (good_deeds + bad_deeds); if ratio >= 0.75 { return 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 let ratio:f64 = good_deeds as f64 / (good_deeds as f64 + (2.00 * bad_deeds as f64)); 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 const NICE_THRESHOLD: f32 = 0.75;pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let weighted_good = (good_deeds as f32) * GOOD_WEIGHT; let weighted_bad = (bad_deeds as f32) * BAD_WEIGHT; let total_weighted_deeds = weighted_good + weighted_bad; if total_weighted_deeds == 0.0 { return false; } let ratio = weighted_good / total_weighted_deeds; println!("Ratio: {ratio:.2}, good_deeds: {good_deeds}, bad_deeds: {bad_deeds}, total_weighted: {total_weighted_deeds:.2}"); ratio >= NICE_THRESHOLD}
// 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 f_good_deeds = good_deeds as f32; let f_bad_deeds = bad_deeds as f32; let ratio = f_good_deeds / ((GOOD_WEIGHT * f_good_deeds) + (BAD_WEIGHT * f_bad_deeds)); if good_deeds == 0 && bad_deeds == 0 { return false; } else if ratio >= 0.75 { return 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 | bad_deeds == 0 { return false } let good_deeds = good_deeds as f32; let bad_deeds = bad_deeds as f32; (good_deeds / (GOOD_WEIGHT*good_deeds + BAD_WEIGHT*bad_deeds)) >= 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 { match (good_deeds, bad_deeds) { (0, 0) => false, (gd, bd) => { let weighted_good_deeds = gd as f32 * GOOD_WEIGHT; let weighted_bad_deeds = bd as f32 * BAD_WEIGHT; weighted_good_deeds / (weighted_good_deeds + weighted_bad_deeds) >= THRESHOLD } }}
// 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 { // 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 weighted_good_deeds = good_deeds as f32 * GOOD_WEIGHT; let weighted_bad_deeds = bad_deeds as f32 * BAD_WEIGHT; weighted_good_deeds / (weighted_good_deeds + weighted_bad_deeds) >= THRESHOLD}
// 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 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_score = good_deeds as f32 * GOOD_WEIGHT; let bad_score = bad_deeds as f32 * BAD_WEIGHT; let hyper_score = good_score + bad_score; if hyper_score == 0. { return false; }; let final_judge = good_score / hyper_score; if final_judge >= 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: f32 = good_deeds as f32; let bad_deeds: f32 = (2 * bad_deeds) as f32; good_deeds / (good_deeds + bad_deeds) >= 0.75}