Skip to content

[Word Count] Adding Approach Docs - #4352

Draft
Yrahcaz7 wants to merge 9 commits into
exercism:mainfrom
Yrahcaz7:word-count-approaches
Draft

Yrahcaz7 wants to merge 9 commits into
exercism:mainfrom
Yrahcaz7:word-count-approaches

Conversation

@Yrahcaz7

@Yrahcaz7 Yrahcaz7 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

A revival of PR #3371.

Current changes:

  • Formatting, spelling, and grammar fixes
  • Added links to the Python docs and a couple other relevant resources
  • Added a detailed explanation of the "filter() with collections.Counter()" approach

Left to do:

  • Some approaches still need more detail, especially "String Iteration with dict.get() or dict.setdefault()".
  • The performance article has not been started yet.

Yrahcaz7 and others added 4 commits December 3, 2026 19:47
Co-Authored-By: BethanyG <BethanyG@users.noreply.github.com>
also update contributor information in `config.json`

for word in words:
word = word.strip(IGNORE)
if word:

@BethanyG BethanyG Dec 4, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

..I sorta don't want t explain the difference here between setdefault() and get() in prose here. And then again, I sorta do. Especially since the "intro snippet" uses dict.get() (and maybe shouldn't).
Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it's a good idea to explain the difference here. When I was newer to Python, the difference between them was rather confusing.

I think one way of setting it up could be to show how the following two snippets would both achieve the same effect but in slightly different ways:

word_list[new_word] = word_list.get(new_word, 0) + 1
word_list.setdefault(new_word, 0)
word_list[new_word] += 1

@BethanyG BethanyG Dec 5, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like that. We might also want to point out in prose how each is evaluated, I think. get() is slow, but really clear. More info in this piece of the noted SO post: https://stackoverflow.com/a/22326645. And an if/else construct as well. Dunno if it is worth doing the long-hand equivalent, or just covering it in its own approach:

 word_list[word] + 1 if word in word_list else word_list[word] = 1

There is also using defaultdict.

```

This approach avoids splitting the phrase into a word list by iterating over each _character_ in the sentence to determine if it belongs in a valid word (_only letters and numbers are allowed_).
As each valid word is built up, it is added to the dictionary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure how to phrase it, but we probably need to acknowledge that this strategy is ASCII dependent. The minute you move to a different locale in Unicode, you have to account for diacritical marks, and where they belong in a "word" (and what a "word" is in different languages).... and Python doesn't have a built-in module for that. So a 3rd party lib is needed (or a lot of processing code). Other strategies here are more flexible, since the punctuation and split() behaviors vary by the language locale setting.


## Which approach to use?

The most performant of these approaches is __ for longer text, and __ for smaller text. Memory-wise, ___ has the least overhead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Guess we need timings or opinions here. 😄

@BethanyG

BethanyG commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

@Yrahcaz7 - not a really serious, in-depth review - mostly just stream-of-consciousness comments as I read through. I do like your filter() write up. 😄 Don't have time right at the moment to get in there and write prose - but happy to do some more later today. Thank you for your work on this!

Yrahcaz7 and others added 5 commits December 4, 2026 12:17
Co-authored-by: BethanyG <BethanyG@users.noreply.github.com>
also clarify "String Replace with Dictionary" -> "String Replace with Mutable Dictionary"
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