Exam 2: Control


Assigned: Wednesday, March 9

Due: Beginning of class, Wednesday, March 16

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 on this examination. Each problem is worth 10 points, for a total of 100 points. Although each problem is worth the same amount, problems are not necessarily of equal difficulty.

Read the entire exam before you begin.

We 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's more to life than CS” and you will earn at least a 70 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 have completed the exam in three hours or less or have spent at least three hours on the exam. In the latter case, they should also report on what work they've completed in the three hours. After receiving such notices, I may 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. If you use code that you wrote for a previous lab or homework, cite that lab or homework and the other members of your group. If you use code that you found on the course Web site, be sure to cite that code. You need not cite the code provided in the body of the examination.

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 Weinman (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. You should use examples other than those that may be provided with the exam. Do not include images; I should be able to regenerate those.

Unless I state otherwise, you should document any procedures you write using the six-P documentation 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 (7 a.m. - 6 p.m.), feel free to try to call me in the office (269-9812).

I will also reserve time at the start of each class before the exam is due to discuss any general questions you have on the exam.

Problems

Part A: Conditionals, Colors, and Images

Problem 1: Reporting types

Topics: Conditionals, predicates

Unlike other programming languages, Scheme is not particularly careful about types. Therefore, it might be helpful for programmers to know what kind of value they might be working with. Write, but do not document, a procedure, (type-of value), that produces

  • the symbol boolean, if value is a boolean;
  • the symbol integer, if value is an integer;
  • the symbol number, if value is a number but not an integer;
  • the symbol procedure, if value is a procedure;
  • the symbol string, if value is a string;
  • the symbol symbol, if value is a symbol;
  • the symbol other, if value is anything else.

Recall that a symbolic value can be generated by prefacing a name with the single quote, as in 'rose.

Problem 2: Changing Colors

Topics: Conditionals, RGB colors, color transformations

So far the behavior of many RGB transformations you have seen and/or implemented has not explicitly been dependent on qualities of the input color. We can also explore color transformations that involve conditionals.

Write and document an RGB transformation (rgb-stronger rgb) that applies rgb-redder to rgb when red is the largest component of rgb, rgb-greener when green is the largest component, or rgb-bluer when blue is the largest component.

Problem 3: Hidden Images

Topics: Image transformations, conditionals, RGB colors as integers

In this problem, we consider an approach to keeping messages secret. Steganography is the process of hiding a message in an image such that no one, other than the sender and receiver, even knows there is any hidden information. One simple approach is to hide a black-and-white image inside of a full-color image. No one is likely to notice if a photo has been changed by only 1/255 from the original brightness values.

Here is the algorithm we used to create a steganographic image.

  • Take the original or cover image, which is in full color, and also a secret image, in pure black and pure white. These images must have the same width and height.
  • For each row and column,
    • Take the color from the original image. If it is represented as an odd integer, subtract 1 so that the result is even.
    • If the color in the secret image is white, add 1 to the previous result. If it is black, add 0 to the previous result.
    • The resulting number is used as the color of this pixel in the new steganographic image.
  • Return the steganographic image.

(a) Write, but do not document, a procedure, (image-steg-decode steg-image), that takes a steganographic image produced using this algorithm and produces the secret image.

Hints: Use image-variant in your procedure. Observe that if the color is represented by an odd number, this corresponds to a white pixel in the secret image. If the color is represented by an even number, this corresponds to a black pixel in the secret image.

(b) Download this image of a cute little kitty (Professor Davis's cat, Sprite, when he was a kitten), and test your procedure on it. Describe the secret image hidden inside it.

Part B: Concise Transformations

Problem 4: Making colors greyer

Topics: RGB colors, color transformations, local bindings

Consider the following procedure.

;;; Procedure:
;;;   rgb-greyer
;;; Purpose:
;;;   To make a color greyer while preserving its hue.
;;; Parameters:
;;;   color, an rgb color
;;;   weight, a real number
;;; Produces:
;;;   greyer, an rgb color
;;; Preconditions:
;;;   0 <= weight <= 1
;;; Postconditions:
;;;   If weight is 0, then greyer is equal to color
;;;   If weight is 1, then greyer is equal to (rgb-greyscale color)
;;;   (rgb->hue greyer) = (rgb->hue color)
;;;     (unless weight is 1; then the hue of greyer will be 0)
;;;   (rgb->value greyer) <= (rgb->value color)
;;;   (rgb->saturation greyer) <= (rgb->saturation color)
;;; Process:
;;;   Each component of the color is blended with the color's grey value
;;;     in proportion to weight.
(define rgb-greyer
  (lambda (color weight)
    (rgb-new (+ (* (- 1 weight) (rgb-red color))
                (* weight
                   (+ (* 0.30 (rgb-red color))
                      (* 0.59 (rgb-green color))
                      (* 0.11 (rgb-blue color)))))
             (+ (* (- 1 weight) (rgb-green color))
                (* weight
                 (+ (* 0.30 (rgb-red color))
                    (* 0.59 (rgb-green color))
                    (* 0.11 (rgb-blue color)))))
           (+ (* (- 1 weight) (rgb-blue color))
              (* weight
                 (+ (* 0.30 (rgb-red color))
                    (* 0.59 (rgb-green color))
                    (* 0.11 (rgb-blue color))))))))

Notice that this procedure is very repetitive. Using let and/or let*, rewrite this procedure as concisely as you can.

Problem 5: Making images greyer

Topics: Image transformations, anonymous procedures

Write, but do not document, a procedure (image-greyer image weight), which transforms all of the pixels in image using rgb-greyer. Note that providing 0 for the weight parameter will result in no change to the image, whereas providing 1 will result in a greyscale image.

Again, be concise.

Part C: Lists and Repetition

Problem 6: Spiraling Turtles

Topics: Turtles, iteration, anonymous procedures

Most of the turtle procedures you have been writing so far use the same turtle to iterate a process. For instance, the procedure turtle-spiral! from the reading on iteration iterates the process of moving one turtle forward and turning by an increasing angle.

  (define turtle-action-02!
  (lambda (turtle angle)
    (turtle-forward! turtle 5)
    (turtle-turn! turtle angle)))

  (define turtle-spiral!
  (lambda (turtle steps)
    (for-each (l-s turtle-action-02! turtle)
              (list-drop (iota (+ 1 steps)) 1))))

While repeatedly drawing polygons is interesting and useful because the turtle always returns to its original position, there are situations in which we may not want to rely on this postcondition. Those situations will often call for using multiple turtles.

Using turtle-spiral! as a helper, write, but do not document, a procedure, (turtles-spin-longer-spirals! turtles initial-steps scale-factor angle), that uses turtle-spiral! with each turtle in the list turtles. Before drawing its spiral, each turtle should be turned by angle degrees more than its previous turtle.

For example, if angle were 30, the first turtle would turn by zero, the second by 30, the third by 60, and so on.

In addition, each turtle's spiral should have a number of steps that is approximately scale-factor times the the previous turtle's number of steps. (Because the number of steps needs to be an integer, you may need to round.)

Here is an example of turtle-spin-longer-spirals! in action:

(define forest (image-new 200 200))
(define tiffany (turtle-new forest))
(turtle-teleport! tiffany 100 100)
(define clones (map turtle-clone (make-list 8 tiffany)))
(turtles-spin-longer-spirals! clones 4 1.5 45)

Problem 7: The Closest Drawing

Topics: Drawings, recursion

On the previous exam, you may have written procedures like the following to calculate the center of a drawing.

(define drawing-center-x
  (lambda (drawing)
    (+ (drawing-left drawing)
       (/ (drawing-width drawing) 2))))

(define drawing-center-y
  (lambda (drawing)
    (+ (drawing-top drawing)
       (/ (drawing-height drawing) 2))))

Consider the following procedure that computes one metric for the “distance” between two drawings.

;;; Procedure:
;;;   drawing-distance
;;; Parameters:
;;;   drawing1, a drawing
;;;   drawing2, a drawing
;;; Purpose:
;;;   Find the "distance" between two drawings, using a simple metric.
;;; Produces:
;;;   distance, a non-negative number.
;;; Preconditions:
;;;   [No additional.]
;;; Postconditions:
;;;   If (x1,y1) is the center of drawing1, and 
;;;    (x2,y2) is the center of drawing2, then
;;;    distance = sqrt( (x1-x2)^2 + (y1-y2)^2 )
(define drawing-distance
  (lambda (drawing1 drawing2)
    (sqrt (+ (square (- (drawing-center-x drawing1)
                        (drawing-center-x drawing2)))
             (square (- (drawing-center-y drawing1)
                        (drawing-center-y drawing2)))))))

Write and document a procedure, (drawing-closest drawing candidates), that finds the element of candidates that is closest to drawing.

Note: You are likely to find it helpful to write a local helper procedure, drawing-closer, that determines which of two candidates is closer to drawing.

Problem 8: First and Last

Topics: Lists

Write and document a procedure (take-ends lst) that returns a list containing the first and last item in lst.

> (take-ends (iota 10))
(0 9)
> (take-ends (list "red" "orange" "yellow" "green" "blue"))
("red" "blue")

Hint: Do not use recursion. Instead, use existing list procedures.

Problem 9: increasing?

Topics: Recursion, predicates, and, or

Write, but do not document, a procedure (increasing? numbers) that returns #t if every number in the list (except the last) is less than or equal to the number immediately after it, and returns #f otherwise.

> (increasing? (iota 10))
#t
> (increasing? (reverse (iota 10)))
#f
> (increasing? (list 1/10 1/2 1/3 5/6))
#f
> (increasing? (list 0 1 1 2 3 3 10))
#t

Part D: Decoding procedures

Topics: Code reading, testing, recursion, documentation

As you've probably noted, we like to write long procedure names that make it perfectly clear what procedures are supposed to do. These procedures have names like image-compute-pixels! or rgb-complement. We also try to choose good names for the variables and parameters in our code.

However, some programmers prefer to give their procedures and variables short names, such as f and x. Some pay little attention to formatting.

Unfortunately, such short names can obfuscate code, particularly when combined with a lack of care in formatting. For example, consider the following useful procedure.

(define f (lambda (p x) (if (null? x) null (cons (p (car x)) (f p (cdr x))))))

Problem 10: Making the procedure readable

Make this procedure definition easier to read as follows.

First, reformat the procedure so that the indentation properly demonstrates nesting. That is, you should choose appropriate points to put in carriage returns. Remember that MediaScheme will re-indent for you after you put in those returns, as long as you select the text and type Control-Tab.

Then, figure out what the procedure does. After doing so, change the names in the procedure to clarify the roles of various things. You should certainly rename the procedure f and the parameters p and x. (As a hint, start by figuring out what types p and x should be.)

Finally, write introductory comments (the six P's) to explain the purpose (and the other P's) of the procedure formerly named f. Pay particular attention to the preconditions.

When you format your exam, please put the introductory comments before the revised procedure definition.

Some questions and answers

Here we will post answers to questions of general interest. Please check here before emailing your questions!

Problem 2

Question: What should we do when one color component alone is not the largest?
Answer: The problem doesn't specify. You may choose, but make sure your documentation accurately reflects your choice.

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. We usually limit such extra credit to five points. However, if we make an astoundingly large number of errors, then we will provide more extra credit.

  • There were words missing from the sentence, "there are situations we may not want to rely on this postcondition," in Problem 6. [KJ, 1 point]
  • The documentation of drawing-distance referred to colors rather than drawings. [KJ, 1 point]

Jerod Weinman

Copyright © 2007-2011 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (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.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License .