-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add Word Counter Exercise with Solution #2717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
InboraStudio
wants to merge
2
commits into
google:main
Choose a base branch
from
InboraStudio:word-counter-exercise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
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
commit 7fe151479eaa0089ab1ffadaab6097f70bb3dc58
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
@@ -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 | ||
|
|
@@ -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: | ||
| - Empty input | ||
| - Simple text with repeated words | ||
|
|
@@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}} | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.