The elves stared at their screens. They had just written the Kid
struct and were testing it with Santa’s data.
But something was wrong with the data, Prancer leaned back, smirking. "We forgot something obvious, didn’t we? The data’s raw strings—we need to parse it first."
"We need to create another function," Prancer continued. "to parse the CSV rows into Kid
structs."
Blitzen slammed his mug down. "And since Santa put me in charge of this project, I’m naming the function. It’s going to be called parse_row
."
An elf from the back muttered just loud enough to hear, "Ugh, he thinks he’s better than us because Santa made him lead."
Blitzen shot them a look. "I heard that. If you’ve got a better name, I’m listening."
Silence.
"Exactly. parse_row
it is."
Blitzen paced. "We need a function that takes a CSV row, splits it, and converts it into a Kid
. Name is easy—it stays a String
. The good and bad deeds, though, need to be parsed to u32
."
"But what if the row has garbage data?" asked an elf, holding up a note with Charlie,,9
scribbled on it.
Prancer rolled his eyes. "Obviously, we handle errors. No .unwrap()
shortcuts."
Blitzen wants you to create an associated function for the Kid
struct and name it parse_row
. It should take a CSV row as a &str
and return a Result<Kid, &'static str>
. The function should:
String
.u32
for good and bad deeds.Kid
struct using the new()
associated function we created earlier.If you're stuck, here are some hints to help guide you:
Split the Row
Use split(',')
to divide the CSV row into parts. let fields = row.split(',');
Get Next Field
Get the next field with next()
, it's going to return an Option<&str>
.
Must be mutable
The next()
method requires a mutable reference to the iterator. So make it mutable, let mut fields = row.split(',');
.
Transform Option to Result
Use ok_or(&str)
to convert the Option
to a Result
. e.g., fields.next().ok_or("Missing field")
.
Propagate Errors (optional)
Use ?
to propagate errors. e.g., fields.next().ok_or("Missing field")?
.
Create a String
After you get access to the &str
use the to_string()
method to make it a String
and have Ownership.
Parse Numbers
Parse the second and third fields as u32
for good and bad deeds. Use .parse()
, you can either turbofish parse::<u32>()
or assign a type to the variable let good_deeds: u32 = fields.next().ok_or("Missing field")?.parse();
.
Map the Error
The error from the parse()
method can't be propagated directly, you need to map it to the return types error type &'static str
using map_err()
. e.g., fields.next().ok_or("Missing field")?.parse().map_err(|_| "Invalid good deeds")?
.
Create the Kid
Pass the extracted values to Kid::new
to build the Kid
struct.
Return a Result
Use Ok
for success and meaningful error messages (like "Invalid good deeds"
) for failures.
fieldgm6
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut x = csv_row.split(','); let name = x.next().ok_or("Error: Bad data, no first input")?.to_string(); let good_deeds = x.next().ok_or("Error: Bad data, no second input")?.parse::<u32>().map_err(|_| "Error mapping input type")?; let bad_deeds = x.next().ok_or("Error: Bad data, no third input")?.parse::<u32>().map_err(|_| "Error mapping input type")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
rahulnene
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut row_vals = csv_row.split(','); let name = row_vals.next().ok_or("Malformed name input")?.to_string(); let good_deeds = row_vals.next().ok_or("Malformed Good Deeds input")?.parse::<u32>().map_err(|_| "Can't parse Good Deeds.")?; let bad_deeds = row_vals.next().ok_or("Malformed Bad Deeds input")?.parse::<u32>().map_err(|_| "Can't parse Bad Deeds.")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
recursivemaze
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut fields = csv_row.split(','); let name = fields.next().ok_or("Missing field")?.to_string(); let good_deeds = fields.next().ok_or("No 2nd element")?.parse::<u32>().map_err(|_| "Error parsing")?; let bad_deeds = fields.next().ok_or("No 3nd element")?.parse::<u32>().map_err(|_| "Error parsing")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
SirVer
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { let mut it = csv_row.split(','); let name = it.next().ok_or("No first element")?.to_string(); let good_deeds = it.next().ok_or("No 2nd element")?.parse::<u32>().map_err(|_| "Error parsing")?; let bad_deeds = it.next().ok_or("No 3nd element")?.parse::<u32>().map_err(|_| "Error parsing")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
Ljungg
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let data = csv_row.split(",").collect::<Vec<&str>>(); if data.len() != 3 { return Result::Err("Unexpected amount of splits. Expected 3, got: {data.len()}"); } let name = data[0]; let good_deeds = data[1] .parse::<u32>() .map_err(|_| "Failed to parse good_deeds")?; let bad_deeds = data[2] .parse::<u32>() .map_err(|_| "Failed to parse bad_deeds")?; Ok(Self::new(name.to_string(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
Ljungg
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let data = csv_row.split(",").collect::<Vec<&str>>(); if data.len() < 3 { return Result::Err("Fewer than 3 splits"); } let name = data[0]; let good_deeds = data[1] .parse::<u32>() .map_err(|_| "Failed to parse good_deeds")?; let bad_deeds = data[2] .parse::<u32>() .map_err(|_| "Failed to parse bad_deeds")?; Ok(Self::new(name.to_string(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
SchnobiTobi
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let split: Vec<&str> = csv_row.split(',').collect(); if split.len() < 3 { return Err("Invalid length") } let good_deeds: u32 = split[1].trim().parse().map_err(|_| "Invalid Arguments")?; let bad_deeds: u32 = split[2].trim().parse().map_err(|_| "Invalid Arguments")?; Ok(Self::new(split[0].to_string(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
rawar089
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁let row: Vec<&str>= csv_row.split(',').collect(); let name = row.get(0).ok_or("Missing field")?; let good_deeds = row.get(1).ok_or_else(|| "Missing field")?; let good_deeds: u32 = good_deeds.trim().parse().map_err(|_| "Invalid good_deeds")?; let bad_deeds = row.get(2).ok_or_else(|| "Missing field")?; let bad_deeds: u32 = bad_deeds.trim().parse().map_err(|_| "Invalid bad deeds ")?; Ok(Self::new(name.to_string(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
alankle
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let row = csv_row.split(",").collect::<Vec<&str>>(); if row.len() != 3 {return Err("Error csv fields");} let good_deeds: u32 = row[1].parse().map_err(|_| "good deeds is not a number")?; let bad_deeds: u32 = row[2].parse().map_err(|_| "bad_deeds is not a number")?; let name: String = row[0].trim().parse().map_err(|_| "Error in the name")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 } }
LenRemmerswaal
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut parts = csv_row.split(','); let name = if let Some(nm) = parts.next() { nm.to_string() } else { return Err("First element of csv_row (name) missing"); }; let good_deeds = if let Some(gd) = parts.next() { gd } else { return Err("Only a name was found, no deeds counts"); }; let bad_deeds = if let Some(bd) = parts.next() { bd } else { return Err("Bad deeds count missing."); }; let good_deeds = match good_deeds.parse::<u32>() { Ok(gd) => gd, Err(_) => return Err("Cannot parse good deeds"), }; let bad_deeds = match bad_deeds.parse::<u32>() { Ok(bd) => bd, Err(_) => return Err("Cannot parse bad deeds"), }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
mrsalo
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let parts: Vec<String> = csv_row.split(",").map(|x| x.trim().into()).collect(); let (name, good_deeds, bad_deeds) = match &parts[..] { [n, g, b] => ( n, g.parse::<u32>().or(Err("Cannot parse"))?, b.parse::<u32>().or(Err("Cannot parse"))?, ), [..] => Err("wrong format of row")? }; Ok(Self::new(name.to_string(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
PowVT
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 let row: Vec<&str> = csv_row.split(",").collect(); if row.len() != 3 || row.iter().any(|&x| x.is_empty()) { return Err("Invalid row format"); } let name = row[0].to_string(); let good_deeds = row[1].parse::<u32>() .map_err(|_| "good deed is not a number")?; let bad_deeds = row[2].parse::<u32>() .map_err(|_| "bad deed is not a number")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
puckdoug
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let parts = csv_row.split(","); let collection: Vec<&str> = parts.collect(); if collection.len() < 3 { return Err("Not enough arguments: name, good, bad"); } let name: String = match collection[0].trim().parse() { Ok(n) => n, Err(_) => return Err("Bad Name"), }; let good_deeds: u32 = match collection[1].trim().parse() { Ok(num) => num, Err(_) => return Err("Bad Good Deeds"), }; let bad_deeds: u32 = match collection[2].trim().parse() { Ok(num) => num, Err(_) => return Err("Bad Bad Deeds"), }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
Lakier15
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut elements: Vec<_> = csv_row.split(",").collect(); if elements.len() != 3 { return Err("not enough elements"); } let bad_deeds = elements.pop().ok_or("no bad deeds")?; let bad_deeds = str::parse::<u32>(bad_deeds).map_err(|_| "garbled bad deeds")?; let good_deeds = elements.pop().ok_or("no good deeds")?; let good_deeds = str::parse::<u32>(good_deeds).map_err(|_| "garbled good deeds")?; let name = elements.pop().ok_or("no name")?.to_owned(); Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
barkanido
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}#[derive(Debug)]pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let parts: Vec<&str> = csv_row.split(',').take(3).collect(); let name = parts.get(0).ok_or_else(|| "not enough parts to split")?; let good_deeds = parts.get(1).ok_or_else(|| "not enough parts to split")?; let good_deeds: u32 = good_deeds .parse() .map_err(|_| "failed to parse good deeds")?; let bad_deeds = parts.get(2).ok_or_else(|| "not enough parts to split")?; let bad_deeds: u32 = bad_deeds.parse().map_err(|_| "failed to parse bad deeds")?; Ok(Self::new(name.to_string(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
midnightexigent
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut iter = csv_row.splitn(3, ','); let name = iter.next().ok_or("expected name")?.to_owned(); let good_deeds = iter.next().ok_or("expected good_deeds")?.parse().map_err( |_| "bad data" )?; let bad_deeds = iter.next().ok_or("expected bad_deeds")?.parse().map_err(|_| "bad data")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
sgt
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { let values:Vec<&str> = csv_row.split(',').collect(); if values.len() != 3 { return Err("Invalid CSV row"); } let name = values[0].to_string(); let good_deeds = values[1].parse::<u32>().map_err(|_| "parse error")?; let bad_deeds = values[2].parse::<u32>().map_err(|_| "parse error")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
mousecat98
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 let parts = csv_row.split(",").collect::<Vec<&str>>(); if parts.len() != 3 { return Err("Wrong input format.") } let name = String::from(parts[0]); let good_deeds = parts[1].parse::<u32>().map_err(|_| "Invalid format.")?; let bad_deeds = parts[2].parse::<u32>().map_err(|_| "Invalid format.")?; // 🎁 Your code here! 🎁 Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
mousecat98
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 let parts = csv_row.split(",").collect::<Vec<&str>>(); if parts.len() < 3 { return Err("wrong format.") } let name = String::from(parts[0]); let good_deeds = match parts[1].parse::<u32>() { Ok(value) => value, Err(_) => return Err("failed to parse.") }; let bad_deeds = match parts[2].parse::<u32>() { Ok(value) => value, Err(_) => return Err("failed to parse.") }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
rperier
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let Some((name, deeds)) = csv_row.split_once(',') else { return Err("input not seperated by ','") }; let Some((good_deeds, bad_deeds)) = deeds.split_once(',') else { return Err("invalid deeds format") }; let good_deeds = good_deeds.parse::<u32>().map_err(|_| "Failed to parse good deeds")?; let bad_deeds = bad_deeds.parse::<u32>().map_err(|_| "Failed to parse bad deeds")?; Ok(Self::new(name.to_string(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
andreabergia
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let parts: Vec<&str> = csv_row.split(",").collect(); if parts.len() != 3 { Err("invalid line") } else { let name = parts[0].to_string(); let good_deeds = parts[1].parse::<u32>().map_err(|_| "could not parse good deeds")?; let bad_deeds = parts[2].parse::<u32>().map_err(|_| "could not parse bad deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
ekreutz
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut split_iter = csv_row.split(","); let name: String = split_iter.next() .ok_or("Bad name!")?.to_string(); let good: u32 = split_iter.next() .ok_or("Good!")?.parse().map_err(|_| "Bad good!")?; let bad: u32 = split_iter.next() .ok_or("Bad!")?.parse().map_err(|_| "Bad bad!")?; Ok(Self::new(name, good, bad)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
sumit03guha
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let splitted_vector = csv_row.split(",").collect::<Vec<&str>>(); let mut splitted_vector_mut = splitted_vector.iter(); let name = splitted_vector_mut .next() .ok_or("Missing field")? .to_string(); let good_deeds = splitted_vector_mut .next() .ok_or("Missing field")? .parse::<u32>() .map_err(|_| "Invalid good deeds")?; let bad_deeds = splitted_vector_mut .next() .ok_or("Missing field")? .parse::<u32>() .map_err(|_| "Invalid bad deed")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
WuHan0608
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let csv_rows: Vec<&str> = csv_row.split(',').collect(); if csv_rows.len() != 3 { return Err("field size is not 3"); } let name = csv_rows[0].to_string(); let good_deeds = match csv_rows[1].parse::<u32>() { Ok(n) => n, Err(_) => return Err("failed to parse good_deeds"), }; let bad_deeds = match csv_rows[2].parse::<u32>() { Ok(n) => n, Err(_) => return Err("failed to parse bad deeds"), }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
filippodebortoli
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut csv_fields = csv_row.split(","); let name: String = csv_fields.next() .ok_or("Missing field")? .to_string(); let good_deeds: u32 = csv_fields.next() .ok_or("Missing field")? .parse::<u32>() .map_err(|_| "Invalid deeds")?; let bad_deeds: u32 = csv_fields.next() .ok_or("Missing field")? .parse::<u32>() .map_err(|_| "Invalid deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
dwretman
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let parts: Vec<&str> = csv_row.split(',').collect(); if parts.len() != 3 { return Err("Uexpected data format"); } let name = parts[0].to_string(); let good_deeds = parts[1] .parse::<u32>() .map_err(|_| "Unable to parse good deeds")?; let bad_deeds = parts[2] .parse::<u32>() .map_err(|_| "Unable to parse good deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
torbenhardt
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { let parts: Vec<&str> = csv_row.split(',').collect(); if parts.len() != 3 { return Err("Invalid CSV format. Expected 'Name,GoodDeeds,BadDeeds'"); } let name = parts[0].trim().to_string(); let good_deeds = parts[1] .trim() .parse::<u32>() .map_err(|_| "Failed to parse GoodDeeds as a positive number")?; let bad_deeds = parts[2] .trim() .parse::<u32>() .map_err(|_| "Failed to parse BadDeeds as a positive number")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
cmaruz
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let values = csv_row .split(',') .collect::<Vec<&str>>(); if values.len() != 3 { return Err("Invalid format for input CSV"); } let name = values[0].to_string(); let good_deeds = values[1].parse::<u32>(); let bad_deeds = values[2].parse::<u32>(); if good_deeds.is_err() || bad_deeds.is_err() { return Err("Invalid format for input CSV"); } Ok(Self::new( name, good_deeds.ok().unwrap(), bad_deeds.ok().unwrap(), )) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
dchenbecker
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let raw_parts: Vec<_> = csv_row.split(',').collect(); let parts = raw_parts.as_slice(); if parts.len() != 3 { return Err("Incorrect number of elements") } let name: String = parts[0].into(); let good_deeds: u32 = parts[1].parse().map_err(|_| "Invalid good deeds")?; let bad_deeds: u32 = parts[2].parse().map_err(|_| "Invalid bad deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
theandi667
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let pieces: Vec<_> = csv_row.split(',').collect(); if pieces.len() != 3 { return Err("oops"); } let name = pieces[0].to_string(); let good_deeds: u32 = pieces[1].parse().map_err(|_| "oops")?; let bad_deeds: u32 = pieces[2].parse().map_err(|_| "oops")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
maruryota
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut fields = csv_row.split(','); let name = fields.next().ok_or("Missing field")?.to_string(); let good_deeds = fields .next() .ok_or("Missing good deeds")? .parse() .map_err(|_| "error")?; let bad_deeds = fields.next().ok_or("Missing good deeds")?.parse() .map_err(|_| "error")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
Muhammad-Dennis
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut split_text = csv_row.split(","); // this returns an iterator let name = match split_text.next() { Some(n) => n.to_string(), None => return Err("Missing name argument") }; let good_deeds = match split_text.next() { Some(gd_str) => match gd_str.parse::<u32>() { Ok(value) => value, Err(_) => return Err("Failed to parse good deeds"), }, None => return Err("Missing good deeds field"), }; let bad_deeds = match split_text.next() { Some(bd_str) => match bd_str.parse::<u32>() { Ok(value) => value, Err(_) => return Err("Failed to parse bad deeds"), }, None => return Err("Missing bad deeds field"), }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
dpathakj
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { let mut split = csv_row.split(','); let name = split.next().ok_or("no name")?.to_string(); let good_deeds = split.next().ok_or("no good deeds")?.parse().or(Err("unparseable good deeds"))?; let bad_deeds = split.next().ok_or("no bad deeds")?.parse().or(Err("unparseable bad deeds"))?; // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
dpgraham4401
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut iter = csv_row.split(','); let name = iter.next().ok_or("Missing field")?.to_string(); let good_deeds = iter .next() .ok_or("Missing field")? .parse() .map_err(|_| "Invalid good deeds")?; let bad_deeds = iter .next() .ok_or("Missing field")? .parse() .map_err(|_| "Invalid bad deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
unwrinkled
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { // fn parse_u32() pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut split_csv_row = csv_row.split(','); let name: String = if let Some(name_str) = split_csv_row.next() { name_str.into() } else { return Err("name not given"); }; let good_deeds = if let Some(good_deeds_str) = split_csv_row.next() { match good_deeds_str.parse::<u32>() { Ok(num) => num, Err(_) => return Err("number couldn't be parsed"), } } else { return Err("good deeds not given"); }; let bad_deeds = if let Some(bad_deeds_str) = split_csv_row.next() { match bad_deeds_str.parse::<u32>() { Ok(num) => num, Err(_) => return Err("number couldn't be parsed"), } } else { return Err("bad deeds not given"); }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
unwrinkled
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { // fn parse_u32() pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut split_csv_row = csv_row.split(','); let name: String = if let Some(name_str) = split_csv_row.next() { name_str.into() } else { return Err("name not given"); }; let good_deeds = if let Some(good_deeds_str) = split_csv_row.next() { match good_deeds_str.parse::<u32>() { Ok(num) => num, Err(_) => return Err("number couldn't be parsed"), } } else { return Err("good deeds not given"); }; let bad_deeds = if let Some(bad_deeds_str) = split_csv_row.next() { match bad_deeds_str.parse::<u32>() { Ok(num) => num, Err(_) => return Err("number couldn't be parsed"), } } else { return Err("bad deeds not given"); }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
NikhilMukraj
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let split_string: Vec<&str> = csv_row.split(",").collect(); if split_string.len() != 3 { return Err("Invalid row"); } let name = split_string[0].to_string(); let good_deeds = match split_string[1].parse::<u32>() { Ok(val) => val, Err(_) => return Err("Invalid deeds"), }; let bad_deeds = match split_string[2].parse::<u32>() { Ok(val) => val, Err(_) => return Err("Invalid deeds"), }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
maxnavarrollaven
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // Split the input CSV row by commas let mut fields = csv_row.split(','); // Extract the fields and handle missing data let name = fields.next().ok_or("Missing field for name")?.to_string(); let good_deeds: u32 = fields.next().ok_or("Missing field for good deeds")? .parse().map_err(|_| "Invalid good deeds")?; let bad_deeds: u32 = fields.next().ok_or("Missing field for bad deeds")? .parse().map_err(|_| "Invalid bad deeds")?; // Construct a Kid struct using the extracted values Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds > 0 { return false; } 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 }}
sergepin
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { let mut parts = csv_row.split(",").map(str::trim); let name = parts .next() .ok_or("No Column for name") ?.to_string(); let good_deeds: u32 = parts .next() .ok_or("No column for good deeds")? .parse() .map_err(|_| "Invalid good deeds")?; let bad_deeds: u32 = parts .next() .ok_or("No column for bad deeds")? .parse() .map_err(|_| "Invalid bad deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
shell720
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let input_vec: Vec<&str> = csv_row.split(',').collect(); if input_vec.len() != 3{ return Err("not parse csv"); } let name = input_vec[0].to_string(); let good_deeds: u32 = input_vec[1].parse().map_err(|_| "Invalid good deeds")?; let bad_deeds: u32 = input_vec[2].parse().map_err(|_| "Invalid bad deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
Kada-quantum
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let parts = csv_row.split(",").collect::<Vec<&str>>(); if parts.len() != 3 { return Err("Invalid format"); } let name = parts[0].to_string(); let good_deeds = match parts[1].parse::<u32>() { Ok(n) => n, Err(e) => return Err(format!("Invalid format: {e}").leak()), }; let bad_deeds = match parts[2].parse::<u32>() { Ok(n) => n, Err(e) => return Err(format!("Invalid format: {e}").leak()), }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
vabka
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { let mut parts = csv_row.split(',').map(|x|x.trim()); let Some(name) = parts.next() else { return Err("no columns"); }; let Some(Ok(good_deeds)) = parts.next().map(|e|e.parse()) else { return Err("invalid good deeds"); }; let Some(Ok(bad_deeds)) = parts.next().map(|e|e.parse()) else { return Err("invalid bad deeds"); }; Ok(Self::new(name.to_owned(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
vabka
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { let mut parts = csv_row.split(',').map(|x|x.trim()); let Some(name) = parts.next() else { return Err("no columns"); }; let Some(Ok(good_deeds)) = parts.next().map(|e|e.parse()) else { return Err("invalid good deeds"); }; let Some(Ok(bad_deeds)) = parts.next().map(|e|e.parse()) else { return Err("invalid bad deeds"); }; Ok(Self::new(name.to_owned(), good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
FrancoTorresSilva
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let fields:Vec<&str> = csv_row.split(",").collect(); if fields.len() != 3 { return Err("Error"); } let name = if fields[0].is_empty() { return Err("Error"); } else { fields[0].to_string() }; let good_deeds = match fields[1].trim().parse::<u32>() { Ok(value) => value, Err(_) => return Err("Error"), }; let bad_deeds = match fields[2].trim().parse::<u32>() { Ok(value) => value, Err(_) => return Err("Error"), }; Ok(Kid::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
MiguelRosgal
use std::str::FromStr;pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let columns: Vec<&str> = csv_row.split(',').collect(); if columns.len() != 3 { return Err("Bad row"); } let name = columns[0].to_string(); let good_deeds = u32::from_str(columns[1]) .or_else(|_| Err("Invalid good deeds column."))?; let bad_deeds = u32::from_str(columns[2]) .or_else(|_| Err("Invalid bad deeds column."))?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
rostyq
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut values = csv_row.split(","); let name = values.next().map(String::from).ok_or("expect name value")?; let good_deeds = values.next().map(|v| v.parse::<u32>().ok()).flatten().ok_or("expect good deeds")?; let bad_deeds = values.next().map(|v| v.parse::<u32>().ok()).flatten().ok_or("expect bad deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
mEstrazulas
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let mut row = csv_row.split(","); let name = row.next().ok_or("missing")?.to_string(); let good_deeds: u32 = row.next().ok_or("missing")?.parse().map_err(|_| "Invalid good deeds")?; let bad_deeds: u32 = row.next().ok_or("missing")?.parse().map_err(|_| "Invalid good deeds")?; Ok(Self::new(name, good_deeds , bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
rgreinho
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let row_parts: Vec<_> = csv_row.split(',').collect(); let part_count = row_parts.len(); if part_count != 3 { return Err("Invalid part numbers: expected 3, got {part_count}") } let name = row_parts[0].to_string(); let good_deeds = row_parts[1].parse::<u32>().map_err(|e| "cannot parse good deeds")?; let bad_deeds = row_parts[2].parse::<u32>().map_err(|e| "cannot parse bad deeds")?; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
pabiadzinski
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { let mut name = String::new(); let mut good_deeds = None; let mut bad_deeds = None; for (index, value) in csv_row.split(',').enumerate() { match index { 0 => name = value.to_string(), 1 => good_deeds = value.parse::<u32>().ok(), 2 => bad_deeds = value.parse::<u32>().ok(), _ => return Err("Invalid CSV format: Too many fields"), } } match (good_deeds, bad_deeds) { (Some(good), Some(bad)) => Ok(Self::new(name, good, bad)), _ => Err("Invalid CSV format: Could not parse good or bad deeds"), } } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}
m0nkey653
pub const GOOD_WEIGHT: f32 = 1.0;pub const BAD_WEIGHT: f32 = 2.0;#[derive(Debug, PartialEq)]pub enum Niceness { Nice(u32), Naughty,}pub struct Kid { pub name: String, pub niceness: Niceness,}impl Kid { pub fn parse_row(csv_row: &str) -> Result<Kid, &'static str> { // 🎁 Transform the CSV row into a Kid struct for Santa's list! // 🎅 Expected CSV: "Name,GoodDeeds,BadDeeds" // Example: "Alice,3,1" -> name: "Alice", good_deeds: 3, bad_deeds: 1 // 🎁 Your code here! 🎁 let cells: Vec<&str> = csv_row.split(',').collect(); if cells.len() != 3 { return Err("Bugger"); } let name = cells[0].to_string(); let good_deeds_result = cells[1].parse::<u32>(); let good_deeds = match good_deeds_result { Ok(good_deeds_num) => good_deeds_num, Err(_) => return Err("Wow!") }; let bad_deeds_result = cells[2].parse::<u32>(); let bad_deeds = match bad_deeds_result { Ok(bad_deeds_num) => bad_deeds_num, Err(_) => return Err("Wow!") }; Ok(Self::new(name, good_deeds, bad_deeds)) } pub fn new(name: String, good_deeds: u32, bad_deeds: u32) -> Self { let niceness = if Self::is_nice(good_deeds, bad_deeds) { Niceness::Nice(good_deeds) } else { Niceness::Naughty }; Self { name, niceness } } pub fn is_nice(good_deeds: u32, bad_deeds: u32) -> bool { if good_deeds == 0 && bad_deeds == 0 { return false; } let good_deeds = good_deeds as f32 * GOOD_WEIGHT; let bad_deeds = bad_deeds as f32 * BAD_WEIGHT; let ratio = good_deeds / (good_deeds + bad_deeds); ratio >= 0.75 }}