Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

InboraStudio
Copy link

This pull request adds a new word counter exercise to the comprehensive-rust course. The exercise includes:

  • Implementation of a word counter in src/std-types/word_counter.rs
  • Solution with detailed comments
  • Test cases demonstrating:
    • Basic word counting
    • Case-insensitive counting
    • Handling of multiple spaces and tabs
    • Error handling for empty input
  • Documentation in word_counter_solution.md

The exercise helps learners understand:

  • Working with HashMaps
  • String manipulation
  • Error handling
  • Basic text processing

This is my first contribution to the comprehensive-rust course. I'm open to any feedback or suggestions for improvement.

Copy link

google-cla bot commented Apr 18, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@InboraStudio InboraStudio force-pushed the word-counter-exercise branch 3 times, most recently from d15ddef to c592d08 Compare April 19, 2025 02:02
@InboraStudio InboraStudio force-pushed the word-counter-exercise branch from c592d08 to 5211436 Compare April 19, 2025 03:25
@InboraStudio InboraStudio reopened this Apr 19, 2025
Copy link
Contributor

@djmitche djmitche left a comment

Choose a reason for hiding this comment

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

Lots of comments below, but before addressing those..

Is there some context for what you're trying to accomplish here? The course typically has one exercise per segment, but this adds a second for this segment. Are you suggesting replacing the existing exercise? Why? What pedagogical advantage does the new exercise have over the existing exercise? Have you tried this exercise with students?

All of these questions might have answers, and we might adopt the exercise, but they will need to be discussed before we can do so.


[View solution](word_counter_solution.md)

```rust,editable
Copy link
Contributor

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.

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
Contributor

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.

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

We typically don't include a link, since it is just the next slide.

assert_eq!(counter.word_count("hello"), 2);
assert_eq!(counter.word_count("rust"), 1);
assert_eq!(counter.word_count("world"), 1);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing closing triple-backtick

@@ -0,0 +1,140 @@
// ANCHOR: word_counter
use std::collections::HashMap;
use clap::Parser;
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't talk about clap in fundamentals. And, command-line interface is sort of out-of-scope for this exercise, anyway -- the assignment relates to counting words, and the segment is about types in std. This should probably just take a static string as input.

Copy link
Contributor

Choose a reason for hiding this comment

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

This file should not be included (I'm not even sure what creates it?)

Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely do not check in the windows executable!

@@ -0,0 +1,110 @@
use std::collections::HashMap;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is Rust, not Markdown.

Copy link
Contributor

Choose a reason for hiding this comment

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

Again, please do not add binary files

Copy link
Contributor

Choose a reason for hiding this comment

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

This appears to be a fibonacci implementation encoded in UTF-16?!?

@InboraStudio
Copy link
Author

Sir please now cheak it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants