From 83fd2284de5178e21690417f959b19bd4393ca8d Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 31 Oct 2026 08:51:50 +0000 Subject: [PATCH] spark: Fibonacci calculator with verification tests --- .../word_counter_artifact/test_verify.txt | 2 + .../test_verify_trailing.txt | 2 + .../word_counter_artifact/word_counter.py | 42 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 wizard-work/spark/word_counter_artifact/test_verify.txt create mode 100644 wizard-work/spark/word_counter_artifact/test_verify_trailing.txt create mode 100644 wizard-work/spark/word_counter_artifact/word_counter.py 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