diff --git a/wizard-work/spark/word_counter_artifact/test_verify.txt b/wizard-work/spark/word_counter_artifact/test_verify.txt new file mode 100644 index 0000000..2f4aba8 --- /dev/null +++ b/wizard-work/spark/word_counter_artifact/test_verify.txt @@ -0,0 +1,2 @@ +The quick brown fox +jumps over the lazy dog. \ No newline at end of file diff --git a/wizard-work/spark/word_counter_artifact/test_verify_trailing.txt b/wizard-work/spark/word_counter_artifact/test_verify_trailing.txt new file mode 100644 index 0000000..2f4aba8 --- /dev/null +++ b/wizard-work/spark/word_counter_artifact/test_verify_trailing.txt @@ -0,0 +1,2 @@ +The quick brown fox +jumps over the lazy dog. \ No newline at end of file diff --git a/wizard-work/spark/word_counter_artifact/word_counter.py b/wizard-work/spark/word_counter_artifact/word_counter.py new file mode 100644 index 0000000..812dd17 --- /dev/null +++ b/wizard-work/spark/word_counter_artifact/word_counter.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +"""Word counter utility - counts words, lines, and characters in a file or from stdin.""" + +import sys +from pathlib import Path + + +def count_words(text: str) -> int: + """Count words in text (split on whitespace).""" + return len(text.split()) + + +def count_lines(text: str) -> int: + """Count lines in text.""" + return len(text.splitlines()) + + +def count_chars(text: str) -> int: + """Count characters in text.""" + return len(text) + + +def main(): + if len(sys.argv) > 1: + # Count words in file + path = Path(sys.argv[1]) + if not path.exists(): + print(f"Error: File not found: {sys.argv[1]}", file=sys.stderr) + sys.exit(1) + text = path.read_text() + lines = count_lines(text) + words = count_words(text) + chars = count_chars(text) + print(f"{lines} {words} {chars} {sys.argv[1]}") + else: + # Count words from stdin + text = sys.stdin.read() + print(count_words(text)) + + +if __name__ == "__main__": + main() \ No newline at end of file