Assigned: Wednesday, 27 April 2011
Due: Beginning of class, Wednesday, 4 May 2011
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 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 if you give evidence of a serious attempt on at least 6 of the problems or come talk to me about the exam no later than one week after it is returned.
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. 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 top-level (i.e., not local) 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. Because 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: Vectors, numeric recursion.
Document and write a
procedure, ( that rotates the elements in
vector-rotate-1!
vec)vec to the "left". That
is, vector-rotate-1! puts the initial element
of vec at the end, the element at position 1
in position 0, the element at position 2 in position 1, and so on.
For example,
>(define vec1 (vector 'a 'b 'c 'd 'e))>(define vec2 (list->vector (iota 8)))>(vector-rotate-1! vec1)>vec1#(b c d e a)>vec2#(0 1 2 3 4 5 6 7)>(vector-rotate-1! vec2)>(vector-rotate-1! vec2)>vec2#(2 3 4 5 6 7 0 1)
Topics: Vectors, code reuse, higher-order procedures
Write, but do not document, a
procedure, ( that
rotates the elements in vector-rotate!
vec n)vec to the "left"
by n places.
>(define vec1 (vector 'a 'b 'c 'd 'e))>(vector-rotate! vec1 3)>vec1#(d e a b c)
Topics: Higher-order procedures, numeric recursion.
While repeat works well for using the same
procedure repeatedly in an imperative model, it does not work well
in the functional model. Some of you have observed that it is
tedious to use the procedure
o to compose the same function with itself several
times. For example, it is tedious to write
(image-variant picture
(o rgb-darker rgb-darker rgb-darker rgb-darker rgb-darker))
to apply the rgb-darker function five times to each
pixel in an image.
Document and write a new procedure, nest, as follows.
nest takes two parameters, a unary
procedure f and an integer
n. The value produced is a new procedure that
results from composing together n copies of
f.
Note that n must be at least 1 for
nest to make sense. Verify this
precondition using husk-and-kernel style.
Here are some examples using nest.
>(define plus5 (nest (l-s + 1) 5))>(plus5 6)11>(define list5 (nest list 5))>(list5 6)(((((6)))))>(nest list 0)n must be at least 1, given 0>(define duplicate (lambda (val n) ((nest (l-s cons val) n) null)))>(duplicate "hello" 5)("hello" "hello" "hello" "hello" "hello")
Topics: Strings, higher-order procedures.
Document and implement the function
(, which produces a new string in which
string-map proc
str)proc has been applied to every character in the
string str.
For example:
>(string-map char-upcase "Banana")"BANANA">(string-map char-downcase "Banana")"banana">(string-map (o integer->char increment char->integer) "Banana")"Cbobob">(string-map (o integer->char increment char->integer) "Apple")"Bqqmf">(string-map (o integer->char (l-s + 65) (l-s - 90) char->integer char-upcase) "Banana")"YZMZMZ"
Hint for documentation:
proc is a procedure. What are the types of its parameter
and its value produced?
Topics: Strings, characters, higher-order procedures, conciseness.
Write, but do not document, a procedure
( that transforms every upper-case letter
in string-invert-case
str)str into a lower-case letter, and vice versa.
Make your definition as concise as possible.
For example:
>(string-invert-case "Banana")"bANANA">(string-invert-case "Banana?!")"bANANA?!">(string-invert-case "davisjan@cs.GRINNELL.edu")"DAVISJAN@CS.grinnell.EDU">(string-invert-case "This Is Silly")"tHIS iS sILLY"
Topics:
Strings, characters, higher-order procedures, apply.
Inspired by: John Motil's BookNumber assignment
The International Book Standard Number (ISBN) is a special code used to identify books. An ISBN consists of nine digits followed by a tenth check character. This check character serves to detect errors if the ISBN is accidentally misread or mistyped.
Write a procedure, (, according to the following documentation.
isbn-check-character
isbn)
;;; Procedure: ;;; isbn-check-character ;;; Parameter: ;;; isbn, a string ;;; Produces: ;;; check-char, a character ;;; Purpose: ;;; To compute the ISBN check character for isbn. ;;; Preconditions: ;;; (string-length isbn) is 9. ;;; isbn contains only characters representing digits, #\0 - #\9. ;;; Postconditions: ;;; check-char is either #\X or a character representing ;;; a digit, #\0 through #\9. ;;; check-char is computed via ISBN check algorithm below. ;;; ;;; Process: ;;; The following algorithm is used to compute the check character. ;;; 1. Compute the sum of 1 times the first digit, two times the ;;; second digit, three times the third digit, and so on up to ;;; 9 times the ninth digit. ;;; 2. Compute the remainder of this sum divided by 11. ;;; 3. If the remainder is 10, then the check character is #\X. ;;; Otherwise, the check character is a digit, #\0 - #\9, ;;; representing the remainder. ;;; ;;; Practicum: ;;; > (isbn-check-character "123456789") ;;; #\X ;;; > (isbn-check-character "020508005") ;;; #\7
Hint 1: The digit #\0 corresponds
to the integer 48. You can convert a string of digits into a list of
equivalent numbers using
(map (o (r-s - 48) char->integer) (string->list str)).
Hint 2: Break the problem down into smaller steps. Implement and test one step at a time.
Topics: Trees, predicates, verifying preconditions, reading documentation.
In the reading on trees, you learned how a tree can be flattened into a list structure with the following procedure.
;;; Procedure:
;;; tree-flatten
;;; Parameters:
;;; tree, a tree
;;; Purpose:
;;; Convert a tree structure into a similar list structure.
;;; Produces:
;;; lst, a list
;;; Preconditions:
;;; none
;;; Postconditions:
;;; lst is a simple list (that is, it contains no sublists).
;;; Each value that appears in lst appears in tree.
;;; Each value that appears in tree appears in lst.
;;; If a precedes b in tree, then a precedes b in lst.
(define tree-flatten
(lambda (tree)
(if (pair? tree)
(append (tree-flatten (car tree))
(tree-flatten (cdr tree)))
(list tree))))
As you saw in the reading on searching, it can be useful to determine whether a vector is sorted. Lists, like vectors, are simply an ordered collection of items. We could therefore ask whether the list containing a flattened tree is sorted as a means of asking whether a tree is sorted. This test of a "sorted tree" seems somewhat indirect, and its implementation strikes us as inefficient. Is there a better way to define and test for a sorted tree? As you might suppose, there is.
Let us consider the cases from tree-flatten. A
leaf is necessarily sorted. What does it mean for a pair to
represent a sorted tree? First, both the left and right subtrees
would certainly need to be sorted. We must also verify that when
placed against one another (as they are
with append above), these two flattened
subtrees represent a correct ordering. For that to be true, the last
element of the left subtree, which represents the largest item in
that subtree, must precede the first item in the right subtree.
Using this definition of a sorted tree, implement the following
number-tree-sorted?.
Here are several examples using
number-tree-sorted?.
>(define ntree1 (cons (cons 1 (cons 2 3)) 4))>(define ntree2 (cons (cons 5 (cons 6 3)) 7))>(tree-flatten ntree1)(1 2 3 4)>(number-tree-sorted? ntree1)(1 . 4)>(tree-flatten ntree2)(5 6 3 7)>(number-tree-sorted? ntree2)#f>(number-tree-sorted? 4)(4 . 4)>(number-tree-sorted? (cons 1 #\A))number-tree-sorted?: #\A is not a number
Here are documentation and the beginning of the procedure definition to get you started.
;;; Procedure:
;;; number-tree-sorted?
;;; Parameters:
;;; ntree, a number tree [Unverified.]
;;; Purpose:
;;; Determine whether a number tree is sorted
;;; Produces:
;;; extremes, a pair of numbers or the value #f
;;; Preconditions:
;;; [No additional.]
;;; Postconditions:
;;; If ntree is a leaf, or
;;; if ntree is a pair, its left and right subtrees are both sorted,
;;; and the largest value in the left subtree is no greater than
;;; the smallest value in the right subtree,
;;; then extremes is a pair whose car is the smallest value
;;; in ntree and whose cdr is the largest value
;;; in ntree.
;;; Otherwise, result is #f.
(define number-tree-sorted?
(lambda (ntree)
(if (pair? ntree)
(let ((left-sorted (number-tree-sorted? (car ntree)))
(right-sorted (number-tree-sorted? (cdr ntree))))
...
Hints:
Note the return value provides useful
information about subtrees in addition to whether or not they are
sorted. As we suggest in the skeleton code above,
use let to store the values produced by
recursive calls to the predicate on the left and right subtrees,
respectively. If the subtrees are not sorted, how will you know?
What should you return? If the subtrees are sorted, what do these
stored values tell you? How can you use that to determine whether
the given tree is also sorted? Also: what value should be produced
for a leaf?
Topics: Searching, list recursion.
The built-in version of assoc returns the first
element of the association list whose car matches the key. Write,
but do not document, an alternate
version, (, that returns
the last element of the association list
whose car matches the key or returns assoc-last key
alist)#f if no match is found.
Topics:
Analyzing procedures, searching, assoc.
Below we rename to my-assoc the implementation
of assoc provided in an earlier reading:
(define my-assoc
(lambda (key alist)
(cond
; If there are no entries left in the association list,
; there are no entries with the given key.
((null? alist)
#f)
; If the key we're looking for is the key of the first
; entry, then use that entry.
((equal? key (car (car alist)))
(car alist))
; Otherwise, look in the rest of the association list.
(else
(my-assoc key (cdr alist))))))
In this problem, you will use define$ and
analyze to count the number of steps taken by this
implementation, and then you will interpret the results. Recall
that computer scientists analyze how many steps it takes to run an
algorithm based on the size of its parameters. That is, if an
algorithm is given a list of length
N,
we might want to know that that algorithm takes 4N+3,
or N(N-1)/2, or
log2(N) steps.
(a) Consider calling the procedure my-assoc on an
association list of length N. How many recursive
calls to the procedure are required when the search is unsuccessful?
Express the number of calls to my-assoc in terms
of N.
(b) How many recursive calls to the
procedure my-assoc are required on average to
search a list of length 4 when the search is successful? That is,
consider how many steps it takes to search for (and find) the first,
second, third, and fourth items in the list, respectively; report
the average of these four numbers.
(c) How many recursive calls to the
procedure my-assoc are required on average to
search a list of length 8 when the search is successful?
(d) How many recursive calls to the
procedure my-assoc are required on average to
search a list of length N when the search is
successful? Express the number of calls
to my-assoc in terms of N.
Topics: Unit testing, binary search.
At the beginning of the lab on binary search, you informally evaluated the correctness of a binary search procedure. In this problem, we ask you to test this procedure formally and systematically.
The following implementation of binary search has a subtle bug.
;;; Procedure:
;;; binary-search
;;; Parameters:
;;; vec, a vector to search
;;; get-key, a procedure of one parameter that, given a data item,
;;; returns the key of a data item
;;; may-precede?, a binary predicate that tells us whether or not
;;; one key may precede another
;;; key, a key we're looking for
;;; Purpose:
;;; Search vec for a value whose key matches key.
;;; Produces:
;;; match, a number.
;;; Preconditions:
;;; The vector is "sorted". That is,
;;; (may-precede? (get-key (vector-ref vec i))
;;; (get-key (vector-ref vec (+ i 1))))
;;; holds for all reasonable i.
;;; The get-key procedure can be applied to all values in the vector.
;;; The may-precede? procedure can be applied to all pairs of keys
;;; in the vector (and to the supplied key).
;;; The may-precede? procedure is transitive. That is, if
;;; (may-precede? a b) and (may-precede? b c) then it must
;;; be that (may-precede? a c).
;;; If two values are equal, then each may precede the other.
;;; Similarly, if two values may each precede the other, then
;;; the two values are equal.
;;; Postconditions:
;;; If vector contains no element whose key matches key, match is -1.
;;; If vec contains an element whose key equals key, match is the
;;; index of one such value. That is, key is
;;; (get-key (vector-ref vec match))
(define binary-search
(lambda (vec get-key may-precede? key)
; Search a portion of the vector from lower-bound to upper-bound
(let search-portion ((lower-bound 0)
(upper-bound (- (vector-length vec) 1)))
(if (>= lower-bound upper-bound)
; Did we find it?
(if (equal? key (get-key (vector-ref vec lower-bound)))
lower-bound ; Yes!
-1) ; No!
; Keep searching
(let ((midpoint (+ lower-bound (quotient (- upper-bound lower-bound) 2))))
(if (may-precede? key (get-key (vector-ref vec midpoint)))
(search-portion lower-bound (- midpoint 1))
(search-portion (+ midpoint 1) upper-bound)))))))
Write a test suite, using the MediaScheme unit testing tools, to test for errors in an implementation of binary search. Your tests should all pass for the correct version of binary search from the lab; some should fail for the buggy version of binary search given above. Comment your tests to explain the purpose of each one.
Hints: In your test suite, you should test that a correct result is given
In addition to writing tests, you will need to create vectors that will be searched in your tests. You will need to create at least two different vectors and write at least ten test cases in order to cover all possible combinations of conditions.
Here is a vector and two tests to start you off:
(define test-vector
(vector (cons 2 "two")
(cons 4 "four")
(cons 6 "six")
(cons 8 "eight")
(cons 10 "ten")))
; Search for key in position 0
(test! (binary-search test-vector car <= 2) 0)
; Search for non-existent key that would appear before position 0
(test! (binary-search test-vector car <= 1) -1)
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.
Copyright © 2007-2011 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
.