ccwc (Coding Challenges Word Count) is a command-line tool inspired by the Unix wc command. It can count the number of lines, words, characters, and bytes in a text file. It also supports reading input from standard input.
- Python 3.6+
virtualenv(optional but recommended for creating isolated environments)
-
Clone the repository:
git clone https://github.com/yourusername/ccwc.git cd ccwc -
(Optional) Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
You can run the ccwc command with various options:
python ccwc/ccwc.py [option] <filename>-c: Count bytes in the file.-l: Count lines in the file.-w: Count words in the file.-m: Count characters in the file.- No option: Output the count of lines, words, and bytes.
To count the number of lines in test.txt:
python ccwc/ccwc.py -l test.txtTo count the number of lines, words, and bytes in test.txt:
python ccwc/ccwc.py test.txtccwc/
├── ccwc.py # Main script
├── __init__.py # Init file for the package
└── tests/ # Directory for test cases
├── __init__.py
└── test_ccwc.py
This is the main script that handles the command-line arguments, reads the file or standard input, and prints the required counts.
This file contains the unit tests for the ccwc script. It uses the unittest framework to verify the functionality of the ccwc script.
To run the tests, use the following command:
python -m unittest discover -s testsThe tests cover the following functionalities:
- Counting bytes (
-c) - Counting lines (
-l) - Counting words (
-w) - Counting characters (
-m) - Default behavior (counts lines, words, and bytes)
- Reading from standard input
python ccwc/ccwc.py -c test.txtOutput:
342190 test.txt
python ccwc/ccwc.py -l test.txtOutput:
7145 test.txt
python ccwc/ccwc.py -w test.txtOutput:
58164 test.txt
python ccwc/ccwc.py -m test.txtOutput:
339292 test.txt
python ccwc/ccwc.py test.txtOutput:
7145 58164 342190 test.txt