ASSIGNMENT 1 2025
UNIQUE NO.
DUE DATE: 2025
,Answer for Question 1 (COS3711 Assignment 1 - 2025)
Requirements Recap:
1. Command-line Usage:
o count → No arguments: Print usage instructions.
o count file1.txt → Process a single file.
o count file1.txt fileTwo.txt → Process multiple files.
o count –a –b file1.txt fileTwo.txt → Apply specific flags.
o count –ab –c file1.txt → Alternative flag syntax.
2. Flags and Their Functions:
o -a: Count words longer than 4 characters that start with a capital letter.
o -b: Count hyphenated words (not starting or ending with a hyphen).
o -c: Count words that start and end with the same character.
o -d: Count words that do not start with a vowel.
3. Text Processing:
o Remove whitespace at the start and end of the document.
o Remove punctuation (.,?!:; etc.) using regular expressions.
o Assume only one space between words.
Implementation Approach
1. Use Python or C++ (since Qt is mentioned, C++ with Qt might be preferred).
2. Process input files and extract words.
3. Apply regex-based counting for each flag condition.
4. Handle command-line arguments properly.
, Sample Python Implementation
python
import sys
import re
def process_file(filename, flags):
try:
with open(filename, 'r', encoding='utf-8') as file:
text = file.read().strip()
# Remove punctuation
text = re.sub(r'[.,?!:;]', '', text)
words = text.split()
results = {"-a": 0, "-b": 0, "-c": 0, "-d": 0}
for word in words:
# -a: Words longer than 4 chars starting with a capital letter
if "-a" in flags and len(word) > 4 and word[0].isupper():
results["-a"] += 1
# -b: Hyphenated words (not starting/ending with a hyphen)
if "-b" in flags and re.search(r'\b\w+-\w+\b', word):
results["-b"] += 1
# -c: Words that start and end with the same letter
if "-c" in flags and len(word) > 1 and word[0].lower() == word[-
1].lower():
results["-c"] += 1
# -d: Words not starting with a vowel
if "-d" in flags and not re.match(r'^[aeiouAEIOU]', word):
results["-d"] += 1
print(f"\nResults for {filename}:")
for key, value in results.items():
if key in flags:
print(f"{key}: {value}")
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
def main():
args = sys.argv[1:]
if not args:
print("Usage: count [-a -b -c -d] <file1> <file2> ...")
return
flags = {arg for arg in args if arg.startswith('-')}
files = [arg for arg in args if not arg.startswith('-')]
if not files:
print("Error: No files specified.")