Assigned: Wednesday, 28 October 2009
Due: Beginning of class, Wednesday, 4 November 2009
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 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.
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.
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 Rebelsky.
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.
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 (), feel free to try to call me in the office (269-9812) or at home ().
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.
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.
a. Write, but do not document, a predicate,
(, that checks whether
simple-drawing?
value)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
b. Write, but do not document, a predicate,
(, that determines whether
drawing-ellipse?
val)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
Document and write a predicate, (, that determines whether
drawing-circle?
val)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
Document and write a procedure, (, 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
val)
>(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
Document and write a procedure, (, 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.
drawings-largest
drawings)
Topics: Turtles, Recursion, Generalizing, Pixels
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, (, that draws the given number
of copies of the specified polygon, with each copy drawn with a
side length turtle-growing-polygon!
turtle initial-side-length
sides length-increment
copies)length-increment larger than
the previous side.
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, (, that
finds the element of rgb-closest
color candidates)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-RED RGB-WHITE)))"255/255/255">(rgb->string (rgb-closest (rgb-new 255 128 0) (list RGB-BLACK RGB-RED RGB-WHITE)))"255/0/0"
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, (,
that creates a new image by converting the color at each pixel
in palettize
image colors)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))
Topics: Lists, Checking Preconditions, Recursion, Generalization
It is often the case that there are multiple ways to write the same
procedure. In this problem, you will write two versions
of (, a procedure that builds a new list from
drop-ends
lst)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.
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.
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 ( that
returns the number of times tally
pred? lst)pred? holds for
the values in lst.
>(tally odd? (iota 5))2
Here we will post answers to questions of general interest. Please check here before emailing your questions!
list-ref in solving this problem?
drawing? in solving this problem?
simple-drawing? predicate in
problem 2 as well as in problem 1b?
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.
simple-drawing? used
simple-drawing (no question mark). [PM, 1 point]
null from the list of acceptable values.
[DR, 1 point]
drop-ends, that builds”
is awkward. [CV]