spark: Fibonacci calculator with verification tests

This commit is contained in:
spark committed 2026-10-31 08:51:50 +00:00
1 parent 40b98f179e
commit 83fd2284de
3 files changed
+46

No files matched your search

@@ -0,0 +1,2 @@
The quick brown fox
jumps over the lazy dog.
@@ -0,0 +1,2 @@
The quick brown fox
jumps over the lazy dog.
@@ -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()