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.
let mut registry = Collection::new();
add_animal_to_section("Eagle", "Birds", &mut registry);
assert_eq!(get_animals_in_section("Birds", ®istry), vec!["Eagle"]);
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.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 vec = registry.entry(section.to_string()).or_insert(vec![]); if !vec.iter().any(|an| an == animal) { vec.push(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).cloned().unwrap_or_else(Vec::new); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals = Vec::new(); for animals in registry.values() { animals.into_iter().for_each(|anim| all_animals.push(anim.clone())); } all_animals.sort(); all_animals}
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(|sectionvec: &mut Vec<String>| { if sectionvec.contains(&animal.to_string()){} else { sectionvec.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(v) => {let mut vp = v.clone();vp.sort();vp}, // ㅠㅠ None => Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut v:Vec<String> = Vec::new(); for (_, ve) in registry.iter(){ for vee in ve.iter(){ v.push(vee.clone()) } } v.sort(); v}
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.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}
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()) .or_insert_with(Vec::new) .iter() .find(|a| *a == animal) .is_none() .then(|| registry.get_mut(section).unwrap().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_else(Vec::new); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // Collect all animals into a vector let mut all_animals: Vec<String> = registry.values() .flat_map(|animals| animals.iter().cloned()) .collect(); all_animals.sort(); all_animals}pub fn main(){ return ()}
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(|a| a.push(animal.to_string())) .and_modify(|a| a.dedup()) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section){ Some(x) => { let mut animals = x.to_vec(); animals.sort(); animals }, None => [].to_vec(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut results: Vec<String> = Vec::new(); for section in registry.keys(){ if let Some(animals) = registry.get(section) { for animal in animals { results.push(animal.to_string()); } } } results.sort(); results}
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().any(|a| a == animal) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(vec) => { let mut cloned = vec.clone(); // clone once cloned.sort(); // sort in place cloned } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut values: Vec<String> =registry.values() .flat_map(|arr| arr.iter().cloned()) .collect(); values.sort(); values}
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().any(|a| a == animal) { animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(vec) => { let mut cloned = vec.clone(); // clone once cloned.sort(); // sort in place cloned } None => Vec::new(), }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut values: Vec<String> =registry.values() .flat_map(|arr| arr.iter().cloned()) .collect(); values.sort(); values}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;// 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.pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { //key - section(Arctic, Savannah) //value - vector of animals let animals = registry.entry(section.to_string()).or_insert_with(Vec::new); if !animals.contains(&animal.to_string()){ animals.push(animal.to_string()); }}// 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.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 animals_list = animals.clone(); animals_list.sort(); animals_list } else{ Vec::new() }}// This function should return a copy of the entire registry with all animals sorted alphabetically in each section.pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals: Vec<String> = registry .values() .flat_map(|v| v.clone()) .collect(); animals.sort(); animals }
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;/// Adds an animal to a section. If the section doesn't exist, it is created./// If the animal already exists in the section, it is not added again.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()); }}/// Returns a list of animals sorted alphabetically in a given section./// Returns an empty list if the section does not exist.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() }}/// Returns a flat list of all animals across all sections,/// sorted alphabetically.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}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { if let Some(v) = registry.get_mut(section) { if !v.contains(&animal.to_string()) { v.push(animal.to_string()); } } else { registry.insert(section.to_string(), vec![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 vals: Vec<String> = registry.values().flatten().cloned().collect(); vals.sort(); vals}
use std::collections::HashMap;use std::collections::HashSet;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let vec = registry.entry(section.to_string()).or_insert(Vec::new()); let tmp = animal.to_string(); if !vec.contains(&tmp){ vec.push(tmp); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(e) = registry.get(section) { let mut tmp = e.clone(); tmp.sort(); tmp } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res = HashSet::new(); for (_, vec) in registry.iter() { for animal in vec.iter(){ res.insert(animal.clone()); } } let mut res: Vec<_> = res.drain().collect(); res.sort(); res}
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 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> { // TODO: implement this function match registry.get(section){ Some(animals)=>{ let mut animals = animals.clone(); animals.sort(); animals }, None => Vec::new(), }}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 }
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { if let Some(animals) = registry.get_mut(section) { for animal_in_list in animals.iter() { if animal == animal_in_list { return; } } animals.push(animal.to_string()); } else { registry.insert(section.to_string(), vec![animal.to_string()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(animals) = registry.get(section) { let mut my_animals = animals.clone(); let _ = my_animals.sort(); return my_animals.clone(); } else { return Vec::new(); }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut my_animals = Vec::new(); for (_, values) in registry.iter() { for value in values { my_animals.push(value.clone()); } } let _ = my_animals.sort(); my_animals.clone()}
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_in_section = registry.entry(section.to_string()).or_insert(Vec::new()); if !animals_in_section.iter().any(|a| a == animal) { animals_in_section.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(§ion.to_string()) { Some(array) => { let mut sorted_array = array.clone(); sorted_array.sort(); sorted_array }, None => Vec::new() } }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::new(); for animals in registry.values() { for animal in animals { all_animals.push(animal.clone()); } } all_animals.sort(); all_animals}
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 animals = registry.entry(section.to_string()).or_insert_with(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> { // TODO: implement this function 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> { // TODO: implement this function let mut all_animals: Vec<String> = Vec::new(); for animals in registry.values() { for animal in animals { all_animals.push(animal.clone()); } } all_animals.sort(); all_animals}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let sect = registry.entry(section.to_string()).or_insert(Vec::new()); if !sect.contains(&animal.to_string()) { sect.push(animal.to_string()); } return}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals = registry.get(§ion.to_string()).clone().unwrap_or(&Vec::<String>::new()).to_vec(); animals.sort(); animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::new(); for section in registry.values() { all_animals.extend_from_slice(section); } all_animals.sort(); all_animals}
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 = get_animals_in_section(section, registry); if registry.contains_key(section){ if registry.get(section).unwrap().contains(&animal.to_string()) { return } } animals.push(animal.to_string()); registry.insert(section.to_string(), animals); }pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(value) = registry.get(section) { let mut out = value.clone(); out.sort(); return out.to_vec() } else { return Vec::from([]) } }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut animals = Vec::new(); for key in registry.keys() { animals.extend(get_animals_in_section(key, registry)); } animals.sort(); return animals;}
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 found = false; let mut f = false; for (key, val) in &mut *registry { if key == section { found = true; for v in val.iter() { if v == animal { f = true; } } if f == false { val.push(animal.to_string()); } } } if found == false { registry.entry(section.to_string()).or_insert(Vec::new()).push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut ans = Vec::new(); if let Some(x) = registry.get(section) { for val in x.iter() { ans.push(val.clone()); } } ans.sort(); ans }pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut ans = Vec::new(); for (key, val) in registry.iter() { for v in val.iter() { ans.push(v.clone()); } } ans.sort(); ans}
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_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> { // TODO: implement this function 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> { // TODO: implement this function let mut all_animals = Vec::new(); for values in registry.values(){ all_animals.extend(values.clone()); } all_animals.sort(); all_animals}
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 regis = registry.entry(section.to_string()).or_insert_with(Vec::new); if !regis.contains(&animal.to_string()) { regis.push(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 copied = animals.to_vec(); copied.sort(); copied } else { vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut copied = registry.clone(); let mut animal = copied .values() .flat_map(|animal| animal.clone()) .collect::<Vec<String>>(); animal.sort(); animal}
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_animals = registry.entry(section.to_string()).or_insert_with(Vec::new); if !section_animals.contains(&animal.to_string()) { section_animals.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(list)=registry.get(section){ let mut new_list : Vec<String>=list.clone(); new_list.sort(); return new_list; } vec![]}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vals: Vec<String> = registry.values().flatten().cloned().collect(); vals.sort(); vals}
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 animals = registry.entry(section.to_string()).or_default(); if !animals.iter().any(|a| a == animal){ animals.push(animal.to_string()) } // get no mod // entry mod}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 sorted = animals.to_vec(); sorted.sort(); sorted } else { Vec::new() }}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}
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::new()); if !entry.contains(&animal.to_string()){ entry.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::new(); for animals in registry.values(){ all_animals.extend(animals.clone()); } all_animals.sort(); all_animals}
use std::collections::HashMap;// Key = Section Name, Value = List of animals in sectiontype 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(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert(vec![animal.to_string()]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry.get(section) .map_or(vec![], |animals| { let mut sorted_animals = animals.clone(); sorted_animals.sort(); sorted_animals.to_vec() })}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = vec![]; for (_section, animals) in registry.iter() { all_animals.extend_from_slice(animals); } all_animals.sort(); all_animals}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let vec = registry.entry(section.to_string()).or_insert_with(Vec::new); if !vec.contains(&animal.to_string()) { vec.push(animal.to_string()) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { return registry.get(section) .map(|animals| { let mut sorted = animals.clone(); sorted.sort(); return sorted }) .unwrap_or_else(Vec::new);}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut sections: Vec<String> = registry.values().flatten().cloned().collect(); sections.sort(); return sections;}
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 animals = registry.entry(section.to_string()).or_default(); if !animals.iter().any(|a| a == animal){ animals.push(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 sorted: Vec<String> = animals.to_vec(); sorted.sort(); sorted } else{ Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut list: Vec<String> = registry.values().flatten().cloned().collect(); list.sort(); list}
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.contains(&animal.to_string()) { animals.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 registry.get(section) .map(|animals| { let mut sorted = animals.clone(); sorted.sort(); sorted }) .unwrap_or_else(Vec::new)}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(); return animals;}
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(|animals| { if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } }) .or_insert_with(|| vec![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.to_vec(); sorted.sort(); return sorted } return Vec::new();}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut sections: Vec<String> = registry.values() .flatten() .cloned() .collect(); sections.sort(); return sections;}
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_value = animal.to_string(); registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal_value) { animals.push(animal_value.clone()); } }) .or_insert(vec![animal_value]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = Vec::new(); if let Some(value) = registry.get(section) { animals = value.to_vec(); animals.sort(); } animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // registry // .iter() // .map(|(_, section_animals)| { // section_animals // .iter() // .map(|animal| animal.clone()) // .collect() // }) // .collect() let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); animals}
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_value = animal.to_string(); registry .entry(section.to_string()) .and_modify(|animals| { if !animals.contains(&animal_value) { animals.push(animal_value.clone()); } }) .or_insert(vec![animal_value]);}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = Vec::new(); if let Some(value) = registry.get(section) { animals = value.to_vec(); animals.sort(); } animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // registry // .iter() // .map(|(_, section_animals)| { // section_animals // .iter() // .map(|animal| animal.clone()) // .collect() // }) // .collect() let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); animals}
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.iter().any(|s| s == animal) { v.push(animal.to_string()) }) .or_insert_with(|| vec![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().flat_map(|v| v.iter().cloned()).collect(); animals.sort(); animals}
use std::collections::HashMap;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_with(Vec::new); if !list.contains(&animal.to_string()) { list.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(sec) => {let mut d: Vec<String> = sec.clone(); d.sort(); d} None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut vals: Vec<String> = registry.values().flatten().cloned().collect(); vals.sort(); vals}
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 if let Some(val)=registry.get_mut(section){ if val.contains(&animal.to_string()){ return }else{ val.push(animal.to_string()); } }else{ 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 if let Some(vec) = registry.get(section){ let mut new_vec=vec.clone(); new_vec.sort(); return new_vec } else{ return vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals=vec![]; for animals in registry.values(){ let mut new = animals.clone(); new.sort(); for x in new { all_animals.push(x); } } all_animals.sort(); all_animals}// pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) {// // TODO: implement this function// if let Some(val)=registry.get_mut(section){// if val.contains(&animal.to_string()){// return// }else{// val.push(animal.to_string());// }// }else{// 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// if let Some(val)=registry.get(section){// let mut new_val=val.clone();// new_val.sort();// return new_val// }else{// return vec![]// } // } // pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> {// // TODO: implement this function// let mut all_animals=vec![];// for animals in registry.values(){// let mut new = animals.clone();// new.sort();// for x in new {// all_animals.push(x);// }// }// all_animals.sort();// all_animals// }
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 if let Some(val)=registry.get_mut(section){ if val.contains(&animal.to_string()){ return }else{ val.push(animal.to_string()); } }else{ 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 if let Some(val)=registry.get(section){ let mut new_val=val.clone(); new_val.sort(); return new_val }else{ return vec![] } } pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all_animals=vec![]; for animals in registry.values(){ let mut new = animals.clone(); new.sort(); for x in new { all_animals.push(x); } } all_animals.sort(); all_animals}
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()).or_insert(vec![animal.to_string()]); if !registry.entry(section.to_string()).or_insert(vec![animal.to_string()]).contains(&animal.to_string()){ registry.entry(section.to_string()).or_insert(vec![animal.to_string()]).push(animal.to_string()); } }pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let mut vector = Vec::new(); if registry.contains_key(section) { vector = registry.get(section).unwrap().clone(); vector.sort(); } return vector;}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut my_vec: Vec<String> = registry.values().cloned().flatten().collect(); my_vec.sort(); my_vec}
use std::collections::HashMap;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_with(Vec::new); if !list.contains(&animal.to_string()) { list.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(sec) => {let mut d: Vec<String> = sec.clone(); d.sort(); d} None => vec![] }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut vals: Vec<String> = registry.values().flatten().cloned().collect(); vals.sort(); vals}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let vector = registry.entry(section.to_string()).or_insert_with(Vec::new); if(!vector.contains(&animal.to_string())) { vector.push(animal.to_string()) }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { let mut vect = registry.get(section).unwrap_or(&Vec::new()).clone(); vect.sort(); vect}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut res: Vec<String> = registry.values().cloned().flatten().collect(); res.sort(); res}
use std::collections::HashMap;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_with(Vec::new); if !list.contains(&animal.to_string()) { list.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(list)=registry.get(section){ let mut new_list : Vec<String>=list.clone(); new_list.sort(); return new_list; } vec![]}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function// let copied_registry:Vec<Vec<String>>=registry.values().cloned().collect();// let mut flattened_registry : Vec<String>=copied_registry.into_iter().flatten().collect();// flattened_registry.sort();// flattened_registry let mut collected_values: Vec<String> = registry .values() .flatten() .cloned() .collect(); collected_values.sort(); collected_values}
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()).or_insert(vec![animal.to_string()]); if !registry.entry(section.to_string()).or_insert(vec![animal.to_string()]).contains(&animal.to_string()){ registry.entry(section.to_string()).or_insert(vec![animal.to_string()]).push(animal.to_string()); } }pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { // TODO: implement this function let my_vec: Vec<String> = Vec::new(); let mut aa = registry.get(section).unwrap_or(&my_vec).to_vec(); aa.sort(); aa}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut my_vec: Vec<String> = registry.values().cloned().flatten().collect(); my_vec.sort(); my_vec}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { if let Some(vec) = registry.get_mut(section) { if !vec.contains(&animal.to_string()) { vec.push(animal.to_string()); } } else { registry.insert(section.to_string(), vec![animal.to_string()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(vec) = registry.get(section) { let mut sorted_vec = vec.clone(); sorted_vec.sort(); return sorted_vec } vec![]}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let values_iterator = registry.values().cloned(); let flattened_iterator = values_iterator.flatten(); let mut all_animals: Vec<String> = flattened_iterator.collect(); all_animals.sort(); all_animals}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { if let Some(vec) = registry.get_mut(section) { if !vec.contains(&animal.to_string()) { vec.push(animal.to_string()); } } else { registry.insert(section.to_string(), vec![animal.to_string()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(vec) = registry.get(section) { let mut sortedVec = vec.clone(); sortedVec.sort(); return sortedVec; } vec![]}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut finalVec = vec![]; for k in registry.keys() { finalVec.extend(get_animals_in_section(k, registry)); } finalVec.sort(); return finalVec;}
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 animals = registry.entry(section.to_string()).or_insert_with(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> { // TODO: implement this function let mut res = Vec::<String>::new(); let animals = registry.get(§ion.to_string()); if let Some(a) = animals { res = a.clone(); res.sort(); } res}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut new_coll = Vec::<String>::new(); for (_, v) in registry.iter() { let mut res = v.clone(); res.sort(); for a in res.into_iter() { if !new_coll.contains(&a){ new_coll.push(a); } } } new_coll.sort(); new_coll}
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 animals = registry.entry(section.to_string()).or_insert_with(Vec::new); // if !animals.contains(&animal.to_string()){ // registry.insert(animal.to_string(),vec![section.to_string()]); // } let animals = registry.entry(section.to_string()).or_insert_with(Vec::new); if !animals.contains(&animal.to_string()) { animals.push(animal.to_string()); } // let animals = registry.entry(section.to_string()).or_insert_with(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> { // TODO: implement this function // registry.get(section).cloned().unwrap_or_default() let mut animals = registry .get(section) .cloned() .unwrap_or_default(); animals.sort(); // sort alphabetically animals}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut all: Vec<String> = registry .iter() // iterate over sections .flat_map(|(_, animals)| animals.iter().cloned()) .collect(); all.sort(); // sort alphabetically all}
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| if !v.contains(&animal.to_owned()) {v.push(animal.to_owned());}) .or_insert(vec!(animal.to_owned()));}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry.get(section) .map(|v| { let mut vector = v.to_vec(); vector.sort(); vector }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut animals: Vec<String> = registry.values().flatten().cloned().collect(); animals.sort(); animals}
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 animals = registry.entry(section.to_string()).or_insert_with(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> { // TODO: implement this function if let Some(animals) = registry.get(section){ let mut animals = animals.clone(); animals.sort(); animals } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut values: Vec<String> = registry.values().flatten().cloned().collect(); values.sort(); return values;}
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 vec = registry.entry(section.to_string()).or_insert(vec![]); if !vec.contains(&animal.to_string()){ vec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { match registry.get(section) { Some(animals) => { let mut sorted = animals.clone(); sorted.sort(); sorted }, None => { Vec::new() } }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut all_animals = Vec::new(); for animals in registry.values() { let mut sorted = animals.clone(); sorted.sort(); all_animals.extend(sorted); } all_animals.sort(); all_animals }
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 animals = registry.entry(section.to_string()) .or_insert_with(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> { // TODO: implement this function match registry.get(section) { Some(animals) => { let mut clone = (animals).clone(); clone.sort(); clone }, None => 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.iter()) .cloned() .collect(); animals.sort(); animals}
use std::collections::HashMap;use std::collections::hash_map::Entry;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { // TODO: implement this function // let curr = registry.entry(section.to_string()); // match curr { // // no existing section exists, so start it // Entry::Vacant(this_entry) => { // registry.insert(section.to_string(), vec![animal.to_string()]); // }, // // existing section so add it to the current list // Entry::Occupied(this_entry) => { // this_entry // }, // }; ///registry.entry(section.to_string()).or_insert(vec![animal.to_string()]); if registry.contains_key(section) { // case that a section exists and is being added to if let Some(x) = registry.get_mut(section) { let mut duplicate = false; // check that the animal doesn't already exist in this section for a in &mut *x { if a == animal { duplicate = true; } } if !duplicate { // add the new animal x.push(animal.to_string()); } } } else { // case for new section being added from scratch 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 if registry.contains_key(section) { let (curr_section, animals) = registry.get_key_value(section).unwrap(); let mut vecout = animals.to_vec(); vecout.sort(); vecout } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { // TODO: implement this function let mut full_list = vec![]; for (sec, animals) in registry.iter() { //let Some(vec) = registry.get(&sec); for elem in animals { full_list.push(elem.to_string()); } } full_list.sort(); full_list}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { let vec = registry.entry(section.to_string()).or_insert(Vec::new()); if !vec.contains(&animal.to_string()) { vec.push(animal.to_string()); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { registry.get(section) .map(|vec| { let mut sorted = vec.clone(); sorted.sort(); sorted }) .unwrap_or_default()}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut result: Vec<String> = registry.values() .flat_map(|vec| vec.iter()) .cloned() .collect(); result.sort(); result}
use std::collections::HashMap;type Collection = HashMap<String, Vec<String>>;pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) { if let Some(val) = registry.get_mut(section) { if val.iter().all(|item| item != animal) { val.push(animal.to_string()) }else{ () } } else { registry.insert(section.to_string(), vec![animal.to_string()]); }}pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> { if let Some(val) = registry.get(section) { let mut clone = val.clone(); clone.sort(); clone } else { Vec::new() }}pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> { let mut acc: Vec<String> = Vec::new(); for (_key, item) in registry.iter() { acc.extend(item.clone()); } acc.sort(); acc}