Cover page images (keyboard)

Top-Down Design

Don Miner & Sue Evans



Adapted from the CS1 Course at Swarthmore College by Lisa Meeden

Press space bar for next slide

Learning Objectives

Top-Down Design

Top-Down Design

Example problem: Box plot

Example: Box plot (specification)

  1. Print out a greeting to the user explaining the program.
  2. Take a list of integers as input from either a file or the terminal.
  3. We want to print out the box plot as well as the following statistics:
    • the minimum
    • the lower quartile
    • the median
    • the upper quartile
    • the maximum
    The output should look like:
    |----[    |      ]-----|
    
    min: 0
    Q1: 4
    median: 8
    Q3:14
    max: 19
    

Example: Box plot (first level)

Example: Box plot (printBoxPlot())

Example: Box plot (printBoxPlotGraph())

Example: Box plot

Example: Box plot (getInputFromUser())

Example: Box plot (getInputFromTerminal())

Example: Box plot (calculateStatistics())

[min, q1, median, q3, max]
(1, 3, 5, 7, 9)
(1, 3, 5, 7, 9)
(5, 5, 5, 5, 5)
(1, 1, 2, 2, 2)

Success!

Example: Box plot

Example: Box plot (Sample Output)

numbers.dat - (file input)

2
17
2
12
16
8
18
12
18
8
14
11
17
18
13
4
14
13
12
11
10
6
6
7
19
13
9
17
9
1
17
2
11
9
8
6
11
12
17
17
19
3
14
5
19
9
17
7
11
15
8
9
16
6
17
2
6
11
8
16
3
11
14
9
3
10
13
6
10
3
2
10
8
15
11
19
5
13
6
7
6
7
2
13
1
10
13
9
15
15
17
15
3
13
4
18
14
17
8
19

Example: Box plot getInputFromFile()


list = getInputFromFile()
print list

linuxserver1.cs.umbc.edu[135] python boxplot.py
Enter the filename : numbers.dat
[2, 17, 2, 12, 16, 8, 18, 12, 18, 8, 14, 11, 17, 18, 13, 4, 14, 13, 12, 11, 10, 6, 6, 7, 19, 13, 9, 17, 9, 1, 17, 2, 11, 9, 8, 6, 11, 12, 17, 17, 19, 3, 14, 5, 19, 9, 17, 7, 11, 15, 8, 9, 16, 6, 17, 2, 6, 11, 8, 16, 3, 11, 14, 9, 3, 10, 13, 6, 10, 3, 2, 10, 8, 15, 11, 19, 5, 13, 6, 7, 6, 7, 2, 13, 1, 10, 13, 9, 15, 15, 17, 15, 3, 13, 4, 18, 14, 17, 8, 19]
linuxserver1.cs.umbc.edu[136]

Success!

Example: Box plot boxplot.py

design

Testing

Without top-down design...

def main():
    
    print "\nThis program will produce a box plot"
    print "for the numerical data you enter."
    print "Hello, welcome to my box plot program!\n"
	
    choice = 'invalid'
    while choice != 'file' and choice != 'type':
        choice = raw_input("Where is the input? (type 'file' or 'type') ")

    numbers = []
    if choice == 'file':
        filename = raw_input("Enter the filename : ")

        file = open(filename, "r")
        for number in file:
            newNumber = eval(number)
            numbers.append(newNumber)

        file.close()
    elif choice == 'type':
        n = int(input("How many numbers are there? "))
     
        for i in range(n):
            newNumber = int(input('> '))
      
            # add this new number to the end of the number list
            numbers.append(newNumber)

    # sort the numbers
    # luckily something exists for this...
    numbers.sort()    

    # len(mylist) returns the number of items in it
    n = len(numbers)  
                  

    # the minimum will be the first item in the list    
    minimum = numbers[0]

    # the maximum will be the last item in the list
    maximum = numbers[n - 1]  

    # the median will be the in the n/2 index
    #  notice the integer division!
    median = numbers[n / 2]

    # and the quartiles...
    q1 = numbers[n / 4]
    q3 = numbers[3 * n / 4]

    # find the sizes of the pieces
    whiskerLeftSize = q1 - minimum
    boxLeftSize = median - q1
    boxRightSize = q3 - median
    whiskerRightSize = maximum - q3

    # construct the pieces
    # the space before the minimum
    blankLeft = ' ' * minimum 
    whiskerLeft = '-' * whiskerLeftSize
    boxLeft = ' ' * boxLeftSize
    boxRight = ' ' * boxRightSize
    whiskerRight = '-' * whiskerRightSize

    # concatenate the pieces
    left = blankLeft + '|' + whiskerLeft + '[' + boxLeft + '|' 
    right = boxRight + ']' + whiskerRight + '|'
    boxplot = left + right

    print boxplot

    print "min:", minimum
    print "Q1:", q1
    print "median:", median
    print "Q3:", q3
    print "max:", maximum

Design Exercise

Draw a design diagram for the following project:

Project description:

This project allows the user to compute the volume and surface area of boxes, when s/he inputs the length, width and height of a box. The user can continue to enter the dimensions of new boxes until s/he is done.

Random Numbers

# Filename: rand1.py
# Author:   Sue Evans
# Date:     10/20/09
# Section:  All
# Email:    bogar@cs.umbc.edu
#
# This program illustrates python automatically seeding
# the random number generator using the system's time,
# so the seed will be different everytime the program runs
# using random()

from random import random

for i in range(10):
    for j in range(5):

        number = random()
        print "%.12f" % (number),
        
    print

Here's the output from two runs:

linuxserver1.cs.umbc.edu[128] python rand1.py
0.943261480372 0.154005266066 0.844525974976 0.583765951220 0.238710918762
0.840630709372 0.792588202641 0.922412700010 0.406105887829 0.928238267395
0.368546449511 0.436283507343 0.134233342198 0.241690027160 0.549987196778
0.680875157308 0.891671479328 0.163688215656 0.356264886504 0.917510472125
0.536991538634 0.725870958470 0.061311983526 0.467390124198 0.841849165201
0.736892870516 0.616416745251 0.765799219193 0.688528381762 0.335759874218
0.713517554627 0.873412461417 0.946687666341 0.753602080884 0.797920356699
0.944693652515 0.065424134275 0.024200402342 0.708441209872 0.927037309789
0.455257650896 0.222231393534 0.920537903693 0.491643880902 0.244733255432
0.284381032317 0.524329119253 0.372705775886 0.079492870458 0.134190063063
linuxserver1.cs.umbc.edu[129] python rand1.py
0.822015872381 0.236398450478 0.496277031584 0.652754711980 0.430250909378
0.712871191620 0.456168190880 0.852566313990 0.643437647359 0.437319556849
0.193554550051 0.970620848238 0.008477434438 0.550374015106 0.429892280832
0.367912143435 0.263518888582 0.690646019624 0.301095452514 0.799951784722
0.553873714449 0.756633155350 0.044985173260 0.137729652020 0.880460160921
0.749262682891 0.620612164245 0.057671190146 0.494361182914 0.471051448670
0.217800777087 0.950471608505 0.086212696549 0.702363377091 0.310036957841
0.065677666387 0.661299499973 0.425212880176 0.499324519028 0.269682940655
0.366411218288 0.093996635968 0.180165188941 0.626948514247 0.688864919803
0.979156243757 0.905714196934 0.191048791602 0.601074564485 0.598188508617
linuxserver1.cs.umbc.edu[130]
# Filename: rand2.py
# Author:   Sue Evans
# Date:     10/20/09
# Section:  All
# Email:    bogar@cs.umbc.edu
#
# This program illustrates python automatically seeding
# the random number generator using the system's time,
# so the seed will be different everytime the program runs
# using randrange() with a range of 1 to 1000, inclusive.

from random import randrange

for i in range(10):
    for j in range(5):

        number = randrange(1, 1001)
        print "%7d" % (number),
        
    print

Here's the output :

linuxserver1.cs.umbc.edu[133] python rand2.py
    688       7     220     438     586
    976     980     529     656     895
    241     236     638     593     116
    749     432     668     841     177
    345      51     388     590      16
     61     665     351     797     897
    183     841     389     845     135
    537     132     298     847     997
    675     196     288     823     428
    254     770     967     952      97
linuxserver1.cs.umbc.edu[134] python rand2.py
    393     504     112      67     306
    917     998     144     576     972
    546     975     978     540     194
    764     269     196     187     835
     62     396     544     469     779
    545     489     264     609     434
    667     124     850      35     830
    778      56     713     163     753
    276     413     381     271     254
    649     874     207     443     406
linuxserver1.cs.umbc.edu[135]

Random Exercise

Write python code that will write 100 randomly-generated values between 1 and 19, inclusive, to a file called numbers.dat with one number per line.