From 6e969f8b3f5d8f50a27ca0e9917d4945003bcc46 Mon Sep 17 00:00:00 2001 From: mikysett Date: Mon, 10 Oct 2022 18:46:05 +0100 Subject: [PATCH] feat(simple_hash): refactor completely the exercise, work in progress --- subjects/simple_hash/README.md | 51 ++++++++++------------------------ 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/subjects/simple_hash/README.md b/subjects/simple_hash/README.md index a9b0ecd6..9665e35d 100644 --- a/subjects/simple_hash/README.md +++ b/subjects/simple_hash/README.md @@ -2,18 +2,18 @@ ### Instructions -Create a **function** named `contain`, that checks a `HashMap` to see if it contains a given key. +Create a **function** named `word_frequency_counter` which will receive a vector of strings (each string being a single word) and return an `HashMap` with the word as the key and the number of repetitions as the value. -Create a **function** named `remove` that removes a given key from the `HashMap`. +Also create a function named `nb_distinct_words` which will take a reference to the `HashMap` and return the number of distinct words present in it. -> Pay attention to the comment in the [usage](#usage) ### Expected functions ```rust -pub fn contain(h: &HashMap<&str, i32>, s: &str) -> bool {} +pub fn word_frequency_counter(words: Vec<&str>) -> HashMap<&str, usize> {} -pub fn remove(h: &mut HashMap<&str, i32>, s: &str) {} +pub fn nb_distinct_words(frequency_count: &HashMap<&str, usize>) -> usize { +} ``` ### Usage @@ -25,32 +25,14 @@ use simple_hash::*; use std::collections::HashMap; fn main() { - let mut hash: HashMap<&str, i32> = HashMap::new(); - hash.insert("Daniel", 122); - hash.insert("Ashley", 333); - hash.insert("Katie", 334); - hash.insert("Robert", 14); - - println!( - "Does the HashMap contains the name Roman? => {}", - contain(hash.clone(), "Roman") - //----------^^^^^^^^ - // this is not correct, fix it to match the solution the expected function - ); - println!( - "Does the HashMap contains the name Katie? => {}", - contain(hash.clone(), "Katie") - //----------^^^^^^^^ - // this is not correct, fix it to match the solution the expected function - ); - println!("Removing Robert {:?}", remove(hash.clone(), "Robert")); - println!( - "Does the HashMap contains the name Robert? => {}", - contain(hash.clone(), "Robert") - //----------^^^^^^^^ - // this is not correct, fix it to match the solution the expected function - ); - println!("Hash {:?}", &hash); + let sentence = "this is a very basic sentence with only few \ + repetitions. once again this is very basic and \ + but it should be enough for basic tests".to_string(); + let words = sentence.split(" ").collect::>(); + + let frequency_count = word_frequency_counter(words); + println!("{:?}", frequency_count); + println!("{}", nb_distinct_words(&frequency_count)); } ``` @@ -58,11 +40,8 @@ And its output ```console $ cargo run -Does the HashMap contains the name Roman? => false -Does the HashMap contains the name Katie? => true -Removing Robert () -Does the HashMap contains the name Robert? => false -Hash {"Katie": 334, "Daniel": 122, "Ashley": 333} +{"tests": 1, "with": 1, "this": 2, "it": 1, "enough": 1, "is": 2, "but": 1, "sentence": 1, "only": 1, "basic": 3, "again": 1, "for": 1, "be": 1, "once": 1, "very": 2, "should": 1, "few": 1, "and": 1, "a": 1, "repetitions.": 1} +20 $ ```