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

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear.
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Guide To UNIX Using Linux Third Edition
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python
General Programming Introduction to Computing Science and Programming I.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
5 1 Data Files CGI/Perl Programming By Diane Zak.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Chapter VII: Arrays.
Introduction to Computing Science and Programming I
Whatcha doin'? Aims: To start using Python. To understand loops.
Input from STDIN STDIN, standard input, comes from the keyboard.
Python: Experiencing IDLE, writing simple programs
Containers and Lists CIS 40 – Introduction to Programming in Python
CSc 120 Introduction to Computer Programing II
Lecture 07 More Repetition Richard Gesick.
CS190/295 Programming in Python for Life Sciences: Lecture 1
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
4. sequence data type Rocky K. C. Chang 16 September 2018
ARRAYS 1 GCSE COMPUTER SCIENCE.
Decision Structures and Indefinite Loops
Introduction to Dictionaries
Programming Assignment #1 12-Month Calendar—
Programming Project #1 Command Shell
Homework #5 — Monte Carlo Simulation
Elements of a Python Program
More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Variables, Lists, and Objects
Debuggers and Debugging
Programming Project #1 Fork and Command Shell
Simple Graphics Package
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
More elements of Python programs
Notes on pyplot Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Python programming exercise
CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Note on Program Design Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Notes on Homework #6 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Strings, Lists, and Files
CISC101 Reminders All assignments are now posted.
CSE 303 Concepts and Tools for Software Development
Introduction to Computer Science
Introduction to Computer Science
CISC101 Reminders Assignment 3 due today.
Numpy, pylab, matplotlib (follow-up)
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

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

Yeah! — I understand all that, but I just cannot get my head around it. CS-1004, A-Term 2014 Homework #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

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

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

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

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. 147-149) Open output file Write output lines Close output file Test from Wrapper Prompt user for file name CS-1004, A-Term 2014 Homework #4

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

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

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

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

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

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

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