CSC151.01 2009F Functional Problem Solving : Exams

Exam 2: Control


Assigned: Wednesday, 28 October 2009

Due: Beginning of class, Wednesday, 4 November 2009

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 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 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) or Professor Weinman.

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 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 (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

Part A: Exploring the Implementation of Drawings as Values

Topics: Drawings as values, Predicates, Lists, Recursion

As you may recall, in MediaScheme, the basic values of the “drawing” data type are represented as lists of eight elements.

> drawing-unit-circle
(drawing ellipse 0 "" -0.5 -0.5 1 1)
> drawing-unit-square
(drawing rectangle 0 "" -0.5 -0.5 1 1)
> (drawing-hshift drawing-unit-circle 2)
(drawing ellipse 0 "" 1.5 -0.5 1 1)
> (drawing-hscale drawing-unit-circle 5)
(drawing ellipse 0 "" -2.5 -0.5 5 1)

For the basic drawing values, the eight elements are straightforward. Element 0 is the symbol drawing. Element 1 is the symbol ellipse or the symbol rectangle. Element 2 is the color of the drawing, represented as an RGB number. Element 3 is a string (currently unused). Element 4 is the left edge of the drawing. Element 5 is the top edge of the drawing. Element 6 is the width of the drawing. Element 7 is the height of the drawing.

There are also compound drawings, created by drawing-group and similar procedures. We will not consider them at this time, except to note that they exist.

Problem 1: A Predicate for Simple Drawings

a. Write, but do not document, a predicate, (simple-drawing? value), that checks whether value meets the form of a simple drawing. That is, your predicate should check whether value has the form described above.

> (simple-drawing? drawing-unit-circle)
#t
> (simple-drawing? 4)
#f
> (simple-drawing? (drawing-hshift drawing-unit-square 5))
#t
> (simple-drawing? (drawing-group drawing-unit-circle drawing-unit-square))
#f
> (simple-drawing? (iota 8))
#f
> (simple-drawing? (list 'drawing 'ellipse RGB-BLACK "" 2 3 4 5))
#t
> (simple-drawing? (list 'drawing 'ellipse "black" "" 2 3 4 5))
#f
> (simple-drawing? (list 'drawing 'ellipse RGB-BLACK))
#f

b. Write, but do not document, a predicate, (drawing-ellipse? val), that determines whether val is a drawing and is an ellipse. You can use the simple-drawing? predicate to first check whether a value is a drawing.

> (drawing-ellipse? drawing-unit-circle)
#t
> (drawing-ellipse? (drawing-scale drawing-unit-circle 23))
#t
> (drawing-ellipse? (drawing-hshift drawing-unit-circle -5))
#t
> (drawing-ellipse? drawing-unit-square)
#f
> (drawing-ellipse? "This is a drawing of an ellipse")
#f

Problem 2: Checking for Circles

Document and write a predicate, (drawing-circle? val), that determines whether val is a drawing and is a circle. (A circle is an ellipse with equal width and height.)

> (drawing-circle? drawing-unit-circle)
#t
> (drawing-circle? (drawing-scale drawing-unit-circle 5))
#t
> (drawing-circle? (drawing-hscale drawing-unit-circle 3))
#f
> (drawing-circle? (drawing-vshift drawing-unit-circle 18))
#t
> (drawing-circle? (drawing-hscale (drawing-vscale drawing-unit-circle 3) 3))
#t
> (drawing-circle? pi)
#f

Problem 3: The Area of Drawings

Document and write a procedure, (drawing-area val), that finds the area of a drawing that is a rectangle or a circle. (You need not find the area of a non-circle ellipse.)

> (drawing-area drawing-unit-square)
1
> (drawing-area drawing-unit-circle)
0.7853981633974483
> (drawing-area (drawing-scale drawing-unit-circle 20))
314.1592653589793
> (drawing-area (drawing-hscale drawing-unit-circle 20))
drawing-area: Don't know how to find the area of that drawing.
> (drawing-area (drawing-hscale drawing-unit-square 20))
20
> (drawing-area (drawing-vscale (drawing-hscale drawing-unit-square 20) 15))
300

Problem 4: The Largest Drawing

Document and write a procedure, (drawings-largest drawings), that takes a list of basic drawings (rectangles and circles) as a parameter and returns the drawing in the list which has the largest area.

Part B: Image Models, Revisited

Topics: Turtles, Recursion, Generalizing, Pixels

Problem 5: Growing Polygons

In a recent homework assignment, you wrote a procedure, turtle-scale-polygon, that drew a sequence of polygons, each scaled by a certain amount. One disadvantage of that procedure is that the size of the polygons grows very rapidly. As an alternative, we might simply increase the size by a constant amount.

Write a procedure, (turtle-growing-polygon! turtle initial-side-length sides length-increment copies), that draws the given number of copies of the specified polygon, with each copy drawn with a side length length-increment larger than the previous side.

Problem 6: The Closest Color

In the past few weeks, you've seen how to select the brightest color in a list or the darkest color in the list. But sometimes our criteria for selecting a color are a bit more complicated.

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

;;; Procedure:
;;;   rgb-distance
;;; Parameters:
;;;   rgb1, an RGB color
;;;   rgb2, an RGB color
;;; Purpose:
;;;   Find the "distance" between two colors, using a simple metric.
;;; Produces:
;;;   distance, a non-negative integer.
;;; Preconditions:
;;;   [No additional.]
;;; Postconditions:
;;;   If rgb1 = rgb2, then distance is 0.
;;;   Given three rgb colors, c1, c2, and c3, if c1 and c2 are likely
;;;     to be perceived as closer than c1 and c3, then
;;;     rgb-distance(c1,c2) < rgb-distance(c1,c3).
(define rgb-distance
  (lambda (rgb1 rgb2)
    (+ (abs (- (rgb-red rgb1) (rgb-red rgb2)))
       (abs (- (rgb-green rgb1) (rgb-green rgb2)))
       (abs (- (rgb-blue rgb1) (rgb-blue rgb2))))))

Document and write a procedure, (rgb-closest color candidates), that finds the element of candidates that is closest to color.

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

> (rgb->string (rgb-closest RGB-RED (list RGB-BLACK RGB-WHITE)))
"0/0/0"
> (rgb->string (rgb-closest RGB-YELLOW (list RGB-BLACK RGB-WHITE)))
"255/255/255"
> (rgb->string (rgb-closest RGB-RED (list RGB-BLACK RGB-RED RGB-WHITE)))
"255/0/0"
> (rgb->string (rgb-closest RGB-YELLOW (list RGB-BLACK RGB-BLUE RGB-WHITE)))
"255/255/255"
> (rgb->string (rgb-closest (rgb-new 255 128 0) (list RGB-BLACK RGB-RED RGB-WHITE)))
"255/0/0"

Problem 7: Color Palettes

When computers had less memory, it was inefficient to represent each color by three values. More frequently, images were represented using palettes, in which each color was selected from a small list of colors. This approach is still used to send compressed versions of images across the Internet. It can also be used to create interesting variants of images.

Write a procedure, (palettize image colors), that creates a new image by converting the color at each pixel in image to the nearest color in colors.

(palettize kitten (list RGB-BLACK RGB-WHITE))

(palettize kitten (list RGB-BLACK RGB-GREY RGB-WHITE))

(palettize kitten (list RGB-GREY RGB-WHITE))

(palettize kitten (list RGB-RED RGB-WHITE RGB-BLUE))

Part C: Lists

Topics: Lists, Checking Preconditions, Recursion, Generalization

Problem 8: Editing Lists

It is often the case that there are multiple ways to write the same procedure. In this problem, you will write two versions of (drop-ends lst), a procedure that builds a new list from lst by dropping the first and last elements.

> (drop-ends (iota 10))
(1 2 3 4 5 6 7 8)
> (drop-ends (list "red" "orange" "yellow" "green" "blue"))
("orange" "yellow" "green")

a. Write drop-ends without using recursion with an appropriate combination of the basic list procedures like cons, car, cdr, reverse, and append You can also use the basic value null.

b. Write drop-ends using recursion explicitly and only the procedures cons, car, cdr, and null?. You may also use the basic value null.

c. Which version of drop-ends do you prefer? Why?

In writing both versions of drop-ends, you may assume that the preconditions of the procedure are met.

Problem 9: Editing Lists, Revisited

Rewrite one of your two versions of drop-ends so that it checks appropriate preconditions. Provide a separate error message for each kind of error.

Problem 10: Tallying, Revisited

In writing various types of recursive procedures, it is often helpful to generalize common code. For example, if we have a two-parameter procedure, such as max or drawing-leftmost, that selects one of its two parameters according to some priority, we can generalize it to a list of values using the following strategy.

(define GENERALIZED
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (SELECT (car lst) (GENERALIZED (cdr lst))))))

Now, we can generalize max to largest by replacing GENERALIZED with largest and SELECT with max

(define largest
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (max (car lst) (largest (cdr lst))))))

We can generalize drawing-leftmost to drawings-leftmost with a similar substitution.

(define drawings-leftmost
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (drawing-leftmost (car lst) (drawings-leftmost (cdr lst))))))

We can take the next step by transforming our template into an actual procedure that takes the SELECT operation as one of its parameters. Since this process is called folding, we might call the procedure fold.

(define fold
  (lambda (select lst)
    (if (null? (cdr lst))
        (car lst)
        (select (car lst) (fold select (cdr lst))))))

Note that the only substantial difference is that we've included the select parameter in the recursive call to fold.

> (fold max (list 3 2 5 -4 0 1))
5
> (fold append (list (iota 3) (reverse (iota 3))))
(0 1 2 2 1 0)

a. We have now looked at a variety of ways to tally values. Write a generalized TALLY template that we can use to easily write new tally functions.

b. Transform your template into an actual procedure (tally pred? lst) that returns the number of times pred? holds for the values in lst.

> (tally odd? (iota 5))
2

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 I use list-ref in solving this problem?
Answer: Yes.
Question: Should we restrict simple drawings to positive widths and heights?
Answer: Yes.
Question: Can I use drawing? in solving this problem?
Answer: No.
Question: What type are drawing, rectangle, and ellipse?
Answer: They are all symbols. You can, for example, test if the second element of a list is the symbol ellipse with (equal? 'ellipse (cadr lst)).

Problem 2

Question: May we use the simple-drawing? predicate in problem 2 as well as in problem 1b?
Answer: Yes.

Problem 3

Question: Do we need to explicitly check the preconditions of this procedure?
Answer: No.
Question: Is it okay if we make this procedure work with arbitrary ellipses?
Answer: It's fine. However, if you do so incorrectly, you will lose credit.

Problem 4

Question: What should we do if there are two (or more) drawings with the largest area?
Answer: Return one of them. It's up to you which one. Your documentation need not specify which one. (You may want to look at other smallest/largest procedures for a hint as to how to document this part.)
Question: We're comparing drawings by area. Should we return the area of the largest drawing, or the drawing itself?
Answer: The goal is to return the largest drawing, not the area of that drawing.

Problem 5

Question: Do we need to check preconditions for this problem?
Answer: No.
Question: Are there any requirements regarding the ending position and orientation of the turtle?
Answer: No.
Question: Can we include a separate turtle-polygon! procedure?
Answer: Yes. However, if you copy it from a previous homework assignment, you should make sure to cite that assignment.

Problem 6

Question: What should we do if there are two equivalently close colors, and they are both “the closest color”.
Answer: You can choose either of them.

Problem 8

Question: Can we use list-drop on this problem?
Answer: No, you may not use list-drop. You also should not need to use list-drop.

Problem 10

Question: May we use the template(s) provided in problem 10 to do earlier problems? If so, do we need to cite them?
Answer: Yes, you may use these templates. Yes, you should cite them. A good rule of thumb is, if you're using provided material outside the intended/given context, it doesn't hurt to be absolutely clear as to its origin.
Question: The problem describes max as a two-parameter procedure. Can't it take more than two parameters?
Answer: Yes, max can take two, three, ten, or even more parameters. In this situation though, we're using it as a function, like drawing-leftmost or rgb-brighter, that selects one of its two parameters, based on some selection criterion.

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.

  • Example of simple-drawing? used simple-drawing (no question mark). [PM, 1 point]
  • Problem 8b omitted null from the list of acceptable values. [DR, 1 point]
  • Missing two closing parentheses in problems 1a and 1b. [JD & DN, 2 points]
  • You will write two version” should be “versions”. [JD, 1 point]
  • Two versions of drop-ends, that builds” is awkward. [CV]
  • By now we have now looked at ...” is inappropriate. [CV]

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

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