CSC151.01 2009F Functional Problem Solving : Exams
Primary: [Front Door] [Schedule] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] - [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings]
References: [A-Z] [By Topic] - [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.02 2009F (Weinman)] [CSC151.02 2009S (Davis)] [CSC151 2008S (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Assigned: Monday, 30 November 2009
Due: Beginning of class, Tuesday, 8 December 2009
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 ten points, for a total of one hundred 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 is more to life than CS” and you will earn at least 70 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 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 Rebelsky (that's me) or 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.
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 state otherwise, you should document any procedures you write. Your documentation should use 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 (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).
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.
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.
assoc-all
Topics: Documentation, Preconditions
Write the six-P documentation for assoc-all
.
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)
>
(cons 'c (cons 'b 'a))
(c b . a)
>
(tree-reverse (cons (cons 'a 'b) (cons 'c 'd)))
((d . c) b . a)
Topics: Vector mutation, Recursion in vectors
Write, but do not document, a procedure, (
,
that reverses a vector in place, swapping the first and
last elements, the second and penultimate elements, and so on and so forth.
vector-reverse!
vec
)
>
(define vec1 (vector 'a 'b 'c 'd 'e))
>
(define vec2 (list->vector (iota 8)))
>
(vector-reverse! vec1)
>
vec1
#(e d c b a)
>
(vector-reverse! vec2)
>
vec2
#(7 6 5 4 3 2 1 0)
>
(vector-reverse! vec2)
>
vec2
#(0 1 2 3 4 5 6 7)
You may not use the list-based reverse
procedure
in solving this problem.
Topics: Divide-and-conquer, Numeric recursion
We learned the divide-and-conquer strategy when studying binary search. We have also applied that strategy to the problem of sorting. Let's consider one other instance in which divide-and-conquer may help: computing cube roots.
To compute the cube root of a number, n
, we start with two
estimates, one that we know is lower than the cube root and one that we
know is higher than the cube root. We repeatedly find the average of
those two numbers and refine our guess. We stop when the cube of
the average is “close enough” to n
.
What should we start as the lower-bound and upper-bound of the cube root? Well, if n is at least 1, we can use 0 as the lower-bound and n as the upper bound.
For example, our procedure produces the following sequence of guesses to find the cube root of 2 with an accuracy of 3 decimal places.
>
(cube-root 2.0)
(lower: 0.0 upper: 2.0 avg: 1.0 avg-cubed: 1.0) (lower: 1.0 upper: 2.0 avg: 1.5 avg-cubed: 3.375) (lower: 1.0 upper: 1.5 avg: 1.25 avg-cubed: 1.953125) (lower: 1.25 upper: 1.5 avg: 1.375 avg-cubed: 2.599609375) (lower: 1.25 upper: 1.375 avg: 1.3125 avg-cubed: 2.260986328) (lower: 1.25 upper: 1.3125 avg: 1.28125 avg-cubed: 2.103302002) (lower: 1.25 upper: 1.28125 avg: 1.265625 avg-cubed: 2.02728653) (lower: 1.25 upper: 1.265625 avg: 1.2578125 avg-cubed: 1.989975452) (lower: 1.2578125 upper: 1.265625 avg: 1.26171875 avg-cubed: 2.008573234) (lower: 1.2578125 upper: 1.26171875 avg: 1.259765625 avg-cubed: 1.999259926)
1.259765625
Write, but do not document a procedure, (
, which should approximate the cube
root of cube-root
n
)n
to three decimal places of accuracy. That
is, the difference between n
and the cube of
(
should be less than 0.001. You can assume that cube-root
n
)n
is at least one.
Topics: Strings, Testing, Common patterns of recursion
Consider the following procedure specification.
;;; Procedure: ;;; alphabetically-first ;;; Parameters: ;;; strings, a non-empty list of strings ;;; Purpose: ;;; Finds the alphabetically first string in strings ;;; Produces: ;;; first, a string ;;; Preconditions: ;;; [No additional] ;;; Postconditions: ;;; first is an element of strings ;;; For each string, str, in strings ;;; (string-ci<=? first str)
Write a comprehensive set of unit tests for
alphabetically-first
. You should be confident
that any implementation of alphabetically-first
that passes the tests is correct.
Here is an incorrect implementation of
alphabetically-first
to help in your testing.
(define alphabetically-first (lambda (strings) (cond ((null? (cdr strings)) (car strings)) ((string-ci<=? (car strings) (cadr strings)) (car strings)) (else (alphabetically-first (cdr strings))))))
Note that this implementation seems to work correctly for some obvious tests.
(begin-tests!) ; Check a one-element list (test! (alphabetically-first (list "alphabet")) "alphabet") ; Check two-element lists (test! (alphabetically-first (list "alphabet" "soup")) "alphabet") (test! (alphabetically-first (list "soup" "alphabet"))"alphabet") ; Check a few multi-element lists (test! (alphabetically-first (list "alphabet" "soup" "tastes" "good")) "alphabet") (test! (alphabetically-first (list "somebody" "likes" "alphabet" "soup")) "alphabet") (test! (alphabetically-first (list "somebody" "likes" "squash" "soup")) "likes") (end-tests!)
6 tests conducted. 6 tests succeeded. No tests failed. CONGRATULATIONS! All tests passed.
Topics: Sorting, Vectors
The famous selection sort algorithm for sorting vectors works by repeatedly stepping through the vector from back to front, swapping the largest remaining thing to the next place in the vector. We might express that algorithm for a vector of strings as
(define string-vector-selection-sort! (lambda (strings) (let kernel ((pos (- (vector-length strings) 1))) (if (< pos 0) strings (let* ((index (string-vector-index-of-largest strings pos)) (largest (vector-ref strings index))) (vector-set! strings index (vector-ref strings pos)) (vector-set! strings pos largest) (kernel (- pos 1)))))))
Of course, we will need the procedure
string-vector-index-of-largest
, which
we might document as
;;; Procedure: ;;; string-vector-index-of-largest ;;; Parameters: ;;; strings, a vector of strings ;;; pos, an integer ;;; Purpose: ;;; Find the index of the alphabetically last string in the ;;; subvector of strings at positions [0..pos]. ;;; Produces: ;;; index, an integer ;;; Preconditions: ;;; pos > 0. ;;; (vector-length strings) > pos. ;;; Postconditions: ;;; For all i from 0 to pos, inclusive, ;;; (string-ci>=? (vector-ref strings index) (vector-ref strings i))
For example,
>
(define claim (vector "computers" "are" "sentient" "and" "malicious" "on" "tv"))
>
(string-vector-index-of-largest claim 6)
6
>
(vector-ref claim 6)
"tv"
>
(string-vector-index-of-largest claim 5)
2
>
(vector-ref claim 2)
"sentient"
>
(string-vector-selection-sort! claim)
#("and" "are" "computers" "malicious" "on" "sentient" "tv")
Implement the string-vector-index-of-largest
procedure.
Topics: Binary search, Divide-and-conquer, Strings
Suppose that a “dictionary” is a vector of pairs of strings intended to represent a simple dictionary. The car of each pair is the term being defined and the cdr is the definition of the term. The vector is sorted by the term being defined.
(define short-dictionary (vector (cons "computer" "a sentient and malicious device") (cons "computer science" "the study of algorithms") (cons "cons" "a procedure that builds pairs") (cons "GIMP" "1. The GNU Image Manipulation Program. 2. Grinnell Independent Musical Productions") (cons "list" "either null or a pair whose cdr is a list") (cons "no limits" "an infamous slogan") (cons "null" "the empty list") (cons "procedure" "a parameterized chunk of code") (cons "recursion" "see recursion") (cons "ten ten" "an infamous party") (cons "vector" "a mutable and indexed data structure")))
Suppose we wanted to write a procedure,
(
, that looks up a word in a dictionary
and returns the definition (if the word is found) or lookup-word
dictionary
word
)#f
(
if the word is not found).
>
(lookup-word short-dictionary "no limits")
"an infamous slogan"
>
(lookup-word short-dictionary "king")
#f
Implement lookup-word
directly, using the divide
and conquer strategy. (When we say “directly”, we mean that
you should not call the generalized
binary-search
procedure.)
You need not document lookup-word
.
Topics: Binary search, Strings, Generalized procedures
You may recall that we wrote a generalized binary-search
procedure.
;;; 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 the portion is empty (if (> lower-bound upper-bound) ; Indicate the value cannot be found -1 ; Otherwise, identify the middle point, the element at that ; point and the key of that element. (let* ((midpoint (quotient (+ lower-bound upper-bound) 2)) (middle-element (vector-ref vec midpoint)) (middle-key (get-key middle-element)) (left? (may-precede? key middle-key)) (right? (may-precede? middle-key key))) (cond ; If the middle key equals the value, we use the middle value. ((and left? right?) midpoint) ; If the middle key is too large, look in the left half ; of the region. (left? (search-portion lower-bound (- midpoint 1))) ; Otherwise, the middle key must be too small, so look ; in the right half of the region. (else (search-portion (+ midpoint 1) upper-bound))))))))
When a generalized procedure is available, it is often useful to take advantage of such a procedure, rather than “reinventing the wheel”.
Implement lookup-word
as a call to the
binary-search
procedure.
Once again, you need not document lookup-word
.
Topics: Higher-order procedures, Code reading
Many implementations of Scheme contain the following procedures.
(define negate (lambda (pred?) (lambda (val) (not (pred? val))))) (define select (lambda (lst pred?) (cond ((null? lst) null) ((pred? (car lst)) (cons (car lst) (select (cdr lst) pred?))) (else (select (cdr lst) pred?))))) (define member? (lambda (val lst) (and (not (null? lst)) (or (equal? val (car lst)) (member? val (cdr lst))))))
Suppose we've defined the following procedure using those standard procedures.
(define fun (lambda (lst1 lst2) (select lst2 (negate (r-s member? lst1)))))
a. In your own words, explain what
fun
computes.
b. In your own words, explain how
fun
works.
Here we will post answers to questions of general interest. Please check here before emailing your questions!
(cons 'c (cons 'b 'a))
shown as
(c b . a)
rather than
(c . (b . a))
?
alphabetically-first
is correct.
string-ci<=?
the procedure should be
case-insensitive? For instance, should "apple" come before
"Banana"? In the case of two entries that are identical except
for capitalization, does it matter which is returned?
string-ci<=?
, the procedure should be
case-insensitive. The only the thing that matters is what is
specified in the post condition: that (string-ci<=? first
other)
be true for first
and all values
of other
. It is possible that may be true of
several values.
string-ci<=?
, so however it treats
non-alphabetical characters is correct.
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.
assoc-all
has the signature
(assoc-all
lst
key
)
while
assoc
has the signature
(assoc
key
lst
)
. [BM and DR, 1 point]
Primary: [Front Door] [Schedule] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] - [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings]
References: [A-Z] [By Topic] - [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.02 2009F (Weinman)] [CSC151.02 2009S (Davis)] [CSC151 2008S (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Copyright (c) 2007-9 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. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.