Functional Problem Solving (CSC 151 2015F) : Assignments

Exam 2: Expanding Our Vocabulary: Conditional Control Structures and New Image Models


Assigned: Tuesday, 6 October 2015

Due: The due dates for various tasks are as follows.

Important! This examination has an initial code file that you should copy and use as the starting point for your work.

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.

This examination has a prologue that must be completed by the Friday evening before the exam is due. The prologue is intended to help you get started thinking about the examination. The prologue is required. Failure to fill in the prologue by the designated time will incur a penalty of five points on the examination.

This examination has an epilogue that must be completed by the evening after the exam is due. The epilogue is intended to help you reflect carefully on the examination. The epilogue is required. Failure to fill in the epilogue will incur a penalty of five points on the exam.

There are seven problems on this examination. Each problem is worth the same number of points. Although each problem is worth the same amount, problems are not necessarily of equal difficulty.

Please 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 four hours, depending on how well you've learned the topics and how fast you work. You should not work more than five hours on this exam. Stop at five hours and write “There's more to life than CS” on the cover sheet of the examination and you will earn at least the equivalent of 70% on this exam, provided you recorded the time spent on each problem, filled in the prologue by the specified deadline, filled in the epilogue by the specified deadline, gotten at least two problems mostly right, and arranged for a meeting with me within one week of receiving your graded exam. You may count the time you spend on the prologue toward those five hours, but not the time you spend on the epilogue.. With such evidence of serious intent, your score will be the maximum of (1) your actual score or (2) the equivalent of 70%. The bonus points for errors and recording time are not usually applied in the second situation, but penalties (e.g., for failing to number pages) usually are.

You should not count time reviewing readings, laboratories, or assignments toward the amount of time you spend on the exam or on individual problems.

We 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 for the exam. Because we worry about the amount of time our exams take, we will give two points of extra credit to the first two people who honestly report that they have completed the exam in four hours or less or have spent at least four hours on the exam. In the latter case, they should also report on what work they've completed in the four hours. After receiving such notices, we 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 as well as any students who worked with you. 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, via IRC, 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 we 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.” 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 hand write, sign, and date each statement separately. 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.

Exams can be stressful. Don't let the stress of the exam lead you to make decisions that you will later regret. Violations of academic honesty and exam policies will be handled through the Committee on Academic Standing, the College Hearing Board, or the Computer Science Department's Academic Honesty Policy, as appropriate.

Presenting Your Work

You must present your exam in two forms, physically and electronically.

For the physical copy, you must write all of your answers using the computer, print them out, number the pages, staple them together (except for the cover sheet), and hand me the printed copy. For your benefit and for ours, we are doing blind grading on this examination, so you have been assigned a number to use on your exam. Please make sure that your number appears at the top of every page. You should turn in a separate cover sheet along with your stapled and printed answers. The cover sheet should include (1) the two hand-written academic honesty statements (individually signed and dated, if it is appropriate for you to sign each), (2) your name, and (3) your assigned number. If you choose to invoke the “there's more to life than computer science” option, then you must indicate that option on the cover sheet, and you should indicate it only on the cover sheet.

The code and comments in your printed copy must use a fixed-width (a.k.a., monospaced or fixed-pitch) font; depending on what platform you use, viable candidates include Monospace, Courier, Courier New, Monaco, DejaVu Sans Mono, Free Mono, Liberation Mono, and Lucida Sans Typewriter. Failure to format your code with a monospace font will result in a penalty. You may read the instructions on printing for more details on how to create readable output.

You must also submit the code for your examination at http://bit.ly/151-2015F-exam2code. Please include the whole code file, including the supplied code that appears at the bottom of the template file.

In both cases (physical and electronic), you should put your answers in the same order as the problems. Failure to number the printed pages will lead to a penalty. Failure to turn in both versions may lead to a much worse penalty.

While your electronic version is due at 10:30 p.m. Tuesday, your physical copy will be submitted in class on Wednesday. It is presumed the physical copy matches the electronic copy. Any discrepancies (other than formatting) will likely be considered a misrepresentation of your work and referred appropriately.

In many problems, we ask you to write code. Unless we specify otherwise in a problem, you should write working code and include examples that show that you've tested the code informally (by looking at what value you get for various inputs) or formally (by using the Rackunit testing framework). In addition to the examples provided in the exam, you should also provide additional examples. Do not include resulting images; we should be able to regenerate those.

Unless we tell you otherwise, you should assume that you need to provide 6P-style documentation for each primary procedure you write. If you write any helper procedures, you must document them with 6P-style documentation using at least the first four P's (Procedure, Purpose, Parameters, Produces).

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. Because we should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify in the preliminaries and problems on this exam. We will limit that form of extra credit to five points.

We will give partial credit for partially correct answers. We are 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 (8am-10pm), feel free to try to call me (cell phone (text only) - 641-990-2947).

I will also reserve time at the start of classes the week the exam is due to discuss any general questions you have on the exam.

Checklists

Since many students regularly seem to miss different elements of the exam, these checklists serve as a way to help you remember everything that you have to do.

Big Picture

  • Have you made a copy of the exam code
  • Have you replaced the 000000 in the exam code with your random number?
  • Have you picked up a random number?
  • Have you filled out the prologue? (Goal: Wednesday; Due: Friday)
  • Have you done your requisite work on the exam? (Due: Tuesday)
  • Have you gone over the contents of your exam using the checklist below? (Due: Tuesday)
  • Have you submitted the code online? (Due: Tuesday)
  • Have you filled out the epilogue? (Due: Tuesday)
  • Have you written a cover sheet and gone over the cover sheet checklist? (Due: Wednesday)
  • Have you printed your examination and gone over the printed exam checklist? (Due: Wednesday)

Exam Contents

  • Have you reviewed the question and answer section of the exam?
  • Have you removed all identifying materials from your code, other than your assigned id number?
  • If you've copied any code, have you also copied the accompanying documentation?
  • Have you cited all the code that you've relied upon?
  • Have you recorded how long each problem took?
  • Have you included examples or test suites for each primary procedure you've written?
  • Have you documented each procedure you've written, including each helper procedure? (Your helper procedures only need the first four P's.)
  • Have you checked your spelling?

Printed Version

  • Did you remove the supplied code from the end of the copy of the exam that you're printing?
  • Did you use a monospace font?
  • Have you written or printed your id on every page?
  • Have you written or printed the page number on every page?
  • Have you looked at the output to make sure that there are no instances in which the code wraps badly?

Cover Sheet

  • Have you written your id on the cover sheet?
  • Have you written your name on the cover sheet?
  • Have you hand written, signed, and dated the first academic honesty statement? (This item assumes you consider it appropriate to sign the first academic honesty statement.)
  • Have you hand written, signed, and dated the second academic honesty statement? (This item assumes you consider it appropriate to sign the second academic honesty statement.)

Problems

Problem 1: Testing Centered Drawings

Topics: Drawings as values, testing

Consider the drawing-center procedure described below.

;;; Procedure:
;;;   drawing-center
;;; Parameters:
;;;   drawing, a drawing
;;;   x, a real number
;;;   y, a real number
;;; Purpose:
;;;   Center drawing at (x,y)
;;; Produces:
;;;   centered, a new drawing
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   The colors, shapes, and relationships between shapes in centered 
;;;     are the same as the colors, shapes, and relationships in
;;;     drawing.  
;;;   (drawing-type centered) = (drawing-type drawing)
;;;   (drawing-width centered) = (drawing-width drawing)
;;;   (drawing-height centered) = (drawing-height drawing)
;;;   (+ (drawing-left centered) (* 1/2 (drawing-width centered))) = x
;;;   (+ (drawing-top centered) (* 1/2 (drawing-height centered))) = y

Write a test suite for the drawing-center procedure. Make sure to test some simple cases, some complex cases, and some cases near the “boundaries” (which we tend to call “edge cases” or “corner cases”). Here are the beginnings of a test suite, with one test.

;;; Name:
;;;   drawing-center-tests
;;; Type:
;;;   test suite
;;; Contents:
;;;   Tests for the drawing-center procedure.
(define drawing-center-tests
  (test-suite
   "Test of drawing-center"
   (test-case "center unit square at (1,1)"
    (let ([centered (drawing-center drawing-unit-square 1 1)])
      (check-equal? (drawing-type centered) 'rectangle)
      (check-equal? (drawing-width centered) 1)
      (check-equal? (drawing-height centered) 1)
      (check-= (drawing-left centered) 0.5 .00001) ; small fudge factor
      (check-= (drawing-top centered) 0.5 .00001) ; small fudge factor
      (check-equal? (drawing-color centered) 
                    (drawing-color drawing-unit-square))))))

While developing the test suite, you can use the following incorrect implementation of drawing-center. (You'll comment it out when you're done.)

(define drawing-center
  (lambda (drawing x y)
    drawing))

Problem 2: Centering Drawings

Topics: Drawings as values

Comment out the incorrect definition of drawing-center and implement it correctly.

Note: You may be tempted to do this problem before you write the unit tests. But we've put the problems in this order for a reason. You will find it much easier to write a correct solution if you write the tests first.

Problem 3: From Images to Drawings

Topics: Drawings as values, documentation, pixels, map

We have seen that it is possible to convert a drawing to an image using the drawing->image procedure. Is it possible to do the reverse? That is, can we convert an image to a corresponding drawing? Perhaps not so surprisingly, the answer is yes. How can we do this conversion? We can grab the color of a pixel at each location and build a small square (or circle, depending on the effect we want) of that color and at that location.

But that's a lot of work for a whole image. It also involves stepping through all of the positions in the image.

Let's suppose that we want to take an image and make a sequence of n ellipses, corresponding to points in the image. What form should that drawing take? It will be a compound drawing (created by drawing-compose or something similar) of n ellipses with the following characteristics.

  • The width of each ellipse is 1/n the width of the image.
  • The height of each ellipse is the height of the image.
  • The left edge of the first ellipse is at 0. The top edge of the first ellipse is at 0.
  • The left edge of each subsequent ellipse is approximately at the right edge of the previous ellipse.
  • The right edge of the final ellipse is (image-width image).
  • The color of each ellipse is determined by the color of the pixel that occurs at the point in the original image that corresponds to the center of the ellipse.

a. Document and write a procedure, (image->ellipses image n), that takes an image and a positive integer as inputs and returns a compound drawing of a sequence of n ellipses as described above.

b. Write, but do not document, a procedure, (image-row->ellipses image n top height) that works similarly to image->ellipses except that the top of each ellipse is at top and the height of each ellipse is height. (You should still sample from the center of the ellipse.)

Hint: You will likely find it useful to write one or more helper procedures to handle common parts of the algorithm.

Note: You should be creating drawings, not images, so do not use the GIMP tools procedures to make the ellipses. (However, you can use image-get-pixel to get the color from the image.)

Here are some examples of these procedures in use.

> (define new-kitten 
    (lambda ()
      (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg"))))
> (define kitten (new-kitten))
> (image->ellipses kitten 1)
'(drawing group ((drawing ellipse 4015701 "" 0.0 0.0 400 300)))
> (image->ellipses kitten 2)
'(drawing 
  group 
  ((drawing ellipse 13685720 "" 0.0 0.0 200 300) 
   (drawing ellipse 11574150 "" 200.0 0.0 200 300)))
> (image->ellipses kitten 4)
'(drawing
  group
  ((drawing ellipse 11316922 "" 0.0 0.0 100 300)
   (drawing ellipse 9013649 "" 100.0 0.0 100 300)
   (drawing ellipse 2696749 "" 200.0 0.0 100 300)
   (drawing ellipse 9144463 "" 300.0 0.0 100 300)))
> (define image1 
    (drawing->image (image->ellipses kitten 40) 400 300))
> (define image2 
    (drawing-render! (image->ellipses kitten 15) (new-kitten)))

> (image-row->ellipses kitten 4 150 25)
'(drawing
  group
  ((drawing ellipse 11251640 "" 0.0 150.0 100 25)
   (drawing ellipse 8619663 "" 100.0 150.0 100 25)
   (drawing ellipse 4737607 "" 200.0 150.0 100 25)
   (drawing ellipse 5654338 "" 300.0 150.0 100 25)))
> (define image3 
    (drawing->image (image-row->ellipses kitten 20 150 25) 400 300))
> (define image4 
    (drawing-render! (image-row->ellipses kitten 20 150 25) (new-kitten)))
(image-show image1)
(image-show image2)
(image-show image3)
(image-show image4)

Problem 4: Generalizing GIMP Graphics

Topics: Procedures, GIMP tools, generalizing code, side effects.

Consider the following set of GIMP code that creates a new image of a “target”.

The following code draws a pretty good target.

(context-set-bgcolor! "white")
(define target (image-new 200 200))
(context-set-fgcolor! "red")
(image-select-ellipse! target REPLACE 0 0 200 200)
(image-select-ellipse! target SUBTRACT 30 30 140 140)
(image-select-ellipse! target ADD 60 60 80 80)
(image-select-ellipse! target SUBTRACT 90 90 20 20)
(image-fill-selection! target)
(image-select-nothing! target)
(image-show target)
;;;  Go ahead and test it to see what it does.

a. Write and document (just the first 4P's) a procedure called image-target! that generalizes the code as much as possible. That is, you must imagine what things someone who calls your procedure might want to vary or customize.

b. Write a short statement explaining why you chose the particular parameters you did and what roles they serve. If appropriate, explain any parameters you considered but chose not to use.

Problem 5: Computing Stripes

Topics: image-compute, conditionals, sectioning, documentation

As you've seen, (image-compute pos->color width height) creates a width-by-height image in which the color of each pixel is determined by applying the procedure pos->color to each pair of (x,y) coordinates in the image.

We've seen how different functions have given us color blends and even shapes. Let's try one more option: Stripes.

Write and document a procedure, (pos->stripes x y width color1 color2 color3) that can be used with image-compute to make an image of alternating stripes of the given colors and specified width.

Here are some examples:
(define red (irgb 255 0 0))
(define black (irgb 0 0 0))
(define grey (irgb 128 128 128))
(image-compute (section pos->stripes <> <> 20 red black grey) 200 100)
(image-compute (section pos->stripes <> <> 15 red black red) 200 100)

For two points of extra credit, write, but do not document, a procedure (pos->stripes-fun x y width fun1 fun2 fun3) that can be used with image-compute to make an image of alternating stripes in which the colors of the stripes are determined by applying fun1, fun2, or fun3 at the given position.

(define red-blue (lambda (x y) (irgb x 0 y)))
(define some-blend (lambda (x y) (irgb y y x)))
(define diagonal (lambda (x y) (irgb 255 (modulo (+ x y) 256) 255)))
(image-compute red-blue 255 255)
(image-compute some-blend 255 255)
(image-compute diagonal 255 255)
(image-compute (section pos->stripes-fun <> <> 20 red-blue some-blend diagonal) 255 255)

Problem 6: Generating Grid Points

Topics: map, lists, anonymous procedures.

Without using recursion, write, but do not document, a procedure, (grid width height) that makes a list of all the (column row) pairs in which column takes on all values from 0 to width-1 and row takes on all values from 0 to height-1.

For example,

> (grid 2 2)
'((0 0) (0 1) (1 0) (1 1))
> (grid 4 1)
'((0 0) (1 0) (2 0) (3 0))
> (grid 4 3)
'((0 0) (0 1) (0 2) (1 0) (1 1) (1 2) (2 0) (2 1) (2 2) (3 0) (3 1) (3 2))

Note: It is acceptable to end up with a nested list, rather than a single list.

> (grid 4 3)
'(((0 0) (0 1) (0 2))
  ((1 0) (1 1) (1 2))
  ((2 0) (2 1) (2 2))
  ((3 0) (3 1) (3 2)))

Hint: You may find it helpful to write a procedure that makes a list of all the points in one row or one column.

Problem 7: Whatzitdo?

Topics: Code reading, Boolean values, local bindings, documentation

A few years ago, students turned in something like the following as a solution to a simple arithmetic problem.

(define f (lambda (a b c) (let*
([p (> a b)] [r (> b c)] [q (> a c)]
[s (not p)]) (or (and p q a) (and r s
b) c)))) 

a. Add carriage returns and indent the code so that it is formatted clearly.

b. Write 6P-style documentation for the code.

c. Explain how the code achieves its purpose.

Some Questions and Answers

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

General Questions

What is a general question?
A question that is about the exam in general, not a particular problem.
Do the two sections have the same exam?
More or less.
Can we still invoke the “There's more to life” clause if we spend more than five hours on the exam?
Yes. However, we really do recommend that you stop at five hours unless you are very close to finishing. It's not worth your time or stress to spend more effort on the exam. It is, however, worth your time to come talk to us, and perhaps to get a tutor or more help (not on this exam, but on the class). There's likely some concept you're missing, and we can help figure that out.
If we get more than 70 points, does the “There's more to life” clause drop our grade to 70?
No! The “There's more to life” clause provides a minimum grade for people who do appropriate amounts and type of work and provide evidence of some mastery.
What do you mean by “implement”?
Write a procedure or procedures that accomplish the given task.
Do we have to make our code concise?
You should strive for readable and correct code. If you can make it concise, that's a plus, but concision is secondary to readability and correctness. Long or muddled code is likely to lose points, even if it is correct.
Much of your sample 6P-style documentation has incomplete sentences. Can we follow that model? That is, can we use incomplete sentences in our 6P-style documentation?
Yes, you can use incomplete sentences in 6P-style documentation.
You tell us to start the exam early, but then you add corrections and questions and answers. Isn't that contradictory? Aren't we better off waiting until you've answered the questions and corrected any errors?
That's one of the reasons we give extra credit to those who work on the exam early. But you're also better able to get your questions answered early if you start early (or at least we think you are). Later questions will generally be told “See the notes on the exam”.
How do we know what our random number is?
You should have received one in class. If you need a new one, there's a stack in the back of our classroom.
To show we’ve tested the code informally, would you just like us to just post the inputs we used to test the procedure? If so, how should we list those?
Copy and paste the interactions pane into the appropriate place in the definitions pane. Select the text. Under the Racket menu, use "Comment out with semicolons."
Should we include examples and, if so, how do we include them?
You should certainly include examples. We would recommend that you copy and paste them from the interactions pane to right below the problem in the definitions pane, and then comment them out with semicolons. (Select and then choose Comment out with semicolons from the Racket menu. Do not use Comment out with a box!
Should we cite our partner from a past lab or assignment if we use code from a past lab or assignment?
You should cite both yourself and your partner, although you should do so as anonymously as possible. For example “Ideas taken from the solution to problem 7 on assignment 3 written by student 641321 and partner”.
What is this STUB comment that appears in the code file?
We typically use the term “STUB” to indicate that we've put in a piece of code to get the program to run, but that the code is intended only as a placeholder until we write something correct.

Problem 1

It looks like many parts of my tests are going to be very similar. Can I write a helper procedure? [2015-10-06]
Certainly. That sounds like a great idea.
How many different kinds of tests did you come up with? [2015-10-06]
SamR came up with about nine different categories of tests. CharlieC came up with a similar number.
Can I start with what you've written, or should I throw it away and start from scratch? [2015-10-07]
You should start with what we've written. However, as the example suggests, each test is pretty long when phrased that way. you may find it easier to rewrite our tests to use a helper.
Are negative x and y permitted? [2015-10-08]
Neither the Parameters section nor the Preconditions section disallow them, so negative centers are permitted.
Can you point me to additional examples or particular resources for thinking more deeply about testing? [2015-10-10]
Not really. You should review the answer key to exam 1. Basically, your goal is to think about a wide range of possibilities. What are the things that can vary? Make sure you check each reasonable thing that you can vary. (See the previous question for one thing to vary.)

Problem 2

What do you mean by “comment out”? [2015-10-07]
Put semicolons in front of the code. Because semicolons represent comments that DrRacket should not pay attention to, DrRacket no longer reads the code. We call such an action “commenting out” the code.

Problem 3

I've written an ellipse procedure for a past lab or homework assignment. Can I use that procedure? [2015-10-06]
Sounds like a good idea to me. Please make sure to cite yourself and your lab partner (as anonymously as possible).
Is it okay if I don't get quite the same colors you got? [2015-10-07]
Yes. It's possible to be a pixel or so off, and that will yield different colors.
Can we use image-get-pixel for this problem? [2015-10-10]
Certainly!

Problem 4

Is it okay if my procedure crashes for some inputs? [2015-10-10]
Yes. But you need to document what the acceptable and unacceptable inputs are. (You'll either do that in the parameters or you'll need to add a preconditions section.)
Is it okay if my procedure changes some aspect of the drawing when the size is other than 200x200? [2015-10-10]
As long as you describe what the different parameters do, the change makes sense, and you can still produce the original image with some set of parameters, it is probably okay.

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. (And no, we don't count errors in the errata section or the question and answer sections.)

  • In problem 3, we included the sentence fragment “... image. four ellipses, corresponding ...”. This fragment has now been fixed. [CL, 1 point]
  • In problem 3, we wrote something clever like “... as input ... as inputs”. [CG, 1 point]
  • In problem 4, we wrote “the server” instead of “they serve”. (Sam had fixed this error, but it did not propagate to Charlie's copy.) [DC, 1 point for CC Students]
  • The dates for the prologue and the epilogue in Sam's version are wrong. (Charlie doesn't have a prologue and epilogue. [AB, 1 point for SR students]
  • In the code file, we misspelled “image” as “iamge”. [EB, 1 point]

Citations

Some of the problems on this exam are based on (and at times copied from) problems on previous exams for the course. Those exams were written by Janet Davis, Rhys Price Jones, Samuel A. Rebelsky, John David Stone, Henry Walker, and Jerod Weinman. Many were written collaboratively, or were themselves based upon prior examinations, so precise credit is difficult, if not impossible.

Some problems on this exam were inspired by conversations with our students and by correct and incorrect student solutions on a variety of problems. We thank our students for that inspiration. Usually, a combination of questions or discussions inspired a problem, so it is difficult and inappropriate to credit individual students.

The photograph of the kitten was released for public use at http://public-photo.net/displayimage-2485.html. It appears that site is now down.