Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add word counter exercise to std-types section
  • Loading branch information
InboraStudio committed Apr 21, 2025
commit 7fe151479eaa0089ab1ffadaab6097f70bb3dc58
1 change: 0 additions & 1 deletion src/std-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"
publish = false

[dependencies]
clap = { version = "4.4", features = ["derive"] }

[[bin]]
name = "hashset"
Expand Down
60 changes: 12 additions & 48 deletions src/std-types/word_counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ minutes: 20

# Exercise: Word Counter

Create a program that counts the frequency of words in a given text. The program should:
Create a program that counts the frequency of words in a given text. The program
should:

1. Take a string of text as input
2. Split the text into words (consider words to be separated by whitespace)
Expand All @@ -16,14 +17,11 @@ Use a `HashMap` to store the word counts.
## Example

```rust
fn main() {
let text = "the quick brown fox jumps over the lazy dog";
let counts = count_words(text);
print_word_counts(&counts);
}
{{#include word_counter.rs:main}}
```

Expected output:

```
brown: 1
dog: 1
Expand All @@ -37,9 +35,12 @@ the: 2

## Tasks

1. Implement the `count_words` function that takes a string slice and returns a `HashMap<String, u32>`
2. Make the word counting case-insensitive (e.g., "The" and "the" count as the same word)
3. Implement the `print_word_counts` function that prints the word counts in alphabetical order
1. Implement the `count_words` function that takes a string slice and returns a
`HashMap<String, u32>`
2. Make the word counting case-insensitive (e.g., "The" and "the" count as the
same word)
3. Implement the `print_word_counts` function that prints the word counts in
alphabetical order
4. Add tests for:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't talked about testing yet. Most exercises focus on getting the task solved, rather than testing.

- Empty input
- Simple text with repeated words
Expand All @@ -51,43 +52,6 @@ the: 2
2. Add statistics like total words, unique words, and average word length
3. Find and display the most common words

[View solution](word_counter_solution.md)

```rust,editable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use anchors to include the non-solution parts of the code.

use std::collections::HashMap;

/// WordCounter counts the frequency of words in text.
struct WordCounter {
word_counts: HashMap<String, usize>,
}

impl WordCounter {
/// Create a new WordCounter.
fn new() -> Self {
todo!("Initialize the WordCounter")
}

/// Count words in the given text.
fn count_words(&mut self, text: &str) {
todo!("Implement word counting logic")
}

/// Get the count for a specific word.
fn word_count(&self, word: &str) -> usize {
todo!("Return the count for the given word")
}

/// Find the most frequent word(s) and their count.
fn most_frequent(&self) -> Vec<(&str, usize)> {
todo!("Find and return the most frequent word(s)")
}
}

#[test]
fn test_word_counter() {
let mut counter = WordCounter::new();
counter.count_words("Hello world, hello Rust!");
assert_eq!(counter.word_count("hello"), 2);
assert_eq!(counter.word_count("rust"), 1);
assert_eq!(counter.word_count("world"), 1);
}
{{#include word_counter.rs:exercise}}
```
Loading