Browse Source

feat(simple_hash): refactor completely the exercise, work in progress

pull/1541/head
mikysett 2 years ago committed by Michele
parent
commit
6e969f8b3f
  1. 51
      subjects/simple_hash/README.md

51
subjects/simple_hash/README.md

@ -2,18 +2,18 @@
### Instructions ### 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 ### Expected functions
```rust ```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 ### Usage
@ -25,32 +25,14 @@ use simple_hash::*;
use std::collections::HashMap; use std::collections::HashMap;
fn main() { fn main() {
let mut hash: HashMap<&str, i32> = HashMap::new(); let sentence = "this is a very basic sentence with only few \
hash.insert("Daniel", 122); repetitions. once again this is very basic and \
hash.insert("Ashley", 333); but it should be enough for basic tests".to_string();
hash.insert("Katie", 334); let words = sentence.split(" ").collect::<Vec<&str>>();
hash.insert("Robert", 14);
let frequency_count = word_frequency_counter(words);
println!( println!("{:?}", frequency_count);
"Does the HashMap contains the name Roman? => {}", println!("{}", nb_distinct_words(&frequency_count));
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);
} }
``` ```
@ -58,11 +40,8 @@ And its output
```console ```console
$ cargo run $ cargo run
Does the HashMap contains the name Roman? => false {"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}
Does the HashMap contains the name Katie? => true 20
Removing Robert ()
Does the HashMap contains the name Robert? => false
Hash {"Katie": 334, "Daniel": 122, "Ashley": 333}
$ $
``` ```

Loading…
Cancel
Save