CS21: Quiz 4 Study Guide

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:

Practice problems:

  1. 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"

  2. 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")
    
  3. 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:
    1. get the number of simulations to run (num) from the user
    2. get a value for k from the user (it needs to be a non-negative integer)
      1. prompt user for a value for k
      2. while k is not a non-negative integer, continue to prompt the user for a valid input
      3. once a valid, non-negative integer input is received, return that value to step 3
    3. repeat num times:
      1. do one simulation of flipping a coin until you get k consecutive heads
      2. 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.