File I/O and top-down design

In class exercises
Create a w07-design subdirectory in your cs21/inclass directory, and from within your w07-design subdirectory copy over some python files from my public directory:
    $ cd 
    $ cd cs21/inclass
    $ pwd
        /home/your_user_name/cs21/inclass
    $ mkdir w07-design        
    $ cd w07-design
    $ pwd
      /home/your_user_name/cs21/inclass/w07-design
    $ cp ~adanner/public/cs21/w07-design/* .
    $ ls
	
Formatted Output
open format.py and we will briefly look at Python's support for formatted output statements.
File Demo
Open fileDemo.py in vim. In a separate window, change to the w07-design directory so you can run the program using python fileDemo.py.

Think about the answers to the following questions. Feel free to modify the program or use the python shell to help you answer these questions.

  1. What does fileDemo.py do?
  2. What is the format of colors.txt?
  3. What type of data does the readlines() method return?
  4. To which class of data (what type?) does the readlines() belong?
  5. What does the strip() method do?
  6. What does the strip() method return?
  7. Does the strip() method modify the original data?
  8. To which class of data (type) does the strip() method belong?
Design
Top Down Design is a problem solving technique where:
  1. Start with General Description of Problem
  2. Break it into several high-level steps
  3. Iteratively break the steps into smaller steps until you have steps that are easy to solve
Top-down design is a lot like creating a paper outline for a large paper where you start with the main sections, and iteratively refine each part with more and more detail until you are ready to start writing the paper.

When you use top-down design, your resulting program's structure should closely match that of the steps: the main function should have calls to a few high-level functions, usually one for each high-level step; high-level functions have calls to functions that implement sub-steps of the high-level step; and so on.

Program functions also come from recognizing part of the code (or recongizing steps in the algorithm) where that are similar to other parts and generalizing that functionality into a function.

Iterative Refinement

When writing a large program, programmers use Iterative Refinement: do some top-down design, write function stubs for this part of code and maybe some implementation, then test. Iteratively, add more functions, and perhaps refine some of the steps using Top-Down design, and test, and so on. The idea is to write some code, test it, then write a little more code, and test it before writing even more. Usually, I write a function, then test it, write another function, test it, ... This way if I'm careful about testing, I know that if there is a bug in my program it is with the new code I've just added.

Function Prototyping

We often use prototyping to just put in function stubs so we can test the whole program's flow of control without having to have a full implementation. For example, here is a stub for a function to compute square root (it doesn't actually do anything but print out a message with the parameter value and return some bogus value, but I can call it from other parts of my program):
def squareRoot(num):
  """
  This function computes the square root of a number.
    num: the number 
    returns: the square root of num
  """

  print "inside squareRoot num is", num

  # TODO: implement this function

  return 1

Let's try it out...

We are going to try applying Top-Down Design and Iterative Refinement to write a program that computes the winning percentage of the game of Craps by simulating some number of games and keeping track of the number won.

The game of Craps is played as follows:

A player rolls a pair of six-sided dice.

If the initial roll is 2, 3, or 12, the player loses.

If the initial roll is 7 or 11, the player wins.

Any other initial roll causes the player to "roll for points". This means that the player keeps rolling either until s/he re-rolls the initial value (a win) or rolls a 7 (a loss).

Program to write

Write a program to simulate multiple games of craps and estimate the probability that the player wins.

First Step of Top Down Design:

When we think about solving this problem, we apply top-down design to first break it down into a few high-level steps:
  1. Input: Get the number of games to play (better be a positive int value)
  2. Compute: Play number of games of craps
  3. Output: print out the number of games won and the winning percentage

At this point we could start writing the code for the main function, its code will look like calls to functions, each one implementing one of these high-level steps. We will add function stubs for the functions implementing these high-level steps to test our our main program's control flow.

Let's think about input and return values for functions we would write for these steps ((1) getPositiveInt (2) playGamesOfCraps (3) we likely don't need a function for this step (it consists of just one python instruction). We will just add the code to do this step directly to main.

Let's open craps.py, which contains code for this first step and test our program's control flow.

Next we will refine step (2) Play some number of games of craps, into sub-steps, and so on.

As we write individual functions, we can test them out independently of how they will be called in the final program by making test calls to them from main (or another function) passing in different test values and seeing if they return the correct values and do the right thing. Once we have tested them for correctness, we will remove these test calls.