Hashmaps are a powerful data structure that allow you to store key-value pairs. In Rust, the HashMap
type is provided that uses the a hashing algorithm called SipHash to store keys and values in a way that allows for fast and secure lookups. Think of them as a dictionary in Python or an object in JavaScript.
In this challenge, we want to build a sanctuary registry that allows us to manage animals in different sections of the sanctuary. We'll use a HashMap
to store the sections as keys and a Vec
to store the animals in each section. Each key is a section name String
and each value is a list of animals in that section Vec<String>
.
You are given a type of Collection
which is a HashMap<String, Vec<String>>
. The key is the section name and the value is a list of animals in that section.
Your task is to implement the following functions:
add_animal_to_section
: This function should add an animal to a section in the registry. If the section does not exist, it should be created. If the animal is already in the section, it should not be added again.
get_animals_in_section
: This function should return a list of animals sorted alphabetically in a given section. If the section does not exist, it should return an empty list.
get_all_animals_sorted
: This function should return a copy of the entire registry with all animals sorted alphabetically in each section.
insert
method on a HashMap
to add a new section or update an existing section.entry
method on a HashMap
to get a mutable reference to a section.iter
method on a HashMap
to iterate over the key-value pairs.sort
method on a Vec
to sort the animals alphabetically.jimlawton
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let section_list = get_animals_in_section(section, registry); if !section_list.contains(&animal.to_string()) { let mut new_val = vec![animal.to_string()]; registry .entry(section.to_string()) .and_modify(|v| { v.append(&mut new_val) }) .or_insert(new_val); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut results: Vec<String> = match registry.get(section) { Some(v) => v.to_vec(), None => Vec::new(), }; results.sort(); results}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let sections = registry.keys(); let mut results: Vec<String> = Vec::new(); for section in sections { let mut values = get_animals_in_section(section, registry); if values.len() > 0 { results.append(&mut values); } } results.sort(); results}
yoakemae
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal_vec = registry.entry(section.to_string()).or_insert_with(Vec::new); if !animal_vec.contains(&animal.to_string()) { animal_vec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).cloned().unwrap_or_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); // ここでソート animals}
DivineGod
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry.entry(section.to_string()).and_modify(|section| if !section.contains(&animal.to_string()) { section.push(animal.to_string())} ).or_insert_with(|| vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = if let Some(animals) = registry.get(section) { animals.to_vec() } else { Vec::<String>::new() }; animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.values().flatten().map(|v| v.clone()).collect::<Vec<_>>(); animals.sort(); animals}
StimhackSoftware
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry.entry(section.to_string()).and_modify(|e| { let new_animal = animal.to_string(); if ! e.contains(&new_animal) { e.push(new_animal); } }).or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(x) = registry.get(section) { let mut y = x.clone(); y.sort(); y } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_ani : Vec<_> = registry.values().into_iter().flatten().cloned().collect(); all_ani.sort(); all_ani}
sroas
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let section = section.to_string(); registry .entry(section) .and_modify(|s| { let animal = animal.to_string(); if !s.contains(&animal) { s.push(animal); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(s) => { let mut sorted = s.clone(); sorted.sort(); sorted } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut f = registry .values() .flat_map(|it| it.to_owned()) .collect::<Vec<_>>(); f.sort(); f}
nknknknkn2
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let rr = registry.get_mut(section); match rr { Some(r) => { if r.contains(&animal.to_string()) == false { r.push(animal.to_string()); } }, None => { let mut v = Vec::new(); v.push(animal.to_string()); registry.insert(section.to_string(), v); } }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(r) => { let mut sorted_animals = r.clone(); sorted_animals.sort(); return sorted_animals; }, None => { return Vec::new(); } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = registry.values().flat_map(|vec| vec.clone()).collect(); animals.sort(); return animals;}
diegocmsantos
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut new_collection = HashMap::new(); if let Some(animals) = registry.get(section) { new_collection.insert(section.to_string(), animals.clone()); } get_all_animals_sorted(&new_collection)}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry .values() // Get an iterator of &Vec<String> .flat_map(|vec| vec.iter().cloned()) // Flatten and clone each string .collect(); // Collect into Vec<String> all_animals.sort(); // Sort alphabetically all_animals}
qiyuan711
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(vec![]); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.get(section).cloned().unwrap_or_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); animals}
tinthid
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let mut animals = registry.entry(section.to_string()).or_insert(vec![]); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = registry.get(section).cloned().unwrap_or_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); animals}
tamanishi
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry.entry(section.to_string()).and_modify(|e| if !(*e).contains(&animal.to_string()) {(*e).push(animal.to_string())}).or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(e) => {let mut ee = e.to_vec(); ee.sort(); ee}, None => Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all = Vec::new(); for e in registry.values() { all.extend(e.clone()); } all.sort(); all}
Thymelizabeth
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal = String::from(animal); registry.entry(String::from(section)).and_modify(|section| if !section.contains(&animal) { section.push(animal.clone()) }).or_insert(vec![animal]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result = registry.get(&String::from(section)).map(|s| s.clone()).unwrap_or(vec![]); result.sort(); result.to_vec()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result = registry.values().cloned().flatten().collect::<Vec<_>>(); result.sort(); result}
konishu
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let entry = registry.entry(section.into()).or_insert(Vec::with_capacity(1)); let animal: String = animal.into(); if !entry.contains(&animal) { entry.push(animal) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.get(section.into()).map(|x| x.clone()).unwrap_or(Vec::new()); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.values().flatten().map(|x| x.to_owned()).collect(); result.sort(); result}
anthonycabreralara
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let entry = registry.entry(section.into()).or_insert(Vec::with_capacity(1)); let animal: String = animal.into(); if !entry.contains(&animal) { entry.push(animal) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.get(section.into()).map(|x| x.clone()).unwrap_or(Vec::new()); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.values().flatten().map(|x| x.to_owned()).collect(); result.sort(); result}
0xsmarter
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = match registry.get(section) { Some(animals) => animals.clone(), None => Vec::new(), }; animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flat_map(|v| v.clone()).collect(); animals.sort(); animals}
aynugek
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal = animal.to_string(); let section = section.to_string(); registry .entry(section) .and_modify(|animals| { if !animals.contains(&animal) { animals.push(animal.clone()); } }) .or_insert(vec![animal]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry .get(section) .map(|animals| { let mut sorted = animals.to_vec(); sorted.sort(); sorted }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry .values() .flat_map(|section| section.iter().cloned()) .collect(); all_animals.sort(); all_animals}
aynugek
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animal = animal.to_string(); let section = section.to_string(); registry .entry(section) .and_modify(|animals| { if !animals.contains(&animal) { animals.push(animal.clone()); } }) .or_insert(vec![animal]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry .get(section) .map(|animals| { let mut sorted = animals.to_vec(); sorted.sort(); sorted }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry .values() .flat_map(|section| section.iter().cloned()) .collect(); all_animals.sort(); all_animals}
cip999
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let section = section.to_string(); let animal = animal.to_string(); let section_animals = registry .entry(section) .or_insert(Vec::new()); if !section_animals.contains(&animal) { section_animals.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry .get(section) .unwrap_or(&Vec::new()) .clone(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = registry.into_iter() .map(|(_, animals)| animals.clone()) .flatten() .collect::<Vec<_>>(); all_animals.sort(); all_animals}
tsucchinoko
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = match registry.get(section) { Some(animals) => animals.clone(), None => Vec::new(), }; animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flat_map(|v| v.clone()).collect(); animals.sort(); animals}
Rolando1994
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let mut vec = registry.entry(section.to_string()).or_insert(Vec::new()); let an: String = animal.into(); if !vec.contains(&an) { vec.push(an); } //registry.insert(section.to_string(), vec.to_vec());}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.get(section.into()).map(|x| x.clone()).unwrap_or(Vec::new()); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vec:Vec<String> = Vec::new(); for (key, value) in registry.iter() { for x in value { vec.push(x.to_string()); } } vec.sort(); vec}
cloki0610
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let entry = registry.entry(section.into()).or_insert(Vec::with_capacity(1)); let animal: String = animal.into(); if !entry.contains(&animal) { entry.push(animal) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.get(section.into()).map(|x| x.clone()).unwrap_or(Vec::new()); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.values().flatten().map(|x| x.to_owned()).collect(); result.sort(); result}
Bubbet
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let entry = registry.entry(section.into()).or_insert(Vec::with_capacity(1)); let animal: String = animal.into(); if !entry.contains(&animal) { entry.push(animal) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.get(section.into()).map(|x| x.clone()).unwrap_or(Vec::new()); result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result: Vec<_> = registry.values().flatten().map(|x| x.to_owned()).collect(); result.sort(); result}
whitwulf
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry .entry(section.to_string()) .and_modify(|v| { v.push(animal.to_string()); v.dedup(); }) .or_insert(vec![animal.to_string()]); return ();}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vec = registry.get(section).cloned().unwrap_or_default(); vec.sort(); vec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vec: Vec<String> = registry.iter().flat_map(|(_, v)| v).cloned().collect(); vec.sort(); vec}
maxvi
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert_with(Vec::new); if animals.iter().find(|&a| a == animal).is_none() { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry .iter() .flat_map(|(_, animals)| animals.clone()) .collect(); animals.sort(); animals}
shinobu52
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|v| { v.push(animal.to_string()); v.dedup(); }) .or_insert(vec![animal.to_string()]); return ();}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut vec = registry.get(section).cloned().unwrap_or_default(); vec.sort(); vec}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut vec: Vec<String> = registry.iter().flat_map(|(_, v)| v).cloned().collect(); vec.sort(); vec}
igroomgrim
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // Check if the section exists, if not, create it registry.entry(section.to_string()).or_insert_with(Vec::new); // Add the animal if it's not already in the section if let Some(animals) = registry.get_mut(section) { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // Return an empty list if the section does not exist if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); sorted_animals.sort(); // Sort the animals alphabetically sorted_animals } else { Vec::new() // Return an empty list }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry.values() .flat_map(|animals| animals.iter()) .cloned() .collect(); all_animals.sort(); // Sort all animals alphabetically all_animals}
majesticalcreature
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let sec = registry.entry(section.to_string()) .or_insert(vec![]); let anistr = animal.to_string(); if !sec.contains(&anistr) { sec.push(anistr); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(§ion.to_string()) { Some(s) => { let mut copy = s.to_vec(); copy.sort(); return copy; }, None => return vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result: Vec<String> = vec![]; registry.values().for_each( |s| result.append(&mut s.to_vec()) ); result.sort(); result}
majesticalcreature
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let sec = registry.entry(section.to_string()) .or_insert(vec![]); let anistr = animal.to_string(); if !sec.contains(&anistr) { sec.push(anistr); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(§ion.to_string()) { Some(s) => { let mut copy = s.to_vec(); copy.sort(); return copy; }, None => return vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result: Vec<String> = vec![]; for section in registry.values() { result.append(&mut section.to_vec()); } result.sort(); result}
armed
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); let animal = animal.to_string(); if !animals.contains(&animal) { animals.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry .get(section) .cloned() .unwrap_or_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry .values() .flat_map(|animals| animals.clone()) .collect(); all_animals.sort(); all_animals}
its-me-sv
type Collection = std::collections::HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let section = registry.entry(section.to_string()).or_default(); let animal = animal.to_string(); if !section.contains(&animal) { section.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut animals = animals.to_owned(); animals.sort(); animals } None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals = registry .values() .flat_map(|v| v.to_owned()) .collect::<Vec<String>>(); all_animals.sort(); all_animals}
its-me-sv
type Collection = std::collections::HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry .entry(section.to_string()) .and_modify(|x| { if !x.contains(&animal.to_string()) { x.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry .get(section) .map(|x| x.to_owned()) .unwrap_or_default(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals = registry .values() .flat_map(|v| v.to_owned()) .collect::<Vec<String>>(); all_animals.sort(); all_animals}
NathanaelRea
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let s = registry.get_mut(section); match s { Some(v) => { let aniStr = animal.to_string(); if !v.iter().find(|a| aniStr == **a).is_none() { return; } v.push(aniStr); } None => { registry.insert(section.to_string(), vec![animal.to_string()]); } };}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.get(section).unwrap_or(&Vec::<String>::new()).clone(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.values().flat_map(|v| v.clone()).collect::<Vec<_>>(); animals.sort(); animals}
raneid
use std::collections::{ HashMap, HashSet };type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let list = registry.entry(section.to_string()).or_insert(Vec::<String>::new()); let animal = animal.to_string(); if !list.contains(&animal) { list.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let Some(e) = registry.get(section) else { return vec![]; }; let mut e = e.clone(); e.sort(); e}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all: HashSet<String> = HashSet::new(); for (_, sec) in registry.iter() { for animal in sec.iter() { all.insert(animal.to_string()); } } let mut result: Vec<String> = all.into_iter().collect(); result.sort(); result}
MegaThorx
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let section = registry.entry(section.to_string()).or_insert(vec![]); let animal = animal.to_string(); if !section.contains(&animal) { section.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(animals) => { let mut animals = animals.clone(); animals.sort(); animals.to_vec() }, None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals = registry.values().flat_map(|animal| animal.clone()).collect::<Vec<String>>(); animals.sort(); animals}
mrsalo
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let section = section.to_string(); let animal = animal.to_string(); registry .entry(section) .and_modify(|animals| { if !animals.contains(&animal) { animals.push(animal.clone()); } }) .or_insert(vec![animal]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if !registry.contains_key(section) { return vec![]; } let mut v = registry .get(section) .unwrap() .iter() .map(|x| x.to_string()) .collect::<Vec<String>>(); v.sort(); v}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut v = registry.iter() .map(|(_k, v)| v.clone()) .flatten() .collect::<Vec<_>>(); v.sort(); v}
uggla
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let animal = animal.to_string(); let animals = registry.entry(section.to_string()).or_insert(vec![animal.clone()]); if !animals.contains(&animal){ animals.push(animal) };}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section){ None => vec![], Some(animals) => { let mut animals = animals.clone(); animals.sort(); animals }, }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = registry.values().flat_map(|o| o.clone()).collect::<Vec<String>>(); animals.sort(); animals}
saukmn
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let section = registry.entry(section.to_string()).or_insert(Vec::<String>::new()); let animal = animal.to_string(); if !section.contains(&animal) { section.push(animal); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(section).unwrap_or(&Vec::new()).to_vec(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<_> = registry.values().map(|v| v.to_vec()).flatten().collect(); animals.sort(); animals}
ludovicknecht
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry.entry(section.to_string()) .and_modify(|v| { if !v.contains(&animal.to_string()) { v.push(animal.to_string()) } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = match registry.get(section) { Some(vec) => vec.clone(), _ => return vec![] }; animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = registry.clone().into_values().flatten().collect(); all_animals.sort(); all_animals}
andrey-yu
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let sec = registry .entry(section.to_string()) .or_insert(Vec::<String>::new()); if !sec.contains(&animal.to_string()) { sec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function registry .get(section) .map_or_else(|| Vec::<String>::new(), |sec| { let mut vec = sec.clone(); vec.sort(); vec })}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vec: Vec<String> = registry.values().flat_map(|sec| sec.iter()) .map(|animal| animal.to_string()).collect(); vec.sort(); vec}
wadjoh
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { match registry.get_mut(section) { Some(animals) => { if !animals.contains(&animal.to_owned()) { animals.push(animal.to_owned()); } }, None => { registry.insert(section.to_owned(), vec![animal.to_owned()]); } };}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(animals) => { let mut animals_result: Vec<String> = animals.to_vec(); animals_result.sort(); animals_result }, None => Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals: Vec<String> = Vec::new(); for animals in registry.values() { all_animals.extend(animals.clone()); } all_animals.sort(); all_animals}
kalley
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function match registry.get_mut(section) { Some(list) => { if !list.contains(&animal.into()) { list.push(animal.into()); } }, None => { registry.insert(section.into(), vec![animal.into()]); } };}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(list) => { let mut sorted = list.clone(); sorted.sort(); sorted }, None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut sorted: Vec<_> = registry.values().flatten().map(String::to_string).collect(); sorted.sort(); sorted}
wendko
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry.entry(section.to_string()) .and_modify(|animals| { if animals.iter().find(|&x| *x == animal.to_string()).is_none() { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function if let Some(animals) = registry.get(section) { let mut result = animals.to_vec(); result.sort(); result } else { vec![].into() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut result = vec![]; for (_k, v) in registry.iter() { let mut animals = v.to_vec(); result.append(&mut animals); } result.sort(); result}
daves003
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let a = animal.to_string(); let e = registry.entry(section.to_string()).or_default(); if !e.contains(&a) { e.push(a); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut res = registry.get(§ion.to_string()).unwrap_or(&vec!()).clone(); res.sort(); res}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res = registry.iter().fold(vec!(), |vec, (_k, v)| [vec, v.clone()].concat()); res.sort(); res.dedup(); res}
abigOrange
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function registry.entry(section.to_owned()).and_modify(|v| { let a = animal.to_owned(); if !v.contains(&a) { v.push(a); } }).or_insert(vec![animal.to_owned()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut ret = animals.clone(); ret.sort_unstable(); ret } None => vec![], }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals: Vec<String> = registry.values().flat_map(|animals| animals.clone()).collect(); all_animals.sort_unstable(); all_animals}
FranklinChen
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry.entry(section.to_owned()).and_modify(|v| { let a = animal.to_owned(); if !v.contains(&a) { v.push(a); } }).or_insert_with(|| vec![animal.to_owned()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut copy = animals.clone(); copy.sort_unstable(); copy } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flat_map(|v| v.iter()).cloned().collect(); animals.sort_unstable(); animals}
querywatson
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals }else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::<String>::new(); for (_, animals) in registry.iter() { all_animals.append(&mut animals.clone()); } all_animals.sort(); all_animals}
querywatson
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals }else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::<String>::new(); for (_, animals) in registry.iter() { all_animals.append(&mut animals.clone()); } all_animals.sort(); all_animals}
querywatson
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let animals = registry.entry(section.to_string()).or_insert(Vec::<String>::new()); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals }else { Vec::<String>::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::<String>::new(); for (_, animals) in registry.iter() { all_animals.append(&mut animals.clone()); } all_animals.sort(); all_animals}
paulosuzart
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { registry .entry(section.to_string()) .and_modify(|f| { if !f.contains(&animal.to_string()) { f.push(animal.to_string()) } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { println!("{:?}", registry); if let Some(v) = registry.get(section) { let mut found_section = v.clone(); found_section.sort(); found_section } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut output = Vec::new(); for (_, animals) in registry.iter() { output.append(animals.clone().as_mut()); } output.sort(); output}
badagent
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let entry = registry.entry(section.to_string()).or_insert(vec![]); if !entry.contains(&animal.to_string()) { entry.push(String::from(animal.to_string())) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut result = match registry.get(section) { Some(list) => list.clone(), None => vec![] }; result.sort(); result}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result:Vec<String> = vec![]; for section in registry.values() { for animal in section { if !result.contains(&animal.to_string()) { result.push(animal.to_string()); } } } result.sort(); result // TODO: implement this function}
alemack17
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function let vv=registry.entry(section.to_string()).or_insert(vec![]); if !vv.contains(&animal.to_string()){ vv.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vect; if let Some(vv)=registry.get(section){ vect=vv.clone(); vect.sort(); }else{ vect=vec![]; } return vect;}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vect=vec![]; for s in registry.keys(){ vect.extend(registry[s].clone()); } vect.sort(); return vect;}