Skip to content

Commit

Permalink
Added character count solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Jul 28, 2023
1 parent 7bded63 commit 316606f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ other languages.
- Buying a Car (https://www.codewars.com/kata/554a44516729e4d80b000012)
- Spinning Words (https://www.codewars.com/kata/5264d2b162488dc400000001)
- Camel Case (https://www.codewars.com/kata/517abf86da9663f1d2000003)
- Count Characters (https://www.codewars.com/kata/52efefcbcdf57161d4000091)

#### Kata 7

Expand Down
26 changes: 26 additions & 0 deletions src/codewars/count_characters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::collections::HashMap;

fn count(input: &str) -> HashMap<char, i32> {
input.chars().fold(HashMap::new(), |mut acc, curr| {
*acc.entry(curr).or_insert(0) += 1;
acc
})
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::codewars::count_characters::count;

#[test]
fn test_count() {
let actual = count("abcabc");

let expected = HashMap::from([
('a', 2),
('b', 2),
('c', 2)
]);
assert_eq!(actual, expected)
}
}
1 change: 1 addition & 0 deletions src/codewars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ mod cats_and_shelves;
mod people_on_the_bus;
mod summation;
mod not_secure;
mod count_characters;

0 comments on commit 316606f

Please sign in to comment.