1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-19 22:16:57 +02:00
CodeWars/6kyu/help_the_bookseller/solution.rs

36 lines
899 B
Rust
Raw Normal View History

use std::collections::HashMap;
fn stock_list(list_art: Vec<&str>, list_cat: Vec<&str>) -> String {
if list_art.is_empty() || list_cat.is_empty() {
return "".to_string();
}
let mut map: HashMap<&str, i32> = HashMap::new();
// Initialize with zero values
for cat in &list_cat {
map.insert(cat, 0);
}
// Iterate through art
for art in &list_art {
for cat in &list_cat {
if art.starts_with(cat) {
let v: Vec<&str> = art.split_terminator(' ').collect();
let count: i32 = v[1].parse().unwrap();
map.insert(cat, map[cat] + count);
}
}
}
// Get result string
list_cat
.iter()
.map(
|category| format!("({} : {})", category, map[category])
)
.collect::<Vec<String>>()
.as_slice()
.join(" - ")
}