Assigned: Wednesday, 27 October 2010
Due: Beginning of class, 11 a.m Wednesday, 3 November 2010
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.
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 Davis.
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 (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.
Topics: Drawings as values, Predicates, Lists, Conditionals, 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.
Note: You may not use the
procedure drawing? in solving this problem.
>(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,
(, 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
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
Write a procedure, (, that finds the area of a drawing
that is a rectangle or a circle. (You need not find the area of any
ellipse that is not a circle.)
drawing-area
val)
Be sure to check preconditions and print appropriate error message if those preconditions are not met.
>(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>(drawing-area (drawing-group drawing-unit-circle drawing-unit-square))drawing-area: Don't know how to find the area of that drawing.>(drawing-area 5)drawing-area: Expected a drawing, given 5
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, Iteration, Recursion, Colors, Generalizing, Local Bindings, Efficiency and Conciseness.
Write, but do not document, a
procedure, (, that draws the given number
of copies of the specified polygon. Each copy should be offset by
the specified turtle-shift-polygon!
turtle side-length
sides shift-angle
shift-distance
copies)shift-angle
and shift-distance.
The turtle should be returned to its original position and orientation.
Here is an example of turtle-shift-polygon! in
action:
|
(define world (image-new 200 200)) (define ted (turtle-new world)) (image-show world) (turtle-teleport! ted 100 100) (turtle-shift-polygon! ted 20 3 0 20 3) (turtle-shift-polygon! ted 10 3 -60 15 10) (turtle-shift-polygon! ted 15 3 135 10 10) |
You may certainly use your turtle-polygon! procedure
from homework 5, but remember to cite it appropriately.
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))))))
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-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"
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))
The Color Scheme Designer we used earlier in the semester has a setting allowing you to view a color palette as an individual with full color blindness might see it. The color blends you saw in an earlier reading might appear very different when given the same treatment.
Therefore, someone implemented a
procedure that
creates an image of a blend between two gray-scale versions of its
arguments, as shown below.
gray-gradient
|
(image-show (gray-gradient RGB-RED RGB-BLUE 128 64)) |
|
(image-show (gray-gradient RGB-RED RGB-YELLOW 128 64)) |
Rewrite the implementation below to eliminate as much of the repeated code and computation as you can. You do not need to document the revised procedure.
(define gray-gradient
(lambda (color1 color2 width height)
(image-compute
(lambda (col row)
(rgb-new (+ (* (/ col width)
(+ (* 0.30 (rgb-red color1))
(* 0.59 (rgb-green color1))
(* 0.11 (rgb-blue color1))))
(* (- 1 (/ col width))
(+ (* 0.30 (rgb-red color2))
(* 0.59 (rgb-green color2))
(* 0.11 (rgb-blue color2)))))
(+ (* (/ col width)
(+ (* 0.30 (rgb-red color1))
(* 0.59 (rgb-green color1))
(* 0.11 (rgb-blue color1))))
(* (- 1 (/ col width))
(+ (* 0.30 (rgb-red color2))
(* 0.59 (rgb-green color2))
(* 0.11 (rgb-blue color2)))))
(+ (* (/ col width)
(+ (* 0.30 (rgb-red color1))
(* 0.59 (rgb-green color1))
(* 0.11 (rgb-blue color1))))
(* (- 1 (/ col width))
(+ (* 0.30 (rgb-red color2))
(* 0.59 (rgb-green color2))
(* 0.11 (rgb-blue color2)))))))
width height)))
Topics: Lists, Checking Preconditions, Recursion, Generalization
(a) Write (, a
procedure that builds a new list from
drop-ends
lst n)lst by dropping the first and
last n elements.
>(drop-ends (iota 10) 1)(1 2 3 4 5 6 7 8)>(drop-ends (list "red" "orange" "yellow" "green" "blue") 2)("yellow")
Hint: Do not use recursion. Instead, use existing list procedures.
(b) Add precondition checking to your implementation of
drop-ends. Provide a separate error message for each
type of error.
You may recall that in the reading on list recursion, revisited, we developed templates for testing whether a predicate holds for any value in a list or for all values in a list. We can implement these templates as new, more general procedures.
a. Write, but do not document, a recursive
predicate, (, that
produces all
predicate? lst)#t if predicate? is true for
all values in lst, and #f
otherwise.
b. Write, but do not document, a recursive predicate, (, that
produces any
predicate? lst)#t if predicate? is true for
some value in lst, and #f
otherwise.
c. Using the simple-drawing? predicate you wrote
for problem 1, write, but do not document, a new function
(.
Make your implementation as concise as possible.
all-simple-drawings?
lst)
Here we will post answers to questions of general interest. Please check here before emailing your questions!
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.
(define world (image-new 200
200) was missing a closing parenthesis. [EL, 1 point]col
and row incorrectly. [HBP and JW, 1 point]