Fundamentals of Computer Science I: Media Computing (CS151.01 2008S)

Exam 3: Scheme, Refined


Assigned: Friday, 25 April 2008

Due: Beginning of class, Friday, 2 May 2008

Preliminaries

Exam format

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.

There are ten problems, each worth 10 points, giving a total of 100 points. Although each problem is worth the same amount, problems are not necessarily of equal difficulty. Nonetheless, you should plan to spend about twenty minutes on each problem. If you spend more than ten minutes on a problem without making progress, move on to another problem and come back to it later. Once you hit more than twenty minutes on a problem, you should consider asking for help from Professor Rebelsky (or Professor Davis).

Read the exam as soon as possible so that you can bring questions to class. Read the entire exam before you begin working.

I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. In particular, this exam is likely to take you about two to three hours, depending on how well you've learned the topics and how fast you work. You should not work more than four hours on this exam. Stop at four hours and write “There is more to life than CS” (on both the electronic and printed versions) and you will earn at least 75 points on this exam.

I would also appreciate it if you would write down the amount of time each problem takes. Each person who does so will earn two points of extra credit. Since I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they've spent at least three hours on the exam or completed the exam. (At that point, I may then change the exam.)

Academic honesty

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than as restricted by that limitation, you should feel free to use all reasonable resources available to you. As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web. And, in case it's not clear, you may not ask others (in person, via email, via IM, by posting a please help message, or in any other way) to put answers on the Web.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say “This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam.” You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must include both of the following statements on the cover sheet of the examination.

  1. I have neither received nor given inappropriate assistance on this examination.
  2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. You need not reveal the particulars of the dishonesty, simply that it happened. Note also that inappropriate assistance is assistance from (or to) anyone other than Professor Rebelsky (that's me) or Professor Davis.

Presenting Your Work

You must present your exam to me in two forms: both physically and electronically. That is, you must write all of your answers using the computer, print them out, number the pages, put your name on the top of every page, and hand me the printed copy. You must also email me a copy of your exam. You should create the emailed version by copying the various parts of your exam and pasting them into an email message. In both cases, you should put your answers in the same order as the problems. Failure to name and number the printed pages will lead to a penalty of two points. Failure to turn in both versions may lead to a much worse penalty.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code. Do not include images; I should be able to regenerate those.

Unless I explicitly tell you to document your procedures, you need not document them. When you are asked to document procedures, you should make sure to document them using the six-P style.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.

I will give partial credit for partially correct answers. I am best able to give such partial credit if you include a clear set of work that shows how you derived your answer. You ensure the best possible grade for yourself by clearly indicating what part of your answer is work and what part is your final answer.

Getting Help

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).

I will also reserve time at the start of classes next week to discuss any general questions you have on the exam.

Problems

Problem 1: Named let vs. letrec

Topics: Local bindings, List recursion.

Consider the following procedure that computes the ratio of dark colors to light colors in a list.

(define dark-to-light-ratio
  (lambda (colors)
    (letrec ((kernel (lambda (dark light remaining)
                       (cond
                         ((null? remaining)
                          (/ dark light))
                         ((rgb-dark? (car remaining))
                          (kernel (+ dark 1) light (cdr remaining)))
                         ((rgb-light? (car remaining))
                          (kernel dark (+ light 1) (cdr remaining)))
                         (else
                          (kernel dark light (cdr remaining)))))))
      (kernel 0 0 colors))))

Rewrite dark-to-light-ratio so that it uses a named let rather than a letrec.

To test dark-to-light-ratio, you will also need the following supporting procedures.

;;; Procedure:
;;;   rgb-dark?
;;; Parameters:
;;;   color, an RGB color
;;; Purpose:
;;;   Determine if the color seems dark.
;;; Produces:
;;;   dark?, a Boolean value
;;; Preconditions:
;;;   [None]
;;; Postconditions:
;;;   dark? is true (#t) if color's intensity is relatively low.
;;;   dark? is false (#f) otherwise.
(define rgb-dark?
  (lambda (color)
    (>= 64 (+ (* 0.30 (rgb-red color)) (* 0.59 (rgb-green color)) (* 0.11 (rgb-blue color))))))

;;; Procedure:
;;;   rgb-light?
;;; Parameters:
;;;   color, an RGB color
;;; Purpose:
;;;   Determine if the color seems light.
;;; Produces:
;;;   light?, a Boolean value
;;; Preconditions:
;;;   [None]
;;; Postconditions:
;;;   light? is true (#t) if color's intensity is relatively high.
;;;   light? is false (#f) otherwise.
(define rgb-light?
  (lambda (color)
    (<= 192 (+ (* 0.30 (rgb-red color)) (* 0.59 (rgb-green color)) (* 0.11 (rgb-blue color))))))
 

Problem 2: Documentation

Topics: Preconditions, documentation.

Using the six-P style, document dark-to-light-ratio.

Note: When referring to dark and light colors you should simply describe them as dark and light and not in terms of the formulae in the definitions above.

Problem 3: Verifying Preconditions

Topics: Precondition testing, checking list types.

Rewrite dark-to-light-ratio so that it tests all appropriate preconditions and reports an error message if any of those preconditions fail.

Problem 4: Computing Sequences of Substrings

Topics: Strings, numeric recursion.

As you may recall, (substring str start finish) extracts a substring from str that runs from position start to position finish-1. The string-length procedure lets you find the length of a string.

Write, but do not document, a procedure, (all-substrings str len), that generates a list of all the substrings of str of length len. For example,

> (all-substrings "television" 3)
("tel" "ele" "lev" "evi" "vis" "isi" "sio" "ion")

Problem 5: Computing the Cube Root

Topics: Divide-and-conquer, numeric recursion.

We learned the divide-and-conquer strategy when studying binary search. We have also applied that strategy to the problem of sorting (or we will, soon). Let's consider one other instance in which divide-and-conquer may help: computing cube roots.

To compute the cube root of a number, n, we start with two estimates, one that we know is lower than the cube root and one that we know is higher than the cube root. We repeatedly find the average of those two numbers and refine our guess. We stop when the cube of the average is “close enough” to n.

What should we start as the lower-bound and upper-bound of the cube root? Well, if n is at least 1, we can use 0 as the lower-bound and n as the upper bound.

For example, our procedure produces the following sequence of guesses to find the cube root of 2 with an accuracy of 3 decimal places.

>(cube-root 2)
(lower: 0 upper: 2 avg: 1 avg-cubed: 1)
(lower: 1 upper: 2 avg: 1.5 avg-cubed: 3.375)
(lower: 1 upper: 1.5 avg: 1.25 avg-cubed: 1.953125)
(lower: 1.25 upper: 1.5 avg: 1.375 avg-cubed: 2.599609375)
(lower: 1.25 upper: 1.375 avg: 1.3125 avg-cubed: 2.260986328)
(lower: 1.25 upper: 1.3125 avg: 1.28125 avg-cubed: 2.103302002)
(lower: 1.25 upper: 1.28125 avg: 1.265625 avg-cubed: 2.02728653)
(lower: 1.25 upper: 1.265625 avg: 1.2578125 avg-cubed: 1.989975452)
(lower: 1.2578125 upper: 1.265625 avg: 1.26171875 avg-cubed: 2.008573234)
(lower: 1.2578125 upper: 1.26171875 avg: 1.259765625 avg-cubed: 1.999259926)
1.259765625

Implement, but do not document, (cube-root n), which should approximate the cube root of n to 3 decimal places of accuracy. (That is, the difference between n and the cube of (cube-root n) should be less than 0.001.)

Problem 6: The Brightest Pixel, Revisited

Topics: Colors, iterating over images, optimal values.

You may recall that in the past, we looked for the brightest colors in a list of colors. It might make more sense to look for the brightest pixel in an image. Write, but do not document, a procedure, (image-brightest-pixel image) that returns the color of the brightest pixel.

Warning: This procedure may be quite slow even if correctly implemented. Test it on a very small image (e.g., 5x5 pixels) before trying it on a larger image.

In writing image-brightest-pixel, you may find the following procedure helpful.

;;; Procedure:
;;;   rgb-brightness
;;; Parameters:
;;;   color, an RGB color
;;; Purpose:
;;;   Computes the brightness of color on a 0 (dark) to 100 (light) scale.
;;; Produces:
;;;   b, an integer
;;; Preconditions:
;;;   color is a valid RGB color.  That is, each component is between
;;;     0 and 255, inclusive.
;;; Postconditions:
;;;   If color1 is likely to be perceived as lighter than color2,
;;;     then (brightness color1) > (brightness color2).
(define rgb-brightness
  (lambda (color)
    (round (* 100 (/ (+ (* 0.30 (rgb-red color))
                        (* 0.59 (rgb-green color))
                        (* 0.11 (rgb-blue color)))
                      255)))))

Problem 7: Searching Files

Topics: Higher-order procedures, searching, files.

Write, but do not document, a procedure, (file-search filename pred?), that searches the specified file for the first value that matches pred?. For example, if the file contains the values

12
1
23
152
6

then we might see the following results.

> (file-search "sample-file" odd?)
1
> (file-search "sample-file" (lambda (x) (> x 100)))
152
> (file-search "sample-file" (lambda (x) (< x 0)))
#f
> (file-search "sample-file" string?)
#f

Problem 8: Puzzling Code

Topics: Deep recursion, code reading.

The following higher-order procedure does something with trees.

(define tt
  (lambda (t p?)
    (cond
       ((pair? t)
        (+ (tt (car t) p?)
           (tt (cdr t) p?)))
       ((p? t)
        1)
       (else
        0))))

Explain the purpose of this procedure. That is, explain what the procedure computes, not how it does that computation.

Problem 9: Selection Sort

Topics: Sorting, vectors.

The famous selection sort algorithm for sorting vectors works by repeatedly stepping through the vector from back to front, swapping the largest remaining thing to the next place in the vector. We might express that algorithm for a vector of strings as

(define string-vector-selection-sort!
  (lambda (strings)
      (let kernel ((pos (- (vector-length strings) 1)))
        (if (< pos 0)
            strings
            (let* ((index (string-vector-index-of-largest strings pos))
                   (largest (vector-ref strings index)))
              (vector-set! strings index (vector-ref strings pos))
              (vector-set! strings pos largest)
              (kernel (- pos 1)))))))

Of course, we will need the procedure string-vector-index-of-largest, which we might document as

;;; Procedure:
;;;   string-vector-index-of-largest
;;; Parameters:
;;;   strings, a vector of strings
;;;   pos, an integer
;;; Purpose:
;;;   Find the index of the alphabetically last string in the
;;;   subvector of strings at positions [0..pos].
;;; Produces:
;;;   index, an integer
;;; Preconditions:
;;;   pos > 0.
;;; Postconditions:
;;;   For all i from 0 to pos, inclusive,
;;;     (string-ci>=? (vector-ref strings index) (vector-ref strings i))

Implement this procedure.

Problem 10: Finding the Largest Element in a List

Topics: Sorting, Patterns of list recursion, Higher-order programming.

Suppose that instead of sorting vectors using selection sort, we wanted to sort lists. Also suppose that we wanted a more generalized sorting routine. We would need to start with a procedure that finds the largest element in the list. We might express the basic design of this procedure as

;;; Procedure:
;;;   list-largest
;;; Parameters:
;;;   lst, a list
;;;   may-precede?, a binary predicate
;;; Purpose:
;;;   Find the largest value in lst.
;;; Produces:
;;;   largest, a value
;;; Preconditions:
;;;   may-precede? may be applied to any two values in lst.
;;;   may-precede? is transitive.
;;; Postconditions:
;;;   largest is an element of lst.
;;;   For valid indices, i, of lst,
;;;     (may-precede? (list-ref lst i) largest)

Implement list-largest.

Hint: We have implemented a number of procedures that find the “best” or “highest” value in a list, such as rgb-brightest or spots-leftmost. You may want to use one of those procedures as a pattern.

Some Questions and Answers

Problem 2

What do you mean by “When referring to dark and light colors you should simply describe them as dark and light and not in terms of the formulae in the definitions above.

I mean that your documentation should not say something like “We determine whether a color is dark by adding 30% of the red component ....” Just call dark and light colors “dark” and “light”.

Problem 3

Do I have to verify that rgb-dark? and rgb-light? are defined?

No.

Errata

Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. I usually limit such extra credit to five points. However, if I make an astoundingly large number of errors, then I will provide more extra credit.

  • The topics for Problem 6 mentioned file iteration, which isn't necessary to solve this problem. You should think about iterating over images instead. [JN, 1 point]
  • The documentation for string-vector-index-of-largest duplicated the word “the”. [GH, 1 point]

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-8 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.