1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-08 18:49:07 +01:00
CodeWars/6kyu/help_the_bookseller/solution.rs
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

35 lines
899 B
Rust

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(" - ")
}