Functional Problem Solving (CSC 151 2016S) : Assignments

Exam 1: Scheme and Color Basics


Assigned: Wednesday, 10 February 2016

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.

Please read the updated instructions for this exam.

Problems

Problem 1: Algorithmic Organization

Topics: Algorithmic thinking, ambiguity, parts of an algorithm

PearCo has been developing a new tool to help you better understand your library of mp3s. (Yes, they assume that people still gather libraries of mp3s rather than using streaming media.) One task that they have been considering is how to provide information on how many different mp3s someone has within a certain genre. (Yes, they know that people seem to gather multiple mp3s of the same song, although they are not sure how or why.)

For each song, they know the title, artist, album, genre, and time. For example,

Venus in Furs,The Velvet Underground,The Velvet Underground and Nico,Proto-Punk,5:15
Gloria,Them,Their Greatest Hits,Rock,2:39
Roadrunner,The Modern Lovers,The Original Modern Lovers,Proto-Punk,4:05
Venus in Furs,The Velvet Underground,The Velvet Underground and Nico,Proto-Punk,5:15
I Can See For Miles,The Who,Meaty Beaty Big and Bouncy,Rock,4:06

As they work on designing an algorithm for the problem, they've decided to focus on a simpler problem: They ask their team to design an algorithm that counts the total number of unique proto-punk songs in the library.

Jordan suggests the following algorithm.

  • Make a list of all of the songs with a genre of Proto-Punk.
  • Put that list in alphabetical order by song title.
  • Remove any elements from the list that are the same as the next song.
  • Count the number of items in the list and return that count.

Taylor suggests the following algorithm.

  • Make a list of all of the songs.
  • Put that list in alphabetical order by song title.
  • Start your count at 0.
  • Look at each element in the list. If its genre is Proto-Punk and it is not the same as the next song, add 1 to the count.
  • Return the total number you counted.

As you may have noted, both algorithms are somewhat ambiguous in that they do not indicate what it means that two songs are “the same”.

a. Identify other places in which the algorithms are somewhat ambiguous and suggest how to make them less ambiguous while achieving the apparent goals of the designer.

b. Identify instances of the use of (i) parameters, (ii) variables, (iii) repetition, (iv) conditionals, (v) sequencing, and (vi) subroutines in these algorithms. (You only need to identify one example of each.)

c. Assume that “the same” means “have the same song title”. Is Jordan's algorithm correct? If not, provide a case in which it will not work. If so, provide a short argument that it does work.

d. Assume that “the same” means “have the same song title”. Is Taylor's algorithm correct? If not, provide a case in which it will not work. If so, provide a short argument that it does work.

e. Assume that “the same” means “the artist, title, album, genre, and time are the same”. Is Jordan's algorithm correct? If not, provide a case in which it will not work. If so, provide a short argument that it does work.

f. Assume that “the same” means “the artist, title, album, genre, and time are the same”. Is Taylor's algorithm correct? If not, provide a case in which it will not work. If so, provide a short argument that it does work.

g. Write an unambiguous algorithm that you are confident will work under both interpretations of “the same”.

Citation: This problem is based on a sample problem for the new AP Computer Science Principles examination.

Problem 2: Whatzitdo?

Topics: Arithmetic operators, code reading, documentation

Sometimes students (and professors) come up with difficult-to-read solutions to arithmetic problems, like the one below.

(define z (lambda 
(w x y) (- 
        (+ w y (* 2 x)) 
    (min w x y) x (max w x y))))

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

b. Rename the procedure and the parameters so that they will make sense to the reader.

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

d. Explain how the code achieves its purpose.

Problem 3: Identifying the Smallest Value

Topics: Algorithmic thinking, arithmetic operators, lambda

Write, but do not document, a procedure (index-of-smallest val1 val2 val3), that takes three positive integers as parameters and returns:

  • 1, if val1 is smaller than the other two values,
  • 2, if val2 is smaller than the other two values,
  • 3, if val3 is smaller than the other two values, and
  • whatever you'd like, if any values are negative or no one value is smallest.
> (index-of-smallest 100 50 0)
3
> (index-of-smallest 100 0 50)
2
> (index-of-smallest 50 100 0)
3
> (index-of-smallest 0 100 50)
1
> (index-of-smallest 0 50 100)
1
> (index-of-smallest 50 0 100)
2

You may not use conditionals (e.g., if) in writing index-of-smallest.

Problem 4: Bounding Colors

Topics: IRGB colors, color transformations, writing procedures with lambda, writing procedures with section and compose.

Consider the following procedure documentation.

;;; Procedure:
;;;   irgb-bound
;;; Parameters:
;;;   color, an integer-encoded RGB color
;;; Purpose:
;;;   Bound the components of color.
;;; Produces:
;;;   bounded, an integer-encoded RGB color
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   If (irgb-red color) < 64, (irgb-red bounded) = 64.
;;;   If (irgb-red color) > 192, (irgb-red bounded) = 192.
;;;   If 64 <= (irgb-red color) <= 192, (irgb-red bounded) = (irgb-red color).
;;;   If (irgb-green color) < 64, (irgb-green bounded) = 64.
;;;   If (irgb-green color) > 192, (irgb-green bounded) = 192.
;;;   If 64 <= (irgb-green color) <= 192, (irgb-green bounded) = (irgb-green color).
;;;   If (irgb-blue color) < 64, (irgb-blue bounded) = 64.
;;;   If (irgb-blue color) > 192, (irgb-blue bounded) = 192.
;;;   If 64 <= (irgb-blue color) <= 192, (irgb-blue bounded) = (irgb-blue color).

a. Implement irgb-bound using lambda but without using section or compose. You may write helper procedures provided you follow the same guidelines for the helper procedures. Call this version irgb-bound-a.

b. Implement irgb-bound without using lambda, max, or min. You will likely need to use section, compose, or both. You may write helper procedures provided you follow the same guidelines for the helper procedures. Call this version irgb-bound-b.

Problem 5: Color Palettes

Topics: RGB Colors, transformations, lambda

Recall that the HSV representation of color (hue, saturation, and value) is an alternative to the RGB representation we typically use in this class. One advantage of HSV is that it more naturally captures the notion of complementary colors. The hue of a color is a number between zero (inclusive) and 360 (exclusive) that is analogous to an angle on a color wheel. Given a color, you can choose other colors with the same saturation and value but different hues evenly spaced around the color wheel to produce a color palette.

Write and document a procedure (color-palette color) that takes an integer-encoded RGB parameter color. This procedure then produces a palette of three colors using the color-swatch function. The palette should include the color provided as a parameter, along with two other colors spaced evenly around the color wheel from the parameter.

(image-show (color-palette (color-name->irgb "steelblue")))
(image-show (color-palette (color-name->irgb "tan")))
(image-show (color-palette (color-name->irgb "peachpuff")))

Problem 6: Whatzitdo?

Topics: Integer-encoded RGB colors, color transformations, code reading, documentation

Consider the following interesting color transformation.

(define t
(lambda (color
  string
  number)
        (irgb-add (irgb (* color (irgb-red string))
                  (* color (irgb-green string))
                  (* color (irgb-blue string)))
                  (irgb (* (- 1 color 
                        )(irgb-red number))
                  (* (- 1 
                  color) (irgb-green number))
                  (* (- 1
                  color) (irgb-blue number))))))

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

b. Rename the procedure and the parameters so that they will make sense to the reader.

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

d. 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.
I know that you prefer that lines be under 80 characters. But what if I'm citing a Web page and the URL is longer than 80 characters?
In that particular case, it's fine that the line is longer than 80 characters.

Problem 3

Can I assume that the inputs are only positive integers?
Yes. We have now updated the problem to clarify that issue.
What if I already solved it for arbitrary real numbers?
Then you may receive a bit of extra credit, at the discretion of the grader.

Problem 4

Any hints on 4b?
Think about solving it with the various integer-encoded RGB color transformations we've been using, rather than doing something arithmetic.

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. (Sam's record is nine points of extra credit; Charlie's is about five.) Note that we do not count errors in the errata section or the question and answer sections.

  • Integer-encoded” spelled as “Intger-encoded”. [WK, 1 point]
  • On problem 6, “-1” should be “- 1”. [JL, 1 point]
  • On problem 1, part f should have asked about Taylor's algorithm. [BW, 1 point]
  • On problem 1, the song by The Who was missing a genre. [OM, AG, 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.