Skip to content

Refactor word_occurrence to use dict - #15186

Closed
Miladkhoshdel wants to merge 2 commits into
TheAlgorithms:masterfrom
Miladkhoshdel:refactor/word-occurrence
Closed

Refactor word_occurrence to use dict#15186
Miladkhoshdel wants to merge 2 commits into
TheAlgorithms:masterfrom
Miladkhoshdel:refactor/word-occurrence

Conversation

@Miladkhoshdel

Copy link
Copy Markdown
Contributor

Describe your change:

Refactor word_occurrence() to use a standard dict instead of defaultdict.

This change:

  • Uses dict.get() to count word occurrences.
  • Updates the return type annotation from dict to dict[str, int].
  • Removes the unnecessary defaultdict dependency.
  • Preserves the existing behavior and doctests.
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeperalgorithms-keeperBot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Sep 4, 2026
@Miladkhoshdel
Miladkhoshdelforce-pushed the refactor/word-occurrence branch from e4e1612 to 2538821CompareSeptember 4, 2026 18:28
@Miladkhoshdel
Miladkhoshdelforce-pushed the refactor/word-occurrence branch from a673a04 to 5bccc57CompareSeptember 4, 2026 18:31

for word in sentence.split():
occurrence[word] += 1
occurrence[word] = occurrence.get(word, 0) + 1

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.

Python's default_dict is in the standard library to accelerate use cases like this one. There is a function call overhead in the proposed solution that does not exist in the current solution.

Create a https://docs.python.org/3/library/timeit.html#timeit.timeit benchmark and compare the two on a big dataset.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Thanks for the feedback. I’ve kept the existing defaultdict implementation unchanged and moved the type-hint and typo fixes into a separate, focused PR: #15194.

@cclausscclauss closed this Sep 4, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviewsThis PR is ready to be reviewedenhancementThis PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@Miladkhoshdel@cclauss