5.1 A function for 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 setupThe 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__.pyThe 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.pyThe patch makes the file tests/test_top.py, with its one test.
uv run python -m unittest discover -s tests -qThe four tests pass.
Check your files against this move. The command prints nothing when they match, and otherwise shows what differs:
git check 72a0129dcee7If 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]
- look at
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)])