/
Notes about Homework #4 Notes about Homework #4

Notes about Homework #4 - PowerPoint Presentation

marina-yarberry
marina-yarberry . @marina-yarberry
Follow
343 views
Uploaded On 2019-11-08

Notes about Homework #4 - PPT Presentation

Notes about Homework 4 Professor Hugh C Lauer CS1004 Introduction to Programming for NonMajors Slides include materials from Python Programming An Introduction to Computer Science 2 nd edition by John ID: 764721

1004 list homework term list 1004 term homework file 2014 module command wrapper function output step verb line test

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Notes about Homework #4" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Notes about Homework #4 Professor Hugh C. LauerCS-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) Homework #4 CS-1004, A-Term 2014 1

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

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

Let’s look at the pieces Start near the beginning …… but not at the beginningOne module, one functionOpen one input fileRead it and turn it into a list of wordsIgnore punctuation for now!Close file!!Test it! Write a separate module called Control, Wrapper, Test, or something like thatPrompt 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? Homework #4 CS-1004, A-Term 2014 4 How?

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

Step #3 Still in same module — new functionSort the list of cleaned up wordsUsing list method sort()Test and inspect manuallyAre the words really in alphabetical order?Need a for-loopGo thru alphabetical list of wordsKeep two variables outside the loopLatest word CountIf next word in loop is same as Latest WordIncrement CountOtherwise Append Latest Word and its Count to yet another listSet Latest Word to next word, Count = 1 Homework #4 CS-1004, A-Term 2014 6

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

Step #4 – output In yet another module …… learn how to format output lines as specifiedLearn and use string method format()§5.8.2 (pp. 147-149)Open output fileWrite output linesClose output fileTest from Wrapper Prompt user for file nameHomework #4 CS-1004, A-Term 2014 8

Step #5 Modify wrapper to get more than one input file nameOpen and get list of words from each oneCombine lists from all input filesGenerate combined outputHomework #4 CS-1004, A-Term 2014 9

Step #6 Modify wrapper to get input from command lineSee next slideOption:– if command line is empty, prompt userPut it all together and testHomework #4CS-1004, A-Term 2014 10

Command Lines Windows, Macintosh, and Linux all have “command prompt” windowsCommand 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! Homework #4 CS-1004, A-Term 2014 11

Operating System’s Responsibility Pick apart command lineCreate 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 Homework #4 CS-1004, A-Term 2014 12

Starting programs in a GUI User “opens” a file or documentOS or Window manager consults list of file typesFinds program that opens the type of this file or document(Essentially) constructs a command line!As if it had been typedName of verb (i.e., program) as arg0Name of file to be opened as arg1Other arguments as neededCalls main() function of the program! Homework #4 CS-1004, A-Term 2014 13

What about Python? Command must be python or python3Command 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', …] Homework #4 CS-1004, A-Term 2014 14 Windows Macintosh Linux

Questions? Homework #4CS-1004, A-Term 201415