In addition to the concepts below, you should also know the
concepts from Quiz 1,
Quiz 2, and Quiz 3.
You should be able to define the following terms:
- top-down design
- incremental testing (also called "unit testing")
- stubbed-out functions
- pseudocode
Practice problems:
- Write a program that reads in the lines from a file
called infile.txt and creates an acronym from the first
letter of each line. For example, if the input file is:
Thanks
Goodness
It's
Friday
Your program will create and print out the string "TGIF"
- Imagine you are writing a function to process grade data (call it
nameAndGrade()). Write a function stub to process a string such as
"rich,11-15-2013,97.5" and return a list with just the name and grade:
["rich", 97.5]. Implement your function and test it with the
following code:
print nameAndGrade("rich,11-15-2013,97.5")
print nameAndGrade("ben,11-17-2013,83.0")
print nameAndGrade("jason,12-15-2013,76.7")
- Suppose we wish to design a program that simulates a coin toss and
counts how many times a coin must be flipped to get k consecutive
heads. The parameter k should be input by the user and the program
should allow the user to enter how many times (num) to run the
simulation. Assume that after applying one level of top-down design,
you came up with the following main steps:
- get the number of simulations to run (num) from the user
- get a value for k from the user (it needs to be a non-negative integer)
- prompt user for a value for k
- while k is not a non-negative integer, continue to prompt the user for a valid input
- once a valid, non-negative integer input is received, return that value to step 3
- repeat num times:
- do one simulation of flipping a coin until you get k
consecutive heads
- print out the number of flips it took to get k
consecutive heads
Apply top down design again by expanding (3a) into several
substeps. Then code up your solution and test.
include("../style/footer.php"); ?>