CSC151.02 2010S Functional Problem Solving : Exams

Exam 2: Image Models and Control Structures


Assigned: Tuesday, 9 February 2010

Due: Beginning of class, Wednesday, 17 March 2010

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 four problems on this examination. They are not necessarily of equal difficulty. Each problem may include subparts. Four correct or mostly-correct solutions will earn you an A. Three correct or mostly-correct solutions will earn you a B. Two correct or mostly-correct solutions will earn you a C. One correct or mostly-correct solution will earn you a D. Zero correct or mostly-correct solutions will earn you an F. Failure to attempt the exam will earn you a 0. Partially-correct solutions may or may not earn you a partial grade, at the discretion of the grader.

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. Four hours of work, documented attempts on all four problems, and a signed statement that “There's more to life than computer science” will earn you at least a C 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 Rebelsky (that's me), Professor Weinman, or Prof. 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 we explicitly tell you not to document a procedure, you should assume that you must document every procedure you write.

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 each class before the exam is due to discuss any general questions you have on the exam.

Problems

Problem 1: Transforming Color Names

Topics: Color representations (color names and RGB colors), Color transformations, List iteration, Composition.

In our explorations of colors, we found that we could start with a procedure such as rgb-redder that takes a color as a parameter and produces a new color, and use that procedure to transform a whole image. Let's try a similar thing with lists of color names. (Yes, it's clear that lists of color names cannot be directly converted to images, but it is certainly possible to use them to create images.) That is, let's consider procedures that transform each color name in a list. Note that this process will be slightly more difficult, because the color transformations expect RGB colors rather than color names.

Document and write a procedure, (cnames-much-darker color-names), that, given a list of color names, returns a new list of color names, with each color name in the result list an approximation of the color that results from applying rgb-darker twice to the corresponding color in the original list. For example,

> (cnames-much-darker (list "yellow" "green" "blue" "pink" "red" "violet" "black" "white"))
("gold" "darkgreen" "mediumblue" "plum" "red" "orchid" "black" "gainsboro")

You should make the body of this procedure as concise as you can. (We were able to write the body with seven units of text, plus parentheses. The body does not include the (define cnames-much-darker (lambda (cnames) ...)).)

Note: In the past, we've observed that the transformations don't always work as we might expect for colors with extreme values. Even if we ignore such colors, you may still find that the transformations you've just written don't work uniformly for all colors. Why? You may want to provide some examples (under Practica or Problems) in your documentation.

Problem 2: Grids of Turtle Drawings

Topics: Turtles, Iteration

You've now seen and written a variety of procedures that use turtles to draw interesting shapes. Let's think about a way to repeat one of those procedures. Implement the procedure given by the following documentation.

;;; Procedure:
;;;   turtle-grid!
;;; Parameters:
;;;   turtle, a turtle
;;;   turtle-proc!, a one-parameter procedure that takes a turtle
;;;     as input
;;;   n, a positive integer
;;;   m, a positive integer
;;;   forward-offset, a real number
;;;   sideways-offset, a real number
;;; Purpose:
;;;   Draw n*m copies of the procedure given by turtle-proc!, drawing
;;;   the copies in a grid.
;;; Produces:
;;;   [Nothing; Called for the side effect.]
;;; Preconditions:
;;;   (turtle-proc! turtle) returns the turtle to its starting point
;;;     and orientation.
;;; Postconditions:
;;;   turtle is returned to its starting point and orientation.
;;;   turtle-proc! has been invoked with the turtle in its original
;;;     orientation and at all positions it can reach in the following
;;;     way, where 0 <= i < n and 0 <= j < m
;;;     * moving forward i*forward-offset
;;;     * turning 90 degrees
;;;     * moving forward j*sideways-offset
;;;     * turning -90 degrees

If the turtle is facing to the right, turtle-grid! draws a grid that is n wide and m high. If the turtle is turned in some other direction, the grid is similarly turned.

For example, here's a 5x4 grid of triangles, drawn with the turtle at (0,0) and facing right.

(turtle-grid! yertle (r-s turtle-triangle! 20) 5 4 35 25)

Here's a similar grid, drawn after the turtle has turned slightly.

(turtle-turn! yertle 15) (turtle-grid! yertle (r-s turtle-triangle! 20) 5 4 35 25)

Problem 3: Quadracolor Images

Topics: Transforming images, Colors (RGB and HSV)

One of the interesting color transformations that many artists employ in manipulating existing images is to convert each pixel to one of a limited palette of colors. We're going to consider this transformation in the context of a very limited palette, one of only four colors.

Write, but do not document, a procedure, (image-quadrachromic image c0 c1 c2 c3), that builds a new image in which each pixel is whichever of c0, c1, c2, or c3 that has the closest hue to the corresponding pixel in image.

Note that you can use the built-in procedure rgb->hue to find the hue of a color.

Here are some abuses of our kitten image using this new procedure. In the first example, we use four simple colors. In the second, we use some colors sampled from the image.

(image-quadrachromic kitten RGB-GREEN RGB-RED RGB-BLUE RGB-PURPLE)
(image-show (image-quadrachromic kitten (image-get-pixel kitten 110 69) (image-get-pixel kitten 168 128) (image-get-pixel kitten 104 40) (image-get-pixel kitten 164 96)))

Problem 4: Reading Recursive Code

Topics: Code reading, Types, Local Bindings, Conditionals, Documentation

As you may have noted, you can make choices in how you write your Scheme programs so as to make them easier or harder for others to read. For example, you can use whitespace (newlines, spaces, etc.) to clarify the structure of your program or to obfuscate that structure. Similarly, you can choose meaningful or meaningless variables names. Consider the following set of procedures, in which the designer has not paid particular attention to any good habits.

(define cname->rgb-list (o rgb->rgb-list color-name->rgb)) (define c
(lambda (y) (if (null? y) (cons 0 (cons 0 (cons 0 (cons 0 null))))
(let ((x (c (cdr y)))) (if (> (car (cname->rgb-list (car y)))
(max (cadr (cname->rgb-list (car y))) (car (cddr (cname->rgb-list
(car y)))))) (cons (increment (car x)) (cdr x)) (cond ((> (car (cdr
(cname->rgb-list (car y)))) (max (car (cname->rgb-list (car y))) (cadr
(cdr (cname->rgb-list (car y)))))) (cons (car x) (cons (increment (cadr
x)) (cddr x)))) (else (if (and (> (caddr (cname->rgb-list (car y)))
(car (cname->rgb-list (car y)))) (> (caddr (cname->rgb-list (car y)))
(cadr (cname->rgb-list (car y))))) (list (car x) (cadr x) (increment
(caddr x)) (cadddr x)) (if (or (= (car (cname->rgb-list (car y)))
(cadr (cname->rgb-list (car y)))) (= (car (cname->rgb-list (car y)))
(caddr (cname->rgb-list (car y)))) (= (cadr (cname->rgb-list (car y)))
(caddr (cname->rgb-list (car y))))) (reverse (cons (increment (car
(reverse x))) (cdr (reverse x)))) (list -100 -100 -100 -100))))))))))

This code is clearly ugly. Your goal is to beautify the code so that someone can better understand both what task it accomplishes and how it accomplishes that task. While you need only turn in the cleaned-up code, we recommend that you save each intermediate version you create in case a cleanup inadvertently breaks the code.

Here are some steps you might follow in improving the code. You need not apply the steps in this order.

a. Document the cname->rgb-list procedure.

b. Figure out what the c procedure does. Here are a few examples.

> (c null)
(0 0 0 0)
> (c (list "red"))
(1 0 0 0)
> (c (list "red" "black" "grey"))
(1 0 0 2)

c. Reformat the c procedure so that the indentation properly demonstrates nesting. That is, you should choose appropriate points to put in (and remove) carriage returns. Remember that MediaScheme will re-indent for you after you put in those returns, as long as you select the text and hit tab.

d. Identify repetitious sections of code and use let or let* statements to ensure that computations are done once.

e. Change the various names in the procedure to clarify the roles of the various things.

f. In this code, similar values are often computed in very different ways. Do your best to ensure that similar values are computed using similar code.

g. There is an awkward mixture of cond and if statements. Simplify this mixture of conditionals. Along the way, you may wish to consider whether every test is necessary.

h. While the names you choose will help your successors read the procedure, they will also benefit from some explanation. Add internal comments to explain the various parts.

i. Add introductory comments (the six P's) to explain the purpose (and the other P's) of the procedure. Pay particular attention to the preconditions.

Some Tips

Problem 1. cnames-much-darker converts each color name to an RGB color, applies a color transformation to that RGB color, and then converts it back to a color name. (Well, you should have done that. If you're hitting your head now, that should be a lesson to read through the exam first.)

Problem 2. You may find it useful to write a helper procedure that draws one “row” of drawings then. returns the turtle to its starting point.

Problem 3: This problem is large enough that you will find it useful to break it up into a series of steps. For example,

  • Write a procedure, (rgb-hue-distance rgb0 rgb1), which finds the measure of the difference between the hues of two RGB colors. (Note that you'll have to take into account “wrap around”; 2 and 359 are within 3 degrees of each other on the hue wheel.)
  • Write a procedure, (rgb-estimate-2 color estimate0 estimate1), that returns whichever of estimate0 and estimate1 is closer to color. If the two are equidistant, you can return either one.
  • Write a procedure, (rgb-estimate-4 color estimate0 estimate1 estimate2 estimate3), that returns whichever of the four estimates is closest to color.
  • Write image-quadrachormic.

Problem 4: As you try to figure out what this procedure does, you should find it useful to determine the type of input it takes. For example, ask yourself what operations are applied to the input.

Some questions and answers

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

Problem 1

Question: Can we assume that the list of color names includes only real color names, and not things like "borgledyborgle"?
Answer: Yes, assuming that you document that requirement.

Problem 2

Question: Can we have the code for turtle-triangle!?
Answer: Yes.
(define turtle-triangle!
  (lambda (turtle side)
    (repeat 3
            (lambda ()
              (turtle-forward! turtle side)
              (turtle-turn! turtle 120)))))

Problem 3

Question: Can you provide some sample images?
Answer: Yes. They are now in the exam. Note that choosing nearby colors based only on hue creates some fairly strange images.

Problem 4

Question: Which one of you was sadistic enough to design this question?
Answer: The blame goes to Professor Rebelsky.
Question: Do we have to do the steps in the same order you suggested?
Answer: No. Any order that you find useful sufficies. Just make sure to complete all of the updates for the version you turn in.

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.

  • Used “turtle-forward” instead of “forward-offset”. [MP & RN, 1 point]
  • result list” should have read “resulting list”. [AM, 1 point]
  • List items in Problem 4 are not labeled correctly. [RN, 1 point]
  • In problem 3, had “two the hues” rather than “the two hues”. [MS, 1 point]
  • In the tips section, had "the two hues of two RGB"; it should be "the hues of two RGB colors". [HC, 1 point]

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-10 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.

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.