digit_loops/
lib.rs

1use std::collections::HashMap;
2
3fn number_of_loops_dict() -> HashMap<usize, i8> {
4    return HashMap::from([
5        (0, 1),
6        (1, 0),
7        (2, 0),
8        (3, 0),
9        (4, 1),
10        (5, 0),
11        (6, 1),
12        (7, 0),
13        (8, 2),
14        (9, 1),
15    ]);
16}
17
18pub fn number_of_loops_in_number(number: &str) -> i8 {
19    let mut sum_of_loops: i8 = 0;
20    number
21        .split("")
22        .into_iter()
23        .collect::<Vec<&str>>()
24        .iter()
25        .filter(|digit_or_whitespace| digit_or_whitespace.len() > 0)
26        .for_each(|digit| {
27            sum_of_loops = sum_of_loops
28                + number_of_loops_dict()
29                    .get(&(*digit).to_string().parse::<usize>().unwrap())
30                    .unwrap();
31        });
32    sum_of_loops
33}
34
35#[cfg(test)]
36mod test;