Due Thursday, September 30th, 3:00 PM
Use pair-programming in lab and the assignment this week. As always, you partner has to be someone in your lab, and this time your partner must be different from your partner in the second assignment.
Recall from class the STAIR steps to problem solving:
- State the problem (understand it thoroughly)
- Review the Tools you have to solve the problem
- Devise an Algorithm
- Implement your algorithm
- Test and Refine your solution, returning to previous steps as necessary
C:\home\202\a\3>python lab4.py Monty test.txt 2
Hints:
- Recall the first application example in class that prints the sum of its arguments
With solution:C:\home\202\src>python sum.py 1 2 3.3 6.3import sys def main(): sum = 0 for arg in sys.argv[1:]: sum += float(arg) print sum main()- There is a string module function that counts substrings. Use the on-line documentation to find it. Whenever a problem calls for doing something that seams like it is a simple-to-specify and frequently useful operation such as this, check the library module documentation. You will often find it has been done for you! Such re-use of library code is one of the most important keys to managing software complexity.
When you have completed the last exercise above, or 15 minutes before the lab ends, whichever comes first, submit your lab4.py file as lab 4 in Vincent.
C:\home\202\a\3>python wc.py futval.py test.txt wc.py
22 103 699 futval.py
2 4 22 test.txt
52 223 1683 wc.py
76 330 2404 total
Hints
- Don't know how to write the whole thing? Then start by solving a simpler problem that has some of the same elements. In this case, start by writing a "word-count" application that only takes one file name and prints the counts associated with that file. Loop over the lines in the file with three accumulators.
- Now finish the assignment, adding an outer loop with three more accumulators for the totals.
Motivation: Besides Windows, the other most common family of operating systems is Unix (and Linux is the most popular flavor of Unix). This application's behavior is similar to the popular Unix command wc (word count). wc counts newline (a.k.a. line feed) and return characters as separate, while wc.py counts them as one character if they appear together at the end of a line. Though wc is smart enough not to print a total when there is only one (or zero) file names, that requires a conditional test that has not been introduced yet in this course, so print the total line in every case. If there are no file name arguments, the totals will be zero.
When you are done, submit your final wc.py file as a4 using Vincent. As always, if you cannot finish all of the assignment, be sure to submit, before the due time, a version of your file that does as much as you can get working.