Assigned: Monday, 27 October 2008
Due: Beginning of class, Friday, 31 October 2008
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 10 problems, each 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.
I 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've spent at least three hours on the exam or completed the exam. (At that point, I may then 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.
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 explicitly ask you to document your procedures, you need not write introductory comments.
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. Feel free to try to call me in the office (269-9812).
I will also reserve time at the start of classes next week to discuss any general questions you have on the exam.
Topics: Color representations (color names and RGB colors), color transformations, list iteration, composition, documentation.
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.
Write (but do not yet document) a procedure,
(, 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
cnames-much-bluer
color-names)rgb-bluer twice to the corresponding color in
the original list. For example,
>(cnames-much-bluer (list "red" "blue" "green" "black" "white" "grey" "brown" "spicy pink" "neon avocado" "neon blue"))("hot pink" "blue" "neon avocado" "deep midnight blue" "white" "periwinkle" "cornflower blue" "magenta" "sea algae green" "neon blue")
You should make the body of this procedure as concise as you can.
Using the six-P style, document cnames-much-bluer.
A Tip. cnames-much-bluer
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.)
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.
As you've seen, image-compute-pixels! can be used
to draw interesting color ranges and to draw shapes. Let's consider
once again a problem in which you might do both.
Suppose we want to draw a background and three circles on canvas.
We might write
(define canvas (image-new 100 100))
(image-show canvas)
(image-compute-pixels!
canvas 0 0 (- (image-width canvas) 1) (- (image-height canvas) 1)
(lambda (pos)
(if (zero? (random 100))
color-white
color-black)))
(image-compute-pixels!
canvas 0 0 (- (image-width canvas) 1) (- (image-height canvas) 1)
(lambda (pos)
(if (>= 1600 (+ (square (- (position-col pos) 30)) (square (- (position-row pos) 40))))
(rgb-new (+ 128 (* 255 (sin (* (/ pi 60) (position-row pos)))))
0
(+ 128 (* -255 (cos (* (/ pi 60) (position-row pos))))))
color-transparent)))
(image-compute-pixels!
canvas 0 0 (- (image-width canvas) 1) (- (image-height canvas) 1)
(lambda (pos)
(if (>= 900 (+ (square (- (position-col pos) 50)) (square (- (position-row pos) 50))))
(let ((c (* 4 (sqrt (+ (square (- (position-col pos) 60))
(square (- (position-row pos) 60)))))))
(rgb-new c 0 c))
color-transparent)))
(image-compute-pixels!
canvas 0 0 (- (image-width canvas) 1) (- (image-height canvas) 1)
(lambda (pos)
(if (>= 25 (+ (square (- (position-col pos) 20)) (square (- (position-row pos) 50))))
color-black
color-transparent)))
Rewrite this code so that there is only one call to
image-compute-pixels!
Hint: Use a
cond to decide which, if any, of the circles each
position belongs in.
Hint: Which circle appears in front, and which appears behind? Your code should produce approximately the same image as the code above (up to some randomness).
Topics: Code reading, testing, recursion, documentation, verifying preconditions, husk and kernel
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 r (lambda (v s) (if (equal? v (car s)) (cdr s) (cons (car s) (r v (cdr s))))))
Make this procedure 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 DrFu will re-indent for you after you put in those returns, as long as you select the text and hit 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 r
and the parameters v and
s. (As a hint, start by figuring out what types
v and s should be.)
Write introductory comments (the six P's) to explain the purpose (and the
other P's) of the procedure formerly named r.
Pay particular attention to the preconditions.
Modify the procedure so that it uses husk-and-kernel style and checks whether its preconditions are met. Throw a separate error for each failed precondition.
Topics: Recursion over lists, helper recursion, locally bound procedures, recursive predicates, procedures as parameters.
reverse
Suppose the reverse
procedure were not included in Scheme. Could you
write it yourself? Certainly! Using the helper recursion strategy,
implement a procedure (. Use my-reverse
lst)letrec or named
let in your solution. You need not verify the procedure's
preconditions. You should not use
append in your solution.
The reading on list recursion, revisited discusses patterns for writing recursive procedures that test whether all values, or any value, in a list satisfy some predicate. We can implement each pattern directly as a higher-order procedure: that is, a procedure that takes another procedure as a parameter.
(a) Using the given template for all-___?
as a guide, write a procedure ( that
returns all?
pred? lst)#t if pred? is true for
every value in lst, and #f
otherwise.
(b) Using the given template for any-___?
as a guide, write a procedure ( that
returns any?
pred? lst)#t if pred? is true for
at least one value in lst, and #f
otherwise.
You might remember that way back in Homework 5 we observed that it would be nice to be able to apply an entire list of transformations to a single color. Now that you've learned how to write recursive procedures, you can write a procedure to let you do this.
Implement the procedure (.
procmap
proclist value)procmap takes
as its parameters a list of procedures and a single value; it produces a
new list that contains the results of applying each procedure to the
value. For example,
>(procmap (list + -) 3)(3 -3)>(procmap (list (lambda (x) x) rgb->string rgb->cname) (rgb-new 255 0 0))(-16776961 "255/0/0" "red")>(procmap (list rgb-lighter rgb-darker rgb-complement rgb-rotate) (rgb-new 255 128 64))(-7319297 -277860097 8372223 -2143223809)>(map rgb->string (procmap (list rgb-lighter rgb-darker rgb-complement rgb-rotate) (rgb-new 255 128 64)))("255/144/80" "239/112/48" "0/127/191" "128/64/255")
Topics:
Color transformations,
let and let*,
anonymous procedures,
and transforming images.
Color maps are a frequently used image format that allow an image to be represented efficiently with a small number of specific colors. Like a "paint-by-number" drawing, a color map is a function that takes an integer and returns a color. Each image pixel stores only the color index, rather than a full RGB color, and the index is used to lookup a particular color in the map when the image is rendered.
In this part, you will write a procedure for converting an image
from RGB to one that is rendered using an indexed color map. You
may use the rgb-brightness procedure from
the lab on
conditionals to convert a color to an integer brightness. The
brightness must then be rescaled for use as an index to a colormap
(a list of rgb colors with no more than 101 entries).
rgb-colormap
(a) Write a procedure (
that takes an RGB color, finds the brightness of the color,
rescales the brightness to
be between 0 and rgb-colormap
color colormap)(length colormap) - 1,
and finally uses this number as an index
into the list of RGB colors colormap, returning the
appropriate color from colormap.
For instance, the following code defines a particular colormap from cold to warm colors as the index increases from 0 to 15:
(define heatmap
(list (rgb-new 0 0 192)
(rgb-new 0 0 255)
(rgb-new 0 64 255)
(rgb-new 0 128 255)
(rgb-new 0 192 255)
(rgb-new 0 255 255)
(rgb-new 64 255 192)
(rgb-new 128 255 128)
(rgb-new 192 255 64)
(rgb-new 255 255 0)
(rgb-new 255 192 0)
(rgb-new 255 128 0)
(rgb-new 255 64 0)
(rgb-new 255 0 0)
(rgb-new 192 0 0)
(rgb-new 128 0 0)))
As an example, the following colors produce the corresponding colors
from heatmap:
>(rgb->list (rgb-colormap (cname->rgb "black") heatmap))(0 0 192)>(rgb->list (rgb-colormap (cname->rgb "cyan") heatmap))(255 128 0)>(rgb->list (rgb-colormap (cname->rgb "red") heatmap))(0 192 255)>(rgb->list (rgb-colormap (cname->rgb "sienna") heatmap))(64 255 192)>(rgb->list (rgb-colormap (cname->rgb "parrot green") heatmap))(255 192 0)>(rgb->list (rgb-colormap (cname->rgb "orchid") heatmap))(255 255 0)>(rgb->list (rgb-colormap (cname->rgb "white") heatmap))(128 0 0)
Hint: Note how rgb-brightness
rescales its values from 0-255 to 0-100.
Hint: You may or may not need to remember
about inexact->exact.
Hint: Notice that there are actually 101 brightness values in [0-100].
(b) Assuming an image is loaded in the variable canvas,
write a single expression using image-variant
and your rgb-colormap
procedure to apply the colormap transform to canvas
using heatmap (above) as the colormap.
For example, using the input image below produces the adjacent output.
(define canvas (image-load "/home/weinman/public_html/courses/CSC151/2008F/assignments/exam2.2008F/pots-in.jpg"))
![]() |
![]() |
cond?rgb-colormap works the
same as the example for most of the colors, but gives a different answer for
the color cyan. Is that OK?
The difference between your version of the code and ours is subtle.
If you are curious and have time to pursue this, here are some
hints: Use quotient rather than
/. And think carefully about the number of
possible brightness values and the number of values in the
colormap.
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. I usually limit such extra credit to five points. However, if I make an astoundingly large number of errors, then I will provide more extra credit.
procmap appeared as map-procs in Problem 9. [JS, 1 point]