"Santa, we need to talk!" Snowball stammered, holding a stack of printouts that smelled vaguely of desperation.
Santa looked up, his patience thinner than Rudolph’s battery life. "What now?"
Bernard, the senior elf, didn’t mince words. "It’s the Naughty-Nice List system. Legacy code. Written in JavaScript."
Santa froze. "Not even TypeScript?"
Bernard shook his head. "Plain JavaScript. var
everywhere. No types. No safety. It’s a dumpster fire. Someone even polyfilled Promise
with copy-paste."
Santa slammed his mug down. "So Naughty kids get PS5s, and Nice kids get socks because JavaScript?"
Snowball nodded, sweating. "It’s worse, Santa. There’s no validation. The Nice List has a SELECT *
injection, and someone added console.log("Merry Christmas, LOL");
in production!"
Santa pinched the bridge of his nose. "Rewriting it in Rust is our only hope. No globals, no runtime panics—safety guaranteed. Bernard, make it happen."
"But Santa," Bernard hesitated, "starting from scratch this late… it’s risky."
Santa leaned forward, eyes blazing. "Riskier than trusting JavaScript on Christmas Eve? I don’t care how late it is. Write it in Rust."
Implement a SantaList
struct that uses a HashMap
to store children’s names as keys and their behaviors (true
for nice, false
for naughty) as values.
The SantaList
struct should have a single field:
records
- a HashMap<String, bool>
to store children’s names and behaviors.The struct should have the following methods and associated functions:
new
- Create a new SantaList
instance.add
- Add a child's name and behavior to the list.remove
- Remove a child from the list.get
- Retrieve a child's behavior.count
- Count the number of nice and naughty children as a tuple (nice, naughty)
list_by_behavior
- Retrieve a list of children based on their behavior as Vec<String>
If you’re unsure where to start, take a look at these tips:
Using HashMap
: Import it with use std::collections::HashMap;
. Use HashMap::new()
to create an empty map.
Adding/Updating: Use insert(key, value)
to add or update an entry.
Querying: Use get(key)
to retrieve values. It returns an Option<bool>
.
Removing: Use remove(key)
to delete an entry and get its value if it exists.
Counting: Use values()
with .filter()
to count nice/naughty children:
tdoan
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool>{ self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut nice = 0; let mut naughty = 0; for (_k,v) in self.records.iter() { if *v { nice +=1; } else { naughty +=1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut children = Vec::new(); for (k,v) in self.records.iter() { if *v == behavior { children.push(k.to_string()) } } children }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
aaron-otis
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool> ,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let good = self.records.values().filter(|b| **b).count(); (good, self.records.len() - good) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|(_, &v)| v == behavior).map(|(k, _)| k.clone()).collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
Ljungg
use std::collections::HashMap;// Implement a SantaList struct that uses a HashMap to store children’s names as keys and their behaviors (true for nice, false for naughty) as values.// Fields//// The SantaList struct should have a single field://// records - a HashMap<String, bool> to store children’s names and behaviors.//// Methods//// The struct should have the following methods and associated functions://// new - Create a new SantaList instance.// add - Add a child's name and behavior to the list.// remove - Remove a child from the list.// get - Retrieve a child's behavior.// count - Count the number of nice and naughty children as a tuple (nice, naughty)// list_by_behavior - Retrieve a list of children based on their behavior as Vec<String>pub struct SantaList { records: HashMap<String, bool>, // 1. Define the records field}impl SantaList { pub fn new() -> SantaList { SantaList { records: HashMap::new(), } } pub fn add(&mut self, name: &str, behaviour: bool) -> Option<bool> { self.records.insert(name.to_string(), behaviour) } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let nice_count = self.records.iter().filter(|&(_k, v)| *v).count(); let bad_count = self.records.len() - nice_count; (nice_count, bad_count) } pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records .iter() .filter(|&(_k, v)| *v == behaviour) .map(|(k, _v)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
josephcopenhaver
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> SantaList { let records = HashMap::<String, bool>::new(); SantaList{ records, } } // 3. Implement the add method pub fn add(&mut self, name: &str, is_nice: bool) { self.records.insert(name.to_owned(), is_nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let n = self.records.keys().len(); let naughty = self.records.iter().filter(|(_,v)|!*v).count(); (n-naughty, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, b: bool) -> Vec<String> { self.records.iter().filter(|&(_,v)|b == *v).map(|(k,_)|k.to_owned()).collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
thescooby
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>}impl SantaList { pub fn new() -> SantaList { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).cloned() } // 6. Implement the count method pub fn count(& self) -> (usize, usize) { let nice = self.list_by_behavior(true).len(); let naughty = self.list_by_behavior(false).len(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self .records .iter() .filter(|(_, v)| **v == behavior) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
eguefif
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> SantaList { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(String::from(name), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(& self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let mut nice = 0; let mut naughty = 0; for value in self.records.values(){ if *value { nice += 1; } else { naughty += 1; } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut retval = Vec::new(); self.records.iter().for_each(|(key, value)| { if *value == behavior { retval.push(key.clone()); } }); retval }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
overplumbum
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, record: &str, behavior: bool) { self.records.insert(record.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, record: &str) { self.records.remove(record); } // 5. Implement the get method pub fn get(&self, record: &str) -> Option<bool> { self.records.get(record).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.list_by_behavior(true).len(); let naughty = self.list_by_behavior(false).len(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self .records .iter() .filter(|(_, v)| **v == behavior) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
A1-exe
use std::collections::HashMap;#[derive(Debug)]pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, record: &str, behavior: bool) { self.records.insert(record.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, record: &str) { self.records.remove(record); } // 5. Implement the get method pub fn get(&self, record: &str) -> Option<bool> { self.records.get(record).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.list_by_behavior(true).len(); let naughty = self.list_by_behavior(false).len(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self .records .iter() .filter(|(_, v)| **v == behavior) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
rjensen
use std::collections::HashMap;#[derive(Debug)]pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, record: &str, behavior: bool) { self.records.insert(record.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, record: &str) { self.records.remove(record); } // 5. Implement the get method pub fn get(&self, record: &str) -> Option<bool> { self.records.get(record).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.list_by_behavior(true).len(); let naughty = self.list_by_behavior(false).len(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self .records .iter() .filter(|(_, v)| **v == behavior) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
sarub0b0
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { self.records.values().fold((0, 0), |count, behavior| { if *behavior { (count.0 + 1, count.1) } else { (count.0, count.1 + 1) } }) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_, &v)| v == behavior) .map(|(k, _)| k.to_string()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
FlorianGD
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::default()} } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nices = self.records.iter().filter(|(_, v)| **v).count(); let not_nices = self.records.iter().filter(|(_, v)| !**v).count(); (nices, not_nices) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|(_, v)| **v == behavior).map(|(k, _)| k.clone()).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
habu1010
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { let records = HashMap::new(); Self { records } } // 3. Implement the add method pub fn add(&mut self, name: &str, is_nice: bool) { self.records.insert(name.to_string(), is_nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&x| *x).count(); let naughty = self.records.values().filter(|&x| !*x).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_, &v)| v == behavior) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
vaclav0411
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> SantaList { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, value: bool) { self.records.insert(name.into(), value); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let res = self.records.values().filter(|&&x| x).count(); (res, self.records.len() - res) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|&(_, x)| *x == behavior) .map(|(y, _)| y.to_string()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
mei28
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records:HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, key:&str, value:bool) { self.records.insert(key.to_string(), value); } // 4. Implement the remove method pub fn remove(&mut self, key:&str) { self.records.remove(&key.to_string()); } // 5. Implement the get method pub fn get(&self, key:&str) -> Option<bool> { self.records.get(key).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize,usize) { let mut counter = (0usize,0usize); self.records.iter().for_each(|(_,b)| { if *b { counter.0 += 1; }else { counter.1 += 1; } }); counter } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, good: bool) -> Vec<String> { let mut counter:Vec<String> = vec![]; self.records.iter().for_each(|(a,&b)| { if b == good { counter.push(a.clone()); } }); counter }}
sweet2honey
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> SantaList { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let good: usize = self.records.values().filter(|v| **v == true).count(); let bad: usize = self.records.values().filter(|v| **v == false).count(); // return value (good, bad) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.records .iter() .filter(|(_, &behaviour)| behaviour == nice) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
pagyeman
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> SantaList { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let good: usize = self.records.values().filter(|v| **v == true).count(); let bad: usize = self.records.values().filter(|v| **v == false).count(); // return value (good, bad) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.records .iter() .filter(|(_, &behaviour)| behaviour == nice) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
titoeb
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> SantaList { SantaList { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.into(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let mut number_of_nice_children = 0; let mut number_of_naughty_children = 0; for (_, &nice) in &self.records { if nice { number_of_nice_children += 1; } else { number_of_naughty_children += 1; } } (number_of_nice_children, number_of_naughty_children) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, was_nice: bool) -> Vec<String> { self.records .iter() .filter(|(_, &behaviour)| behaviour == was_nice) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
kapaseker
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records:HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, key:&str, value:bool) { self.records.insert(key.to_string(), value); } // 4. Implement the remove method pub fn remove(&mut self, key:&str) { self.records.remove(&key.to_string()); } // 5. Implement the get method pub fn get(&self, key:&str) -> Option<bool> { self.records.get(key).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize,usize) { let mut counter = (0usize,0usize); self.records.iter().for_each(|(_,b)| { if *b { counter.0 += 1; }else { counter.1 += 1; } }); counter } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, good: bool) -> Vec<String> { let mut counter:Vec<String> = vec![]; self.records.iter().for_each(|(a,&b)| { if b == good { counter.push(a.clone()); } }); counter }}
wendko
use std::{collections::HashMap};pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { let mut list = Self { records: HashMap::new() }; list } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) -> Option<bool> { self.records.insert(name.into(), behavior) } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); (); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).map(|x| *x) } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let val = self.records.clone().into_values().collect::<Vec<bool>>(); let total = val.len(); let nice = val.into_iter().filter(|v| *v).count(); let naughty = total - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut list: Vec<String> = vec![]; for (key, val) in self.records.iter() { if *val == behavior { list.push(key.into()); } } list }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
MGehrmann
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&name.to_string()).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { ( self.records.values().filter(|nice| **nice == true).count(), self.records.values().filter(|nice| **nice == false).count() ) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|(_, b)| **b == behavior).map(|(name, _)| name.clone()).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
vihamaki
use std::collections::HashMap;//use std::collections::List;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).map(|behavior| behavior.clone() ) } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { return self.records.values().fold((0,0), |(nice, naughty), item| if *item { (nice + 1, naughty) } else { (nice, naughty + 1) } ); } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, wanted_behavior: bool) -> Vec<String> { let mut ret = Vec::new(); for (name, &behavior) in &self.records { if wanted_behavior == behavior { ret.push(name.to_string()); } } return ret; }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
KushnerykPavel
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string().clone(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&name.to_string()).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.iter().filter(|(_, v)| **v == true).count(); let naughty = self.records.len() - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self .records .iter() .filter(|&(_, &v)| v == behavior ) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
kometen
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string().clone(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&name.to_string()).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.iter().filter(|(_, v)| **v == true).count(); let naughty = self.records.len() - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self .records .iter() .filter(|&(_, &v)| v == behavior ) .map(|(k, _)| k.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
frankstolle
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method3896 pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_owned(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.iter().filter(|(_, nice)| **nice).count(); (nice, self.records.len() - nice) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.records .iter() .filter(|(_, record_nice)| **record_nice == nice) .map(|(name, _)| name.to_owned()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
Fedott
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string(), nice); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { if let Some(b) = self.records.get(name) { Some(*b) } else { None } } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { self.records.iter().fold((0, 0), |result, item| { if *item.1 { (result.0 + 1, result.1) } else { (result.0, result.1 + 1) } }) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<&str> { self.records.iter().filter_map(|x1| { if *x1.1 == behavior { Some(x1.0.as_str()) } else { None } }).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
hafihaf123
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { let records = HashMap::new(); Self { records } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&&behavior| behavior).count(); let naughty = self.records.len() - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|&(_, &value)| value == behavior) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
joanne-cmd
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList{ records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|v| **v).count(); let naughty = self.records.values().filter(|v| !**v).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut list = vec!(); for (key, value) in &self.records { if *value == behavior { list.push(key.to_string()); } } list }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
CianciuStyles
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(&name.to_string()); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(&name.to_string()).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { ( self.records.values().filter(|nice| **nice == true).count(), self.records.values().filter(|nice| **nice == false).count() ) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|(_, b)| **b == behavior).map(|(name, _)| name.clone()).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
gmvar
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList{ records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|v| **v).count(); let naughty = self.records.values().filter(|v| !**v).count(); (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut list = vec!(); for (key, value) in &self.records { if *value == behavior { list.push(key.to_string()); } } list }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
strachan
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { (self.records.values().filter(|x| **x).count(), self.records.values().filter(|x| !**x).count()) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|(_, &how_behaves) | how_behaves == behavior).map(|(name, _)| name.clone()).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
arm01846
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList { records: HashMap::new() } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.into(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).cloned() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let (nice_list, naughty_list): (Vec<_>, Vec<_>) = self.records.iter().partition(|(_, behavior)| **behavior == true); (nice_list.len(), naughty_list.len()) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|x| *x.1 == behavior) .map(|x| x.0.clone()) .collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
tamanishi
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { (self.records.values().filter(|value| **value == true).cloned().collect::<Vec<bool>>().len(), self.records.values().filter(|value| **value == false).cloned().collect::<Vec<bool>>().len()) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { let mut list = Vec::new(); for (key, value) in &self.records { if *value == behavior { list.push(key.to_string()); } } list }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
Ankit8848
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new() } } pub fn add(&mut self, name: &str, behaviour: bool) { self.records.insert(name.to_string(), behaviour); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let nice = self.records .values() .filter(|&&b| b) .count(); let naughty = self.records .len() .abs_diff(nice); (nice, naughty) } pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records .iter() .filter_map(|(n, b)| { if *b == behaviour { Some(n.clone()) } else { None } }) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
DominicD
use std::collections::HashMap;pub struct SantaList { pub records: HashMap<String,bool>}impl SantaList { pub fn new() -> Self { SantaList{ records : Default::default(), } } pub fn add(&mut self, name: &str, good: bool) { self.records.insert(name.into(),good); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (u32,u32) { let mut nice = 0; let mut naughty = 0; for behavior in self.records.values() { match behavior { true => {nice +=1;}, false => {naughty += 1;} } } return (nice,naughty) } pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records.iter().filter( |(_k,v)| **v == behaviour).map(|(k,_v)| k.clone()).collect() } // 2. Implement the new method // 3. Implement the add method // 4. Implement the remove method // 5. Implement the get method // 6. Implement the count method // 7. Implement the list_by_behavior method}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
rony0000013
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new() } } pub fn add(&mut self, name: &str, behaviour: bool) { self.records.insert(name.to_string(), behaviour); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let nice = self.records .values() .filter(|&&b| b) .count(); let naughty = self.records .len() .abs_diff(nice); (nice, naughty) } pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records .iter() .filter_map(|(n, b)| { if *b == behaviour { Some(n.clone()) } else { None } }) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
Stephan-Lindner
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new() } } pub fn add(&mut self, name: &str, behaviour: bool) { self.records.insert(name.to_string(), behaviour); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let nice = self.records .values() .filter(|&&b| b) .count(); let naughty = self.records .len() .abs_diff(nice); (nice, naughty) } pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records .iter() .filter_map(|(n, b)| { if *b == behaviour { Some(n.clone()) } else { None } }) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
wamimi
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new() } } pub fn add(&mut self, name: &str, behavior: bool) -> bool { let name = name.to_string(); let old_value = self.records.insert(name, behavior); old_value.is_none() } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&&b| b).count(); let naughty = self.records.len() - nice; (nice, naughty) } pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.records .iter() .filter(|&(_, &behavior)| behavior == nice) .map(|(name, _)| name.clone()) .collect() }}
chriswmann
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field pub records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behaviour: bool) { self.records.insert(name.to_string(), behaviour); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut naughty = 0; let mut nice = 0; for behaviour in self.records.values() { match behaviour { true => nice += 1, false => naughty += 1, } }; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behaviour: bool) -> Vec<String> { self.records.iter().filter_map(|(n, b)| { if *b == behaviour { Some(n.clone()) } else { None } }).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
GiulianoCTRL
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.entry(name.to_string()) .and_modify(|e| { *e = behavior }) .or_insert(behavior); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let nice_count = self.records.values() .filter(|b| **b) .count(); let naughty_count = self.records.len().abs_diff(nice_count); (nice_count, naughty_count) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_, b)| **b == behavior) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
IslameN
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>}impl SantaList { pub fn new() -> SantaList { Self { records: HashMap::new() } } pub fn add(&mut self, name: &str, nice: bool) { self.records.insert(name.to_string(), nice); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).cloned() } pub fn count(&self) -> (usize, usize) { let mut nice = 0; let mut naughty = 0; for (_, &behavior) in &self.records { if behavior { nice += 1; } else { naughty += 1; } } (nice, naughty) } pub fn list_by_behavior(&self, nice: bool) -> Vec<String> { self.records .iter() .filter(|&(_, &behavior)| behavior == nice) .map(|(name, _)| name.to_string()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
gjdenhertog
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new() } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } pub fn remove(&mut self, name: &str) { if self.records.contains_key(name) { self.records.remove(name); } } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let cnt = self.records.values().filter(|i| **i).count(); (cnt, self.records.len() - cnt) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter_map(|(k, v)| if *v == behavior { Some(k.to_string()) } else { None }).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
SirVer
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { SantaList { records: HashMap::new() } } pub fn add(&mut self, name: impl Into<String>, val: bool) { self.records.insert(name.into(), val); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let cnt = self.records.values().filter(|i| **i).count(); (cnt, self.records.len() - cnt) } pub fn list_by_behavior(&self, val: bool) -> Vec<String> { self.records.iter().filter_map(|(k,v)| if *v == val { Some(k.to_string()) } else { None }).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
DomiDre
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>}impl SantaList { // 2. Implement the new method pub fn new() -> Self { let records = HashMap::new(); SantaList { records } } // 3. Implement the add method pub fn add(&mut self, child_name: &str, behavior: bool) { self.records.insert(child_name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, child_name: &str) { self.records.remove(&child_name.to_string()); } // 5. Implement the get method pub fn get(&self, child_name: &str) -> Option<bool> { self.records.get(&child_name.to_string()).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|v| **v == true).count(); let naughty = self.records.len() - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|&(_, value)| *value == behavior).map(|(key, _)| key.clone()).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
KLcpb
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } pub fn add(&mut self, name: &str, is_nice: bool) { self.records.insert(name.to_string(), is_nice); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { if let Some(result) = self.records.get(name) { return Some(*result) } None } pub fn count(&self) -> (usize, usize) { let total = self.records.len(); let nice_count = self.records.values() .filter(|is_nice| **is_nice).count(); (nice_count, total - nice_count) } pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { let mut result = Vec::new(); for (name, &behavior) in self.records.iter() { if behavior == is_nice { result.push(name.to_string()) } } result }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
Burnus
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } pub fn add(&mut self, child: &str, behavior: bool) { self.records.insert(child.into(), behavior); } pub fn remove(&mut self, child: &str) { self.records.remove(child); } pub fn get(&self, child: &str) -> Option<bool> { self.records.get(child).map(|&c| c) } pub fn count(&self) -> (usize, usize) { let nice = self.records.iter().filter(|(_c, b)| **b).count(); (nice, self.records.len() - nice) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records .iter() .filter(|(_c, b)| **b == behavior) .map(|(c, _b)| c.to_string()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
wlabranche
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new(), } } pub fn add(&mut self, name: &str, is_nice: bool) { self.records.insert(name.to_string(), is_nice); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { if let Some(result) = self.records.get(name) { return Some(*result) } None } pub fn count(&self) -> (usize, usize) { let total = self.records.len(); let nice_count = self.records.values() .filter(|is_nice| **is_nice).count(); (nice_count, total - nice_count) } pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { let mut result = Vec::new(); for (name, &behavior) in self.records.iter() { if behavior == is_nice { result.push(name.to_string()) } } result }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
jean-roland
use std::collections::HashMap;pub struct SantaList { // 1. Define the records field records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { SantaList{ records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove(&mut self, name: &str) { self.records.remove(name); } // 5. Implement the get method pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { let nice = self.records.values().filter(|&&value| value).count(); let naughty = self.records.len() - nice; (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|&(_, &value)| value == behavior) .map(|(key, _)| key.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
lulingar
use std::{ collections::HashMap, convert::AsRef,};pub struct SantaList { records: HashMap<String, bool>,}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add<T>(&mut self, name: T, behavior: bool) where T: AsRef<str> + std::string::ToString { self.records.insert(name.to_string(), behavior); } // 4. Implement the remove method pub fn remove<T>(&mut self, name: T) where T: AsRef<str> + std::string::ToString { self.records.remove(name.as_ref()); } // 5. Implement the get method pub fn get<T>(&self, name: T) -> Option<bool> where T: AsRef<str> + std::string::ToString { match self.records.get(name.as_ref()) { Some(b) => Some(*b), None => None } } // 6. Implement the count method pub fn count(&self) -> (usize, usize) { self.records.iter() .fold( (0, 0), |(nice, naughty), (_, &v)| { if v { (nice + 1, naughty) } else { (nice, naughty + 1) } } ) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter() .filter(|(_, &how_behaves)| how_behaves == behavior) .map(|(name, _)| name.clone()) .collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
tonisk
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>, // 1. Define the records field}impl SantaList { // 2. Implement the new method pub fn new() -> Self { Self { records: HashMap::new(), } } // 3. Implement the add method pub fn add(&mut self, child: &str, nice: bool) { self.records.insert(child.into(), nice); } // 4. Implement the remove method pub fn remove(&mut self, child: &str) { self.records.remove(child); } // 5. Implement the get method pub fn get(&self, child: &str) -> Option<bool> { self.records.get(child).copied() } // 6. Implement the count method pub fn count(&self) -> (u32, u32) { let mut nice = 0; let mut naughty = 0; for (_, is_nice) in self.records.iter() { match is_nice { true => nice += 1, false => naughty += 1, } } (nice, naughty) } // 7. Implement the list_by_behavior method pub fn list_by_behavior(&self, is_nice: bool) -> Vec<String> { self.records.iter() .filter(|(_, &niceness)| niceness == is_nice) .map(|(name, _)| name.clone()) .collect::<Vec<String>>() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}
sriharshamadala
use std::collections::HashMap;pub struct SantaList { records: HashMap<String, bool>}impl SantaList { pub fn new() -> Self { Self { records: HashMap::new() } } pub fn add(&mut self, name: &str, behavior: bool) { self.records.insert(name.to_string(), behavior); } pub fn remove(&mut self, name: &str) { self.records.remove(name); } pub fn get(&self, name: &str) -> Option<bool> { self.records.get(name).copied() } pub fn count(&self) -> (usize, usize) { let nice_count = self.records.iter().filter(|e| *(e.1)).count(); let naughty_count = self.records.iter().filter(|e| !*(e.1)).count(); (nice_count, naughty_count) } pub fn list_by_behavior(&self, behavior: bool) -> Vec<String> { self.records.iter().filter(|e| *(e.1) == behavior).map(|e| e.0.clone()).collect() }}pub fn main() { let mut santa_list = SantaList::new(); santa_list.add("Alice", true); santa_list.add("Bob", false); santa_list.add("Charlie", true); if let Some(behavior) = santa_list.get("Alice") { println!( "Alice is on the {} list.", if behavior { "Nice" } else { "Naughty" } ); } let (nice, naughty) = santa_list.count(); println!( "Santa's list contains {} nice and {} naughty children.", nice, naughty ); let nice_list = santa_list.list_by_behavior(true); println!("Nice children: {:?}", nice_list); let naughty_list = santa_list.list_by_behavior(false); println!("Naughty children: {:?}", naughty_list); santa_list.remove("Bob"); let (nice, naughty) = santa_list.count(); println!( "After removing Bob, Santa's list contains {} nice and {} naughty children.", nice, naughty );}