5.1 A test of the most common words

Build it yourself. Read the tutorial is the other way to read this part.

This part does not start where the part before it ends, because the walk skips some changes. This command puts the files that this part starts from in place of yours. Run it, and then go on:

git restore --source bfcbe07463b5 --staged -- :/ && git restore --source f79588d3b046 --staged --worktree -- :/

If you start at this part, make your clone as Get the project says, and then run this command. It puts in your clone the files that this part starts from. A file that the project deleted before this part stays, so delete it if git check lists it:

git rm -rq --cached --ignore-unmatch -- :/ && git restore --source f79588d3b046 --staged --worktree -- :/
just setup

Since the last part, the walk leaves out ruff, the corpus, the types and the docstring. None of them changes a test. This part begins with their code.

The code before this part. The code after it.

5.1 A function for the most common words

count gives the counts in no useful order. To find the most common words, tally must sort the counts, most common first, and keep the first few. A function, top, does it. It sorts by count, highest first, and then by the word, so that two words with the same count keep one order. Then it keeps the first n.

Apply the patch to src/tally/__init__.py, from the top of the project:

curl -fsSL https://github.com/rahuldave/tally/commit/72a0129dcee765132ad267788636ff4af9066369.diff | git apply --include=src/tally/__init__.py

The patch puts top after count and before main, with two blank lines around it.

A test says what top must give. It gives top three words with their counts, and asks for the two most common.

Apply the patch to tests/test_top.py, from the top of the project:

curl -fsSL https://github.com/rahuldave/tally/commit/72a0129dcee765132ad267788636ff4af9066369.diff | git apply --include=tests/test_top.py

The patch makes the file tests/test_top.py, with its one test.

uv run python -m unittest discover -s tests -q

The four tests pass.

The test of top has the same form as the three tests of count. The walk stops here, before the option --top and the report.

Check your files against this move. The command prints nothing when they match, and otherwise shows what differs:

git check 72a0129dcee7

If you are stuck, this command puts the files of this move in place of yours. A file that you made yourself stays:

git restore --source f79588d3b046 --staged -- :/ && git restore --source 72a0129dcee7 --staged --worktree -- :/

What changed

  • src/tally/__init__.py: sort by count, most common first, then by the word. 5 lines added. The file.
    • look at return sorted(counts.items(), key=lambda pair: (-pair[1], pair[0]))[:n]
  • tests/test_top.py: its test, with three words. New, 8 lines. The file.
The patch to src/tally/__init__.py
@@ -11,6 +11,11 @@ def count(text: str) -> dict[str, int]:
     return counts
 
 
+def top(counts: dict[str, int], n: int) -> list[tuple[str, int]]:
+    "The n most common words, most common first."
+    return sorted(counts.items(), key=lambda pair: (-pair[1], pair[0]))[:n]
+
+
 def main() -> None:
     text = open(sys.argv[1]).read() if len(sys.argv) > 1 else sys.stdin.read()
     for word, n in sorted(count(text).items()):
The patch to tests/test_top.py
@@ -0,0 +1,8 @@
+import unittest
+
+from tally import top
+
+
+class Top(unittest.TestCase):
+    def test_most_common_first(self):
+        self.assertEqual(top({"a": 1, "b": 3, "c": 2}, 2), [("b", 3), ("c", 2)])

This change on GitHub. The code after it.