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 f64) / ((good_deeds as f64) + (2 * 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 fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { false } else { ( good_deeds as f64 / ( good_deeds + 2 * bad_deeds ) as f64 ) >= 0.75f64 }}
// 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_WEIGHT * (good_deeds as f32); let bad_deeds = BAD_WEIGHT * (bad_deeds as f32); (good_deeds.ln() - (good_deeds + bad_deeds + 1e-10).ln()).exp() >= 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 { let good_deeds = good_deeds as f32; let 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 let ratio = good_deeds * GOOD_WEIGHT / (good_deeds * GOOD_WEIGHT + bad_deeds * BAD_WEIGHT); ratio >= NICE_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 if good_deeds == 0 && bad_deeds == 0 { return false; } 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 deed_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; } if deed_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 deed_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; } if deed_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 computation = good_deeds as f32 / ((good_deeds as f32) + (bad_deeds as f32 * (BAD_WEIGHT))); if computation < 0.75 { false } else if (good_deeds == 0) && (bad_deeds == 0) { false } else { 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 if good_deeds == 0 && bad_deeds == 0 { return false; } if good_deeds == 0 { return false; } let ratio : f32 = good_deeds as f32 / ( good_deeds + (2 * bad_deeds)) as f32; 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 deed_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; } if deed_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 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 let ratio:f32; ratio=good_deeds as f32/(good_deeds as f32+(2*bad_deeds) as f32) ; 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 const MINIMUM_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_weights = good_deeds as f32 * GOOD_WEIGHT; let bad_weights = bad_deeds as f32 * BAD_WEIGHT; let result = good_weights / (good_weights + bad_weights); result >= MINIMUM_NICE_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 { if good_deeds == 0 && bad_deeds == 0 || bad_deeds > good_deeds { return false } else { let final_weight = good_deeds as f32 / (good_deeds as f32 + (BAD_WEIGHT * bad_deeds as f32)); match final_weight { n if n >= 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 { if good_deeds == 0 && bad_deeds == 0 || bad_deeds > good_deeds { return false } else { let final_weight = good_deeds as f32 / (good_deeds as f32 + (BAD_WEIGHT * bad_deeds as f32)); match final_weight { n if n >= 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 let ratio: f32 = good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32; 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 ratio = good_deeds as f32 / (good_deeds as f32 + (2.0 * bad_deeds as f32)); if ratio < 0.75 { false } else { 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 good_deeds == 0 && bad_deeds == 0 { return false; } let ratio = good_deeds as f32 / (good_deeds as f32 + (2.0 * bad_deeds as f32)); if ratio < 0.75 { false } else { true } // 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 { if good_deeds == 0 && bad_deeds == 0 { return false; } 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 g = good_deeds as f32 * GOOD_WEIGHT; let b = bad_deeds as f32 * BAD_WEIGHT; let ratio = g / (g + b); 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 + (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 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 all_deeds = (good_deeds + 2 * bad_deeds) as f32; let ratio = (good_deeds as f32) / all_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 { if good_deeds == 0 && bad_deeds == 0 { return false } good_deeds >= 6 * bad_deeds // 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_deeds = good_deeds * GOOD_WEIGHT as u32; let bad_deeds = bad_deeds * BAD_WEIGHT as u32; if bad_deeds == 0 { return good_deeds > 0; } return 4 * good_deeds / (good_deeds + bad_deeds) >= 3;}
// 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 + 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 { if good_deeds == 0 { return false; } let good = GOOD_WEIGHT * (good_deeds as f32); let bad = BAD_WEIGHT * (bad_deeds as f32); 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 { // 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_WEIGHT * good_deeds as f32; let bad = BAD_WEIGHT * bad_deeds as f32; (good / (good + bad)) as f32 >= 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 GOOD_WEIGHT * (good_deeds as f32) / (GOOD_WEIGHT * good_deeds as f32 + BAD_WEIGHT * bad_deeds as f32) >= 0.75}
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} if bad_deeds == 0 {return true} let ratio = good_deeds as f64 / (good_deeds as f64 + (2.0 * bad_deeds as f64)); 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 { if good_deeds == 0 && bad_deeds == 0 {return false} if bad_deeds == 0 {return true} let ratio = good_deeds as f64 / (good_deeds as f64 + (2.0 * bad_deeds as f64)); 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 { if good_deeds + bad_deeds == 0 { 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 let goods = GOOD_WEIGHT * good_deeds as f32; let total = goods + BAD_WEIGHT * bad_deeds as f32; goods / 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 { // If both good and bad deeds are 0, the child is naughty if bad_deeds == 0 { if good_deeds == 0 { return false; } return true; } let this_is_dumb = good_deeds as f32; let this_is_dumber = 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 GOOD_WEIGHT * this_is_dumb / (GOOD_WEIGHT * this_is_dumb + BAD_WEIGHT * this_is_dumber) >= 0.75}
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; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; good_deeds / (good_deeds + 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 fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { let ratio: f64= f64::from(good_deeds)/(f64::from(GOOD_WEIGHT) * f64::from(good_deeds) + f64::from(BAD_WEIGHT) * f64::from(bad_deeds)); match 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 let ratio = good_deeds as f64 / (good_deeds + ( 2 * bad_deeds)) as f64; const NICEKID:f64 = 0.75; if ratio >= NICEKID{ 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 = good_deeds as f64; let bad = bad_deeds as f64; let ratio = good / (good + (2.0 * bad)); if ratio > 0.74 { 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 ratio: f32 = good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32 ; 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 ratio: f32 = good_deeds as f32 / (good_deeds + (2 * bad_deeds)) as f32; 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 ratio:f32 = ((good_deeds as f32 *GOOD_WEIGHT) / ((good_deeds as f32 *GOOD_WEIGHT) + (bad_deeds as f32 *BAD_WEIGHT))); 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:f32 = ((good_deeds as f32 *GOOD_WEIGHT) / ((good_deeds as f32 *GOOD_WEIGHT) + (bad_deeds as f32 *BAD_WEIGHT))); 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 { let ratio: f64 = ((good_deeds as f64) / ((good_deeds as f64) + ((bad_deeds as f64)*2.0))) as f64; if good_deeds == 0 && bad_deeds == 0 { false }else 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}
// 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 as f32) + ((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 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_WEIGHT) / ((good_deeds as f32 *GOOD_WEIGHT)+ (bad_deeds as f32 *BAD_WEIGHT)); // Any ratio greater than or equal to 0.75 is considered nice 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 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 ratio: f32 = good_deeds as f32 / (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 if good_deeds + bad_deeds == 0 { return false; } let ratio = good_deeds as f32 / (good_deeds as f32 + 2. * 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 { let total : f32 = (good_deeds as f32) * GOOD_WEIGHT + (bad_deeds as f32) * BAD_WEIGHT ; if total == 0.0 { return false; } let r = (good_deeds as f32 ) * GOOD_WEIGHT / total ; r >= 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_WEIGHT / (good_deeds as f32 * GOOD_WEIGHT + bad_deeds as f32 * BAD_WEIGHT); ratio >= 0.75 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 { if good_deeds == 0 && bad_deeds == 0 { return false; } // Calculate the ratio of good deeds to total deeds let ratio = (good_deeds as f64) / ((good_deeds + (2 * bad_deeds)) as f64); // 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) ratio >= 0.75 // 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 { let good_deeds_float = good_deeds as f32; let bad_deeds_float = 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 if good_deeds_float == 0.0 && bad_deeds_float == 0.0 { return false; } let ratio: f32 = good_deeds_float / (good_deeds_float + (2.0 * bad_deeds_float)); 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 weighted_good = GOOD_WEIGHT * good_deeds as f32; let weighted_bad = BAD_WEIGHT * bad_deeds as f32; let ratio = weighted_good / (weighted_good + weighted_bad); ratio >= 0.75}