Presentation is loading. Please wait.

Presentation is loading. Please wait.

Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:

Similar presentations


Presentation on theme: "Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:"— Presentation transcript:

1 Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Homework #4

2 Homework #4 Maximum function length = 50 lines
However, good practice  < 25 lines! Make your modules into logical units — e.g., Reading files and picking apart input Managing the list of words Formatting and writing output file Control (and testing) Within each module, make functions into logical units E.g., reading file and scanning lines are separate logical units! CS-1004, A-Term 2014 Homework #4

3 Yeah! — I understand all that, but I just cannot get my head around it.
CS-1004, A-Term 2014 Homework #4

4 Let’s look at the pieces
Start near the beginning … … but not at the beginning One module, one function Open one input file Read it and turn it into a list of words Ignore punctuation for now! Close file!! Test it! Write a separate module called Control, Wrapper, Test, or something like that Prompt user (i.e., YOU) for file name, call your function for that file, get returned list, print out list Manually check, convince yourself that it is correct Questions so far? How? CS-1004, A-Term 2014 Homework #4

5 Some students put this part in file-read module!
Next Step In a new module, write one or more functions … … go thru list of words i.e., with a for-loop Use string method strip() on each word Remove leading and trailing quotes, commas, periods, semicolons, et. Don’t worry if you miss some punctuation You can come back later to fix Also make lower case Create new list of “cleaned up” words Test again Modify wrapper to take list from one module, pass it to another Inspect result manually Some students put this part in file-read module! CS-1004, A-Term 2014 Homework #4

6 Step #3 Still in same module — new function
Sort the list of cleaned up words Using list method sort() Test and inspect manually Are the words really in alphabetical order? Need a for-loop Go thru alphabetical list of words Keep two variables outside the loop Latest word Count If next word in loop is same as Latest Word Increment Count Otherwise Append Latest Word and its Count to yet another list Set Latest Word to next word, Count = 1 CS-1004, A-Term 2014 Homework #4

7 Testing Step #3 Beef up your wrapper (i.e., control module or test module, or whatever you call it) Prompt for one file name Invoke function to read file and build up list Invoke functions to clean up words, strip punctuation Invoke function to sort and count list Print out and inspect result manually CS-1004, A-Term 2014 Homework #4

8 Step #4 – output In yet another module …
… learn how to format output lines as specified Learn and use string method format() §5.8.2 (pp ) Open output file Write output lines Close output file Test from Wrapper Prompt user for file name CS-1004, A-Term 2014 Homework #4

9 Step #5 Modify wrapper to get more than one input file name
Open and get list of words from each one Combine lists from all input files Generate combined output CS-1004, A-Term 2014 Homework #4

10 Step #6 Modify wrapper to get input from command line
See next slide Option:– if command line is empty, prompt user Put it all together and test CS-1004, A-Term 2014 Homework #4

11 Command Lines Windows, Macintosh, and Linux all have “command prompt” windows Command line format:– verb arg1 arg2 arg3 ... verb is name of a program that carries out command action Each arg is a string Delimited by spaces arg0 is verb! Meaning:– Apply verb to the list of arguments Don’t return till finished! CS-1004, A-Term 2014 Homework #4

12 Operating System’s Responsibility
Pick apart command line Create a list of strings called “argv” Number of items is list is “argc” Load the program named verb (i.e., arg0) into a clean memory space. Call the function with the name main(), passing argc and argv as arguments Wait till it returns, continue with next command line CS-1004, A-Term 2014 Homework #4

13 Starting programs in a GUI
User “opens” a file or document OS or Window manager consults list of file types Finds program that opens the type of this file or document (Essentially) constructs a command line! As if it had been typed Name of verb (i.e., program) as arg0 Name of file to be opened as arg1 Other arguments as needed Calls main() function of the program! CS-1004, A-Term 2014 Homework #4

14 python HW4.py outFile InFile1 InFile2 …
What about Python? Command must be python or python3 Command line must be python HW4.py outFile InFile1 InFile2 … Getting the arguments into Python sys.argv is a list containing the strings:– ['HW4.py', 'outFile', 'InFile1', 'InFile2', …] Windows Macintosh Linux CS-1004, A-Term 2014 Homework #4

15 Questions? CS-1004, A-Term 2014 Homework #4


Download ppt "Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:"

Similar presentations


Ads by Google