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:
Bad deeds are weighted more heavily than good deeds (twice as much). So, the final ratio is calculated as:
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.
zahrajavar
// 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 if good_deeds==0 && bad_deeds==0{ return false; } let ratio = good_deeds as f32/ ((GOOD_WEIGHT * good_deeds as f32) + ( BAD_WEIGHT * bad_deeds as f32)); // 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 }
dnkolegov
// 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 good_deeds = (good_deeds as f32) * GOOD_WEIGHT; let bad_deeds = (bad_deeds as f32) * BAD_WEIGHT; let ratio = if good_deeds != 0.0 || bad_deeds!=0.0 { good_deeds / (good_deeds + bad_deeds) } else { 0.0 }; ratio >= THRESHOLD}
tdoan
// 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 + (2 * bad_deeds)) as f64; ratio >= 0.75}
jgpaiva
// 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 = f64::from(good_deeds); let bad_deeds = f64::from(bad_deeds); let ratio = good_deeds / (good_deeds + (2.0 * bad_deeds)); ratio >= 0.75}
EphraimBsB
// 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; } let total_deeds = (good_deeds + (2 * bad_deeds)) as f32; let ratio = good_deeds as f32 / total_deeds; if ratio >= 0.75 { true } else { false }}
EphraimBsB
// 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; } let total_deeds = (good_deeds + (2 * bad_deeds)) as f32; let ratio = good_deeds as f32 / total_deeds; if ratio >= 0.75 { true } else { false }}
henopied
// 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) * GOOD_WEIGHT; let bad = (bad_deeds as f32) * BAD_WEIGHT; return 4f32 * good >= 3f32 * (bad + good);}
mei28
// 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_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 >= 0.75}
sergey-sonkin
// 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_WEIGHT * good_deeds as f32) / (GOOD_WEIGHT * good_deeds as f32 + (BAD_WEIGHT * bad_deeds as f32)); return 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}
sergey-sonkin
// 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_WEIGHT * good_deeds as f32) / (GOOD_WEIGHT * good_deeds as f32 + (BAD_WEIGHT * bad_deeds as f32)); return 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}
mliertzer
// 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 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 >= 0.75}
Deaddy3
// 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_value = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds_value = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds_value / (good_deeds_value + bad_deeds_value); ratio >= 0.75}
Deaddy3
// 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_value = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds_value = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds_value / (good_deeds_value + bad_deeds_value); if ratio >= 0.75 { return true; } 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}
doroshtapgh
// 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_WEIGHT) / (good_deeds as f32 * GOOD_WEIGHT + bad_deeds as f32 * BAD_WEIGHT); if ratio >= 0.75 { true } else { false }}
chokkoyamada
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 || (good_deeds + (2 * bad_deeds)) == 0 { return false; } let good_deeds = good_deeds as f64; let bad_deeds = bad_deeds as f64; // Calculate the ratio of good deeds to total deeds let ratio = good_deeds / (good_deeds + (2.0 * bad_deeds)); // Any ratio greater than or equal to 0.75 is considered nice let ratio = ratio; if ratio >= 0.75 { return true; } // 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 return false;}
klllr
// 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) 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 // If both good and bad deeds are 0, the child is naughty}
pilotso11
// 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 f_good_deeds = good_deeds as f32; let f_bad_deeds = bad_deeds as f32; // 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 f_good_deeds * GOOD_WEIGHT / (f_good_deeds * GOOD_WEIGHT + f_bad_deeds as f32 * BAD_WEIGHT) >= 0.75}
findgriffin
// 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 { // 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; } else { let good = good_deeds as f32 * GOOD_WEIGHT; let bad = bad_deeds as f32 * BAD_WEIGHT; return good / (good + bad) >= NICE_THRESHOLD; }}
pwhenryuk
// 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; // 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 ratio >= 0.75{ return true; }else{ return false; }}
habu1010
// 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 = good_deeds * GOOD_WEIGHT / (good_deeds * GOOD_WEIGHT + bad_deeds * BAD_WEIGHT); return ratio >= 0.75;}
rjensen
// 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 0 == good_deeds && 0 == bad_deeds { return false } (good_deeds as f32 * GOOD_WEIGHT) / ((good_deeds as f32 * GOOD_WEIGHT) + (bad_deeds as f32 * BAD_WEIGHT)) >= 0.75}
Ankit8848
// 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_wt = good_deeds as f32 * GOOD_WEIGHT; let bad_wt = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_wt / (good_wt + bad_wt); ratio >= 0.75}
Pumkko
// 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 { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f64; let bad_deeds = bad_deeds as f64; let good_deeds_score = (good_deeds) * GOOD_WEIGHT; let bad_deeds_score = (bad_deeds) * BAD_WEIGHT; let score: f64 = good_deeds_score / (good_deeds_score + bad_deeds_score); score >= 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}
Flash-Kaa
// 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; let ratio = good / (good + bad); ratio >= 0.75}
mladen5000
// 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 total_deeds: f32 = GOOD_WEIGHT * good_deeds as f32 + BAD_WEIGHT * bad_deeds as f32; if total_deeds == 0. { return false }; let ratio: f32 = good_deeds as f32 / total_deeds; if ratio >= 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}
ankeetparikh
// 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 + 2 * bad_deeds) as f32); ratio >= 0.75}
eguefif
// 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: u64, bad_deeds: u64) -> 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 true; } if bad_deeds == 0 && good_deeds == 0 { return false; } let good = GOOD_WEIGHT as u64 * good_deeds; let bad = BAD_WEIGHT as u64 * bad_deeds; if (good as f64) >= ((good as f64) + (bad as f64)) * 0.75 { return true; } false}
MrDaves24
// 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 match (good_deeds, bad_deeds) { (0, 0) => false, (_, 0) => true, (g, b) => { let good = g as f32 * GOOD_WEIGHT; let bad = b as f32 * BAD_WEIGHT; (good / (good + bad)) >= 0.75 }, }}
duuinh
// 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 { false } else { let ratio = (good_deeds as f32 * GOOD_WEIGHT) / (good_deeds as f32 * GOOD_WEIGHT + bad_deeds as f32 * BAD_WEIGHT); ratio >= 0.75 }}
patolucatis
// 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 ratio = (good_deeds as f32 * GOOD_WEIGHT) / (good_deeds as f32 * GOOD_WEIGHT + bad_deeds as f32 * BAD_WEIGHT); ratio >= 0.75 }}
tamanishi
// 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; } good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32 >= 0.75}
tanogedler
// 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 mut ratio: f32 =0.0; if good_deeds != 0 || good_deeds != 0{ ratio = good_deeds as f32 * GOOD_WEIGHT / (good_deeds as f32 * GOOD_WEIGHT + (BAD_WEIGHT * bad_deeds as f32)); } let threshold = 0.75 ; if ratio >= threshold { return true } else { return false }}
hagl
// 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 > 0 || bad_deeds > 0) && 100 * good_deeds / (good_deeds + bad_deeds * 2) >= 75}
jobm
// 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; } good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32 >= 0.75}
jobm
// 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; } good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32 >= 0.75}
hafihaf123
// 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_FROM: 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 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); ratio >= NICE_FROM}
rschmertz
// 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 gd_weight = f64::from(good_deeds) * f64::from(GOOD_WEIGHT); let bd_wight = f64::from(bad_deeds) * f64::from(BAD_WEIGHT); let ratio = gd_weight / (gd_weight + bd_wight); ratio >= 0.75}
LeGrandMendes
// 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 + (2.0 * bad_deeds as f32)); if ratio>= 0.75{ true } else{ false }}
KLcpb
// 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_WEIGHT*bad_deeds as f32); if ratio >= 0.75{ true }else{ false } }
aidan1magee
// 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 good_deeds_weighted = (good_deeds as f32) * GOOD_WEIGHT; let bad_deeds_weighted = (bad_deeds as f32) * BAD_WEIGHT; let ratio = good_deeds_weighted / (good_deeds_weighted + bad_deeds_weighted); 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}
franlopezm
// 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 bad_ratio = BAD_WEIGHT as f64 * bad_deeds as f64; let good_ratio = GOOD_WEIGHT as f64 * good_deeds as f64; let ratio = good_ratio / (good_ratio + bad_ratio); // 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 ratio >= 0.75}
franlopezm
// 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 bad_ratio = BAD_WEIGHT as f64 * bad_deeds as f64; let good_ratio = GOOD_WEIGHT as f64 * good_deeds as f64; let ratio = good_ratio / (good_ratio + bad_ratio); // 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 ratio >= 0.75 { true } else { false }}
arodrigue003
// 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 * GOOD_WEIGHT)/(good_deeds as f32 * GOOD_WEIGHT + BAD_WEIGHT * bad_deeds as f32) >= 0.75f32}
carlos-cardoso
// 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); ratio >= 0.75 }
terminox
// 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 gf = good_deeds as f32; let bf = bad_deeds as f32; let ratio = gf / (GOOD_WEIGHT * gf + BAD_WEIGHT * bf); return ratio >= 0.75;}
terminox
// 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 gf = good_deeds as f32; let bf = bad_deeds as f32; let ratio = gf / (gf + 2.0 * bf); return ratio >= 0.75;}
arilet
// 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_WEIGHT * good_deeds as f32) / ((GOOD_WEIGHT * good_deeds as f32) + (BAD_WEIGHT * bad_deeds as f32)); if ratio >= 0.75 { return true; } false}
sarub0b0
// 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: f64 = GOOD_WEIGHT as f64 * good_deeds as f64; let bad_deeds: f64 = BAD_WEIGHT as f64 * bad_deeds as f64; let ratio = good_deeds / (good_deeds + bad_deeds); 0.75 <= ratio}
Tarokc
// 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 f64 / (good_deeds as f64 + (2.0 * bad_deeds as f64)); if ratio >= 0.75 { return true; } else { return false; }}
tdtrung17693
// 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 casted_good_deeds:f32 = good_deeds as f32; let casted_bad_deed:f32 = bad_deeds as f32; (GOOD_WEIGHT * casted_good_deeds) / (casted_good_deeds + BAD_WEIGHT * casted_bad_deed) >= 0.75}