diff --git a/homework04/Makefile b/homework04/Makefile new file mode 100644 index 0000000..c45b03d --- /dev/null +++ b/homework04/Makefile @@ -0,0 +1,27 @@ +test: + @$(MAKE) -sk test-all + +test-all: test-cut test-wc + +test-data: + @curl -sLO https://www3.nd.edu/~pbui/teaching/cse.20289.sp25/static/txt/homework04/cut.input + @curl -sLO https://www3.nd.edu/~pbui/teaching/cse.20289.sp25/static/txt/homework04/wc.input + +test-scripts: test-data + @curl -sLO https://www3.nd.edu/~pbui/teaching/cse.20289.sp25/static/txt/homework04/cut.test + @curl -sLO https://www3.nd.edu/~pbui/teaching/cse.20289.sp25/static/txt/homework04/wc.test + + @chmod +x ./*.test + +test-cut: test-scripts cut.py + @echo Testing Cut ... + @./cut.test -v + @echo + +test-wc: test-scripts wc.py + @echo Testing WC ... + @./wc.test -v + @echo + +clean: + @rm -f *.input *.test diff --git a/homework04/cut.py b/homework04/cut.py new file mode 100644 index 0000000..62b5225 --- /dev/null +++ b/homework04/cut.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +''' cut.py - remove sections from each line of stream ''' + +import io +import sys + +# Functions + +def usage(exit_status: int=0) -> None: + ''' Print usage message and exit. ''' + print('''Usage: cut -d DELIMITER -f FIELDS + +Print selected parts of lines from stream to standard output. + + -d DELIMITER Use DELIM instead of TAB for field delimiter + -f FIELDS Select only these fields''', file=sys.stderr) + sys.exit(exit_status) + +def strs_to_ints(strings: list[str]) -> list[int]: + ''' Convert all strings in list to integers. + + >>> strs_to_ints(['2', '4']) + [2, 4] + ''' + pass + +def cut_line(line: str, delimiter: str='\t', fields: list[int]=[]) -> list[str]: + ''' Return selected fields from line separated by delimiter. + + >>> cut_line('Harder, Better, Faster, Stronger', ',', [2, 4]) + [' Better', ' Stronger'] + ''' + pass + +def cut_stream(stream=sys.stdin, delimiter: str='\t', fields: list[int]=[]) -> None: + ''' Print selected parts of lines from stream to standard output. + + >>> cut_stream(io.StringIO('Harder, Better, Faster, Stronger'), ',', [2, 4]) + Better, Stronger + ''' + pass + +# Main Execution + +def main(arguments=sys.argv[1:], stream=sys.stdin) -> None: + ''' Print selected parts of lines from stream to standard output. + + This function will parse the command line arguments to determine the + delimiter and which fields to select from each line. + + >>> main('-d , -f 2,4'.split(), io.StringIO('Harder, Better, Faster, Stronger')) + Better, Stronger + ''' + # Parse command line arguments + pass + + # Cut stream with delimiter and fields + pass + +if __name__ == '__main__': + main() + +# vim: set sts=4 sw=4 ts=8 expandtab ft=python: diff --git a/homework04/wc.py b/homework04/wc.py new file mode 100644 index 0000000..67b4ee3 --- /dev/null +++ b/homework04/wc.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +''' wc.py - print newline, word, and byte counts for stream ''' + +import io +import sys + +# Functions + +def usage(exit_status: int=0) -> None: + ''' Print usage message and exit. ''' + print('''Usage: wc.py [-l | -w | -c] + +Print newline, word, and byte counts from standard input. + +The options below may be used to select which counts are printed, always in +the following order: newline, word, byte. + + -c Print byte counts + -l Print newline counts + -w Print word counts''', file=sys.stderr) + sys.exit(exit_status) + +def count_stream(stream=sys.stdin) -> dict[str, int]: + ''' Count the newlines, words, and bytes in specified stream. + + >>> count_stream(io.StringIO('Despite all my rage, I am still just a rat in a cage')) + {'newlines': 1, 'words': 13, 'bytes': 52} + ''' + pass + +def print_counts(counts: dict[str, int], options: list[str]) -> None: + ''' Print the newline, word, and byte counts. If none of the options are + specified, then include all options in output. Othewrise, only include the + specified options. + + Note: always output the counts the following order: newlines, words, bytes. + + >>> print_counts({'newlines': 1, 'words': 13, 'bytes': 52}, ['newlines', 'words', 'bytes']) + 1 13 52 + ''' + pass + +# Main Execution + +def main(arguments=sys.argv[1:], stream=sys.stdin) -> None: + ''' Print the newline, word, and byte counts from stream. + + This function will parse the command line arguments to select which counts + to include in the final report. + + >>> main([], io.StringIO('Despite all my rage, I am still just a rat in a cage')) + 1 13 52 + ''' + # Parse command line arguments + pass + + # Count stream and print counts + pass + +if __name__ == '__main__': + main() + +# vim: set sts=4 sw=4 ts=8 expandtab ft=python: