Word Count Tool

Lab Python Intermediate 20 points

Read lines until EOF and save them to input.txt. Then, reading the file, print the number of lines, words and characters (excluding newlines), like the wc tool.

Input

Lines of text, then EOF.

Output

lines words chars

Example

Input

hello world
the quick brown fox

EOF

Output

3 6 30
Sample input available Sample output available
Editor
Python

Hints

Process the file line by line instead of reading it all at once.

Solution explanation

Iterating over a file streams it, so even huge files use little memory.

Show solution
with open("input.txt", "w") as f:
    while (line := input()) != "EOF":
        f.write(line + "\n")

lines = words = chars = 0
with open("input.txt") as f:
    for line in f:
        line = line.rstrip("\n")
        lines += 1
        words += len(line.split())
        chars += len(line)
print(lines, words, chars)
Output
Answer
HOME LEARN COMMUNITY DASHBOARD