COMP 200: Elements of Computer Science
Spring 2013

Exam 1

You may use 3.5 contiguous hours on this exam. You may use any resources on this exam, except for other people. See also the course policies.

Put all your answers in one CodeSkulptor file. Use Python comments to include any answers that are not Python code. Use Python comments to clearly mark each problem number. Submit your CodeSkulptor URL on OWL-Space, under Exam 1.

To save you time, we have provided a template for you to start from.

Test your Exam 1 code using OwlTest.

As a matter of good style and conciseness, wherever reasonable you should use functions that have been defined previously on this assignment, on a previous assignment, or in class. Documentation strings have been provided for all specified code, but you should also provide documentation strings in any additional helper functions that you create. Also, only use global variables when absolutely necessary.

Plotting a Histogram (60 points total)

In this section, you will write a series of functions. For each, you may assume that the input is of the specified form, so that no error-checking is necessary.

  1. (10 points)

    Define a function that takes a filename like "comp200-S13exam1-scores.txt". It reads the data found in the URL for such CodeSkulptor data files, splits it based on whitespace, and returns a list of strings.

    • def strings_in_file(filename):
          """Returns a list of strings of the whitespace-delimited content in the specified file."""
      
          # Put your code here.
      

    Example: strings_in_file("comp200-S13exam1-scores.txt") should return ["45", "89", "73", "100", "97", "23", "6", "18", "0", "78", "97", "23", "63", "67", "80", "20", "68", "88", "92"].

  2. (10 points)

    Define a function that takes a list of strings. It returns a list of floating-point numbers.

    • def strings_to_floats(strings):
          """Returns a list of floating numbers represented by the given list of strings."""
      
          # Put your code here.
      

    Example: strings_to_floats(["1.3", "-2", "0", ".5", "27.9"]) should return [1.3, -2, 0, 0.5, 27.9].

  3. (10 points)

    Define a function that takes a non-negative integer n. It returns a dictionary mapping each of the integers 0, …, n-1 to zero.

    • def zero_dict(n):
          """Returns a dictionary mapping 0..n-1 each to zero."""
      
          # Put your code here.
      

    Example: zero_dict(4) should return {0 : 0, 1 : 0, 2 : 0, 3 : 0}.

  4. (20 points)

    For grading purposes, scores typically get grouped in ranges 90–100, 80–89.99, 70–79.99, etc. In this problem, we will number those ranges 9, 8, 7, etc., and count the number of scores in each of those ten ranges.

    Define a function that takes a list of numeric scores, each between 0 and 100, inclusive. It returns a dictionary mapping each of the keys key = 0, …, 9 to a count of the number of scores in the range key×10 to just below (key+1)×10. For example, key 7 maps to the count of all scores in the range 70–79.99, and key 0 maps to the count of all scores in the range 0–9.99. As a special case, key 9 maps to the count of all scores in the range 90–100.

    • def make_histogram(scores):
          """Given a list of numeric scores in range 0-100, returns a dictionary
             mapping 0..9 each to a count of the scores in the range
             key*10 to just below (key+1)*10.  Key 9's value also counts
             scores of 100."""
      
          # Put your code here.
      

    Example: make_histogram([3, 64, 83, 18, 100, 29, 23, 74, 83]) should return {0 : 1, 1 : 1, 2 : 2, 3 : 0, 4 : 0, 5 : 0, 6 : 1, 7 : 1, 8 : 2, 9 : 1}.

    Hints: You can use zero_dict() to initialize the counts. Early in the course, we saw math to calculate the tens digit of a number.

  5. (10 points)

    Define a function that takes a filename of a file containing numbers between 0 and 100. It displays a plot of a histogram counting those scores as grouped in deciles.

    The plot's title should be "Histogram of scores in FILENAME by deciles", where FILENAME is replaced by the given filename. The x- and y-axis labels should be "Deciles" and "Number of scores", respectively. There should be no legend.

    • def plot_scores_histogram(filename):
          """Displays a plot of the histogram for the scores stored in the given file."""
      
          # Put your code here.
      

    Hint: Use the previous functions!

Short-answer Problems (20 points total)

For each of the following, you should be able to give a concise answer within a single paragraph.

  1. (10 points)

    Describe at least two benefits to writing your program as a combination of smaller functions, as in the previous section, than as one big function.

  2. (10 points)

    When using the original version of our predator-prey population-modeling code, we often saw negative populations. Such data is impossible in real life. Assuming that the Lotke-Volterra equations accurately model the real world, describe in your own words why our initial code obtained anomalous results and why adding time steps alleviated this problem.

Debugging (20 points total)

  1. (20 points total)

    The following code is buggy — it doesn't work. See the documentation string for what it is supposed to do.

    • def fibs_incorrect(start1, start2, n):
          """Given two numbers, start1 and start2, returns a list of n
             numbers in the Fibonacci sequence starting with the given
             two start values.  Thus, n should be an integer greater
             than or equal to 2.  The Fibonnacci sequence is defined with
             the formula
               result[i]+result[i+1] = result[i+2].
             For example, fibs(0, 1, 10) should return
             [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]."""
          
          for i in range(n):
               result[i + 2] = result[i] + result[i + 1]
          return result
      
    1. (10 points)

      Fix the code. Your corrected version should be named fibs().

    2. (10 points)

      For each change you made, describe what the mistake was and why it was incorrect.