From 6578d8f7f3cbc6b9c28aa51916ee5f613479b670 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Sun, 26 Nov 2023 17:25:34 +0800 Subject: [PATCH] cii: Add python version of word count --- cii/tests/word_count.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100755 cii/tests/word_count.py diff --git a/cii/tests/word_count.py b/cii/tests/word_count.py new file mode 100755 index 00000000..eba6690a --- /dev/null +++ b/cii/tests/word_count.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import os +import sys + + +def main(): + filename = sys.argv[1] + words = dict() + with open(filename) as fh: + for line in fh.readlines(): + for word in line.split(): + # if not word[-1].isalpha(): + # word = word[:-1] + count = words.get(word, 0) + words[word] = count + 1 + + for item in sorted(words.keys()): + print(item, "occurs", words[item], "times") + + +if __name__ == "__main__": + main() \ No newline at end of file