Assigned: Wednesday, November 28
Due: Tuesday, December 4, 10:30 p.m.
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 be able to complete the exam in a reasonable amount of time. In particular, this exam is likely to take you about three to four 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 if you give evidence of a serious attempt on at least 6 of the problems.
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.
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.
While your electronic version is due Tuesday night, 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 be considered a misrepresentation of your work and referred to the Committee on Academic Standing.
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. You should use examples other than those that may be provided with the exam. 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: List recursion, higher-order procedures, encapsulating control
As you may recall, we began our investigation of recursion by considering how we might step through a list of numbers, adding them (or multiplying them or ...). Here are variants of the two versions we wrote:
One uses simple recursion.
(define sum-right (lambda (numbers) (if (null? (cdr numbers)) (car numbers) (+ (car numbers) (sum-right (cdr numbers))))))
The other uses a helper to accumulate the results.
(define sum-left (lambda (numbers) (let kernel ((so-far (car numbers)) (remaining (cdr numbers))) (if (null? remaining) so-far (kernel (+ so-far (car remaining)) (cdr remaining))))))
If you think carefully about it, the two processes compute results
slightly differently. Given the list (n0 n1 n2 n3 n4)
,
the first computes
(+ n0 (+ n1 (+ n2 (+ n3 n4))))
while the second computes
(+ (+ (+ (+ n0 n1) n2) n3) n4)
For addition, this doesn't make a difference. If the operation were subtraction, it would certainly make a difference. (Rimshot!)
The process of combining a list of values using a binary procedure
is quite common. We've seen it for addition, subtraction, and
multiplication.
We might also use it for appending strings and many other operations.
Computer scientists have various names for such a process. We'll call
it fold
, but others use inject
(as in “inject this
operation into this list”)
or reduce
(as in, “reduce this list to a
single value”).
Write, but do not document, a procedure
(
that mimics
fold-right
proc
lst
)sum-right
, but with proc
in place of +
. That is, (fold-right
should
compute
proc (list v0 v1 v2 ... vn-1 vn))
(proc v0 (proc v1 (proc v2 (... (proc vn-1 vn) ... ))))
For example,
>
(fold-right + (list 1 2 3 4))
10
>
(fold-right * (list 1 2 3 4))
24
>
(fold-right - (list 1 2 3 4))
-2
>
(fold-right (lambda (s1 s2) (string-append s1 ", " s2)) (list "sentient" "malicious" "powerful" "stupid"))
"sentient, malicious, powerful, stupid"
>
(fold-right list (list 'a 'b 'c 'd 'e))
(a (b (c (d e))))
Write, but do not document, a procedure (
that mimics
fold-left
proc
lst
)sum-left
, but with proc
in place of
+
. That is,
(fold-left
should compute
proc (list v0 v1 v2 ... vn-1 vn))
(proc (proc ... (proc (proc v0 v1) v2) ... vn-1) vn)
For example,
>
(fold-left + (list 1 2 3 4))
10
>
(fold-left * (list 1 2 3 4))
24
>
(fold-left - (list 1 2 3 4))
-8
>
(fold-left (lambda (s1 s2) (string-append s1 ", " s2)) (list "sentient" "malicious" "powerful" "stupid"))
"sentient, malicious, powerful, stupid"
>
(fold-left list (list 'a 'b 'c 'd 'e))
((((a b) c) d) e)
Topics: Characters, strings, higher-order procedures
A substitution cipher is a code that simply replaces one character with another in a one-to-one fashion. This is the kind of code that must be broken when solving the typical newspaper puzzle called a "cryptogram." A special case of this code is called a Caesar cipher, named for the Roman emperor Julius Caesar, who used it to communicate with his generals. A Caesar cipher simply shifts each character to another a certain number of places down the alphabet. In a Caesar cipher with a shift of 3, the letter A (character 0) would become D (character 3), P (character 15) would become S (character 18), and so on. Typically, Y (character 24) would wrap-around to the front of the alphabet to become B (character 27, or, actually character 1).
Write a procedure
(
,
which produces a procedure that takes a character and shifts it
by char-shifter
amount
)amount
, accounting for any wrap-around.
Your procedure only needs to work for upper-case letters.
You should strive to make your procedure as concise as possible.
>
(define char-encode-1 (char-shifter 5))
>
(char-encode-1 #\H)
#\M
>
(char-encode-1 #\Z)
#\E
Of course, Caesar ciphers are much more interesting when applied to strings, rather than to individual characters. It is also natural to think of a cipher as a function from strings to strings, rather than from numbers to strings. Hence, we'd like a procedure that builds ciphers.
Write, but do not document, a procedure
(
, which returns a procedure
that takes a string as input and produces a string where each character is
replaced by another whose collating sequence number differs from
the original by shift-encoder
amount
)amount
, accounting for
wrap-around. Again, your solution only needs to work for strings
consisting of upper-case letters.
You should strive to make your procedure as concise as possible.
>
(define encode-1 (shift-encoder 5))
>
(encode-1 "HELLO")
"MJQQT"
>
(define encode-2 (shift-encoder 2))
>
(encode-2 "MJQQT")
"OLSSV"
>
(define decode-12 (shift-encoder -7))
>
(decode-12 "OLSSV")
"HELLO"
>
((o decode-12 encode-2 encode-1) "WORLD")
"WORLD"
Before we have confidence in our cipher, we ought to test it thoroughly. Building a test suite involves writing a set of inputs (and outputs or errors) that would convince someone the code is completely correct. Or, as the reading on unit tests puts it, "a collection of tests that are unlikely to all succeed if the code being tested is erroneous."
Using begin-tests!
,
end-tests!
, test!
, and
test-error!
, write a test suite for your
char-shifter
procedure.
Note: You should include a comment with each test indicating its purpose. For example:
; Known equilateral triangle (test! (classify-triangle 1 1 1) "equilateral") ; Impossible triangle: hypotenuse exceeds sum of other two sides (test-error! (classify-triangle 1 1 3))
Topics: Trees, deep recursion, pairs
You should be familiar with the reverse
procedure,
which, given a list, creates a new list of the same length and the same
elements, but with the elements in the reverse order.
Write, but do not document, a procedure,
(
,
that “reverses” a tree by reversing the two subtrees and
then tree-reverse
tree
)cons
-ing them together in reverse order.
>
(tree-reverse 'a)
a
>
(tree-reverse (cons 'a 'b))
(b . a)
>
(tree-reverse (cons 'a (cons 'b 'c)))
((c . b) . a)
>
(tree-reverse (cons (cons 'a 'b) 'c))
(c b . a)
>
(tree-reverse (cons 'c (cons 'b 'a)))
((a . b) . c)
>
(tree-reverse (cons (cons 'a 'b) (cons 'c 'd)))
((d . c) b . a)
assoc
for repeated keys
Topics: Sequential search, assoc
,
list recursion
As you may have noted, one potential deficiency of assoc
is that when multiple entries contain the same key,
assoc
returns only the first entry with a particular
key.
Write, but do not document, a procedure, (
, that
makes a list of all the values in
assoc-all
key
lst
)lst
whose car is key
.
You need not check the preconditions for this procedure.
Note that assoc-all
should return the empty list if
no entry contains the desired key.
For example,
(define people (list (list "Adams" "Amy" "Architecture") (list "Jones" "Jack" "Geography") (list "Jones" "Jane" "Geology") (list "Smith" "Sarah" "Science") (list "Jones" "Jenna" "Genetics")))
>
(assoc-all "Jones" people)
(("Jones" "Jack" "Geography") ("Jones" "Jane" "Geology") ("Jones" "Jenna" "Genetics"))
>
(assoc-all "Smith" people)
(("Smith" "Sarah" "Science"))
>
(assoc-all "Davis" people)
()
assoc-all
Write the six-P documentation for assoc-all
.
Topics: Vectors, numeric recursion, randomness
You have seen how your instructors use the
random
procedure to generate lab partners for
the class. This process essentially begins by coming up with an
unpredictable ordering for all the students in the class. To make
that task easier, we will break the problem into two steps.
(a) Write the procedure (
that transposes (swaps) the
items at position vector-transpose!
vec
index-0
index-1
)index-0
and
index-1
.
>
(define animals (vector 'avocet 'bongo 'cuttlefish 'dugong 'echidna 'fossa))
>
(vector-transpose! animals 0 3)
>
animals
#(dugong bongo cuttlefish avocet echidna fossa)
>
(vector-transpose! animals 4 2)
>
animals
#(dugong bongo echidna avocet cuttlefish fossa)
(b) Armed with the ability to transpose vector entries, randomly re-ordering the entries in a vector is accomplished with the following algorithm: Start with the last index in the vector as the "current" index. Choose a random index no greater than (but possibly equal to) the current index. Transpose the items at the current and randomly chosen indices. Repeat these steps for the next lower index, continuing until all positions have been exhausted.
Write, but do not document, the procedure (
that implements the algorithm described above to shuffle the items of vector-permute!
vec
)vec
in an unpredictable manner.
Euler and Galois had lunch in Grinnell last week, and both were intrigued by the Stir Fry counter. On this particular day, one could select from a choice of five vegetables: onions, mushrooms, beansprouts, scallions, and radishes.
"I wonder how many different ways there are to select three different veggies from this set of five," said Euler.
Being a mathematician, Galois responded: "I'd be more interested in the general question of how many ways there are to select k different veggies from a set of n offerings." He continued in this thought and invented a notation. "Let's call it C(n,k)," he said, "C(n,k) is the number of ways to choose k veggies out of a set of n."
Euler, anticipating our conventions for CSC 151, said "I'd prefer to think like this." And he took out of his pocket an old envelope, on the back of which he wrote:
(define combinations (lambda (n k)
Galois said: "There are some easy cases: If n is 0 or 1 then there is basically no choice. The number of combinations is just 1."
And Euler contributed: "And if k is 0, it's easy. There is exactly one way to pick 0 vegetables out of a choice of n, no matter the value of n."
To which Galois added: "If k happens to be n, there is also no problem. We must pick all of the veggies, so the number of options is also 1."
Euler said: "That's an awful lot of base cases. It's a good thing I took CSC151 at Grinnell and know how to code recursive procedures with base cases! But how about the recursive case(s)? How can we solve that?"
"Hmm," said Galois, "let me see: If one of the veggies is, say, onions and
we insist that we will always pick onions among our k choices, then there
will be exactly (combinations (- n 1) (- k 1))
to choose the
other k-1 veggies."
"Right!" said Euler. "And if we want to count the number of combinations
that do not include onions, then there are exactly
(combinations (- n 1) k)
ways to do that. Because without
onions there are only n-1 veggies to choose from, and we still need to pick
k veggies.
After following the above conversation, please complete the
definition and documented postconditions of procedure
combinations
;;; Procedure: ;;; combinations ;;; Parameters: ;;; n, an integer ;;; k, an integer ;;; Purpose: ;;; Find the number of ways to select k items from a set of n distinct items ;;; Produces: ;;; count, an integer ;;; Process: ;;; There are several possible base cases ;;; which occur when n or k is such that there is really no choice ;;; Otherwise we need to add together ;;; the number of selections that do contain a specific object ("onions"), ;;; and the number of selections that do not contain that specific object ;;; Preconditions: ;;; n >= k >= 0 ;;; Postconditions: ;;; ...? (define combinations (lambda (n k) ; Base case tests: (if (or (zero? n) ; n is zero: no choice ______ ______ ______) ____ ; Base case value (+ _____________________________ _____________________________))))
For example,
>
(combinations 5 3)
10
>
(combinations 4 2)
6
>
(combinations 8 3)
56
(a) Change your define combinations
from question 9
to define$
combinations
. Then, use the tools from
the lab on
analysis to analyze the number of times
combinations
is called
in evaluating each of the expressions above. How many times is
combinations
called when you invoke (combinations 20
5)
?
(b) Do some more analysis of (combinations ...)
and write a short
paragraph
describing your observations. Why is this or why is it not a good
program?
Optional Extra Credit (c) Euler observed to Galois: "Here's another way to think about our stir fry lunch. For each choice of veggy: onions, mushrooms, beansprouts, scallions, and radishes; all we have to do to get a selection of three veggies is to pick another two veggies out of the remaining four. Now apply that to the general problem with n and k: For each choice of one of the n veggies it remains to pick k-1 out of the remaining n-1. Doesn't this suggest that C(n,k) = n * C(n-1,k-1)?" So Euler wrote the following code:
;;; Procedure: ;;; combinations2 ;;; Parameters: ;;; n, an integer ;;; k, an integer ;;; Purpose: ;;; Find the number of ways to select k items from a set of n distinct items ;;; Process: ;;; This code contains an error. It is based on Euler's mistaken observation ;;; that the number of combinations of k items from a set of n is going to ;;; be n times the number of combinations of k-1 items from a set of size ;;; n-1 ;;; Base cases occur when n or k is 0 ;;; Produces: ;;; count, an integer ;;; Preconditions: ;;; n >= k >= 0 ;;; Postconditions: ;;; ...? (define combinations2 ;;; This code is incorrect (lambda (n k) (cond ((zero? (* n k)) 1) (else (* n (combinations2 (- n 1) (- k 1)))))))
"Almost," said Galois. "But you're overcounting if you do that. Each combination of k veggies gets counted several times. To get the correct recurrence you need to modify your recurrence C(n,k) = n * C(n-1,k-1) to take into account that each combination is counted that many times on the right hand side. You need to divide the right hand side by the number of times each combination is counted to obtain the correct recurrence."
Galois is right. Confirm that the code given above gives wrong
answers. For extra credit, correct the code for
combinations2
. Then change the define
to
define$
and perform an analysis of how well the new code
performs. Finally, write a paragraph based on your analyses
explaining which of combinations
or
combinations2
you prefer and why.
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.
vector-transpose!
function was missing parameter vec
[LMS, 1point]
vector-transpose!
function was misspelled. [JMS, 1point]
Copyright © 2007-2012 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)
This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License .