Saturday, October 25, 2025

Week 2 Assignments

1. Real Legend:

The Unix system (https://en.wikipedia.org/wiki/Unix) is a legend. It sets the foundation for most of the modern-day operating systems that run on billions of devices. More importantly its design philosophy of simplicity and reusability has a massive influence on generations of programmers, in simple terms, to create a program that does one thing really well, and let it work with other programs.

The Unix system has many very famous commands which are also legends.  wc is one of them.  See https://en.wikipedia.org/wiki/Wc_(Unix).  You want to recreate the functions of wc in Python so that you can be a real legend too.

Input the name of an existing text file.  Output 3 numbers, the number of lines, the number of words (delimited by spaces), and the number of characters (including spaces and newlines).

2. Head, tail, and more

You've managed to recreate the classical Unix command wc with Python.  Dennis Ritchie and Ken Thompson send their regards.

They would like you to continue with a few more extremely useful Unix commands:

head: takes an input file name, number n, and an output file name; writes the first n lines of the input file to the output file.  If the output file name is not provided, just write to the screen.

hint: what happens if there are less than n lines in the input file?

tail: very similar to head, only that it writes the last n lines.

more: takes an input file name.  Writes the contents of the file to screen, but pause at a full screen and wait for input.  Once the ENTER key is hit, show the next full screen of file content, until the entire file is shown.

hint: how do you know how many lines is a full screen?  Make some assumptions.  If you really want to be accurate, look up shutil.get_terminal_size().
Extension: what if the program accepts 'any key' instead of ENTER for the next screen of output?  Do some research.

No comments:

Post a Comment