| Schedule | Readings | Labs | Homework | Mechanics | Contact | |
| CSC 151-01, 2007S » Exam 3 » Advanced Scheme | ||||||
Distributed: Friday, April 27,
2007
Due: 9:00 a.m., Friday, May 4, 2007
This document may be found online at http://www.cs.grinnell.edu/~davisjan/csc/151/2007S/exams/03.advanced.html
Contents
There are four problems on the exam. Some problems have subproblems. Each problem is worth twenty-five (25) points. The point value associated with a problem does not necessarily correspond to the complexity of the problem or the time required to solve the problem.
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.
As a reminder, you may refer to the Scheme manual as well as the course readings:
Kelsey, Richard, Clinger, William, and Rees, Jonathan, eds. (1998). Revised5 Report on the Algorithmic Language Scheme [HTML] [PDF]. February 20, 1998.
If you choose to work from home, you may wish to install DrScheme on your own computer.
Although you may use the Web for this exam, you may not post your
answers
to this examination on the Web (at least not until after I return exams
to you). 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.
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.
I 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 to six hours, depending on how well you've learned
topics
and how fast you work. You should not work more than eight
hours
on this exam. Stop at eight hours and write There's more to
life
than CS
and you will earn at least 75 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've spent at least five hours on the exam or completed the exam. (At that point, I may then change 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 Davis (that's me) or Professor Rebelsky.
- I have neither received nor given inappropriate assistance on this examination.
- I am not aware of any other students who have given or received inappropriate assistance on this examination.
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 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.
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. You ensure the best possible grade for yourself by emphasizing your answer and including a clear set of work that you used to derive the 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 midnight and after 10 a.m.), feel free to try to call me on my cell phone (206-383-8798). You may also email me (davisjan@cs.grinnell.edu) or seek help from Professor Rebelsky (rebelsky@grinnell.edu).
Topics: Higher-Order Procedures, Sequential Search, Vectors, Binary Trees, Deep Recursion
In a recent homework assignment, you wrote a procedure, (tally pred? lst) that, given a predicate and a list, computes the number of values in the list for which the predicate holds.
As you've noted before, Scheme provides a variety of ways to collect data. For example, in addition to collecting values in a list, we might collect them in a vector, file, or tree.
a. [15 points] Write a procedure, (tally-vector pred? vec),
which, given a unary predicate and a vector, counts the number of
values in the vector for which the predicate holds. For example,
> (tally-vector (l-s <= 5) (vector 6 4 1 2 6 9 4 4 1 0))
3
> (tally-vector symbol? (vector 2 'too "two" 'tutu))
2
> (tally-vector odd? (vector))
0
b. [10 points] Write a procedure, (tally-tree pred? tree), that takes a unary predicate and a tree and counts the number of leaves in the tree for which the predicate holds.
For example,
> (tally-tree exact? (cons (cons 3+4i 3.0) (cons 22/7 (cons 4.2 5))))
3
> (tally-tree complex? (cons (cons 3+4i 3.0) (cons 22/7 (cons 4.2 5))))
5
> (tally-tree integer? (cons (cons 3+4i 3.0) (cons 22/7 (cons 4.2 5))))
2
> (tally-tree integer? 5)
1
> (tally-tree integer? 'a)
0
> (tally-tree null? (cons (cons 3+4i 3.0) (cons (cons 22/7 (cons 4.2 2)) 'a)))
0
> (tally-tree null? (list (list 3+4i 3.0) (list (list 22/7 (list 4.2 2)) 'a)))
5
Subject: Higher-order procedures, predicates
In our explorations of higher-order procedures, we have encountered a variety of interesting procedures. Here are some you might find useful, as well as some other standard procedures from the experienced Scheme programmer's toolbox.
(define l-s
(lambda (binproc left)
(lambda (right)
(binproc left right))))
(define r-s
(lambda (binproc right)
(lambda (left)
(binproc left right))))
(define compose
(lambda (f g)
(lambda (x)
(f (g x)))))
(define both
(lambda (pred1? pred2?)
(lambda (val)
(and (pred1? val) (pred2? val)))))
(define either
(lambda (pred1? pred2?)
(lambda (val)
(or (pred1? val) (pred2? val)))))
(define negate
(lambda (pred?)
(lambda (val)
(not (pred? val)))))
(define all
(lambda (pred?)
(lambda (lst)
(let kernel ((lst lst))
(or (null? lst)
(and (pred? (car lst))
(kernel (cdr lst))))))))
(define all-real? (all real?))
(define any
(lambda (pred?)
(lambda (lst)
(let kernel ((lst lst))
(and (not (null? lst))
(or (pred? (car lst))
(kernel (cdr lst))))))))
(define any-odd? (any (both integer? odd?)))
(define member?
(lambda (val lst)
((any (l-s equal? val)) lst)))
(define select
(lambda (pred? lst)
(cond
((null? lst) null)
((pred? (car lst))
(cons (car lst) (select pred? (cdr lst))))
(else (select pred? (cdr lst))))))
We use these procedures to more concisely and more elegantly define
procedures, as in the case of any-odd? above.
Your goal is to write the following definitions as concisely as possible,
using the various procedures above.
a. [10 points] Write a procedure, (select-Bs lst), that,
given a list of real numbers, selects only those that are at least 80
and strictly less than 90.
b. [10 points] Write a procedure, (make-filter pred?), that returns
a filter. The filter is a procedure of one parameter (a list,
lst) which returns a new list by removing all the elements
of the lst for which pred? holds. For example,
> (define no-symbols (make-filter symbol?))
> (no-symbols (list 1 2 'a 'b 3 5))
(1 2 3 5)
> (no-symbols null)
()
c. [5 points] As you may recall, in an earlier assignment you wrote a procedure,
(intersect lst1 lst2) in which lst1
and lst2 are lists of symbols,
representing sets. Now that you have the higher-order toolbox,
you can write the solution much more concisely. Do so.
You may assume that lst1 and lst2 are proper sets: That is, neither of the lists contains duplicate elements.
Hint: You may find member?, select, and left- and right-section useful.
Topics: Sorting, Anonymous Procedures, Comparators
In our exploration of sorting, we discovered that the choice of the comparator used to determine whether one value naturally preceded another value was of key import in writing calls to sorting routines. Here are some potentially interesting problems. For each, use merge sort as the mechanism for sorting, relying on mergesort.scm, the sample implementation of merge sort.
a. [10 points] Suppose words is a list of strings. Write an expression to sort words by the length of each word.
b. [10 points] Suppose we represent homework grades in the class with a list of lists. Each element list begins with a string that represents the student's name and is followed by all of the grades, represented as integers. For example,
(define grades
(list
(list "Sam" 60 60 90 100 60 80)
(list "Sammy" 95 95 95 95 90 80)
(list "Samwise" 100 0 0 0 0 0)
(list "Sammer" 60 70 80 90 100 110)))
Write an expression that sorts grades by average grade.
c. [5 points] As you may know, some printers use lists of grid points to
determine where to place ink. Suppose we are representing points by
pairs with the x coordinate as the car and the y coordinate as the cdr.
To improve the performance of our printer, we want to group rows together
(so that all the values with a y of 0 appear before all values with a y
of 1 appear before all values with a y of 2, and so on and so forth).
In addition, the printer often scans rows in opposite directions.
That is, it scans row 0 left to right, row 1 right to left, row 2 left
to right, and so on and so forth. Write an expression to sort a list
of points, points to meet the scan behavior of the printer.
Topics: Divide-and-conquer, algorithm analysis, Quicksort
As you may recall, one of the reasons that we have to use a random pivot in Quicksort is that it is expensive (in terms of computing time) to find the median element of a list. In fact, for an arbitrary n, it is expensive to find the nth-smallest element of the list.
There is one obvious strategy:
After we remove n elements, the smallest remaining element is the nth smallest. For a list of length m, this takes approximately n*m comparisons.
Can we do better? Yes. It turns out that a variant of Quicksort is also ideal for finding the nth element of the list.
a. [10 points] Document (nth-smallest n lst precedes?),
a procedure that finds the nth smallest element of lst
using precedes? to determine ordering.
b. [15 points] Implement (nth-smallest n lst precedes?).
You may find the following procedures helpful.
;;; Procedure:
;;; random-element
;;; Parameters:
;;; lst, a nonempty list of values
;;; Purpose:
;;; "Randomly" select a element of lst.
;;; Produces:
;;; val, an element of list.
;;; Preconditions:
;;; lst is nonempty.
;;; Postconditions:
;;; val is a member of lst - i.e., (member? val lst)
;;; Each element of lst can be selected with equal probability.
(define random-element
(lambda (lst)
(list-ref lst (random (length lst)))))
;;; Procedure:
;;; partition
;;; Parameters:
;;; lst, a list
;;; pivot, a value
;;; precedes?, a binary predicate
;;; Purpose:
;;; Partition lst into three lists,
;;; one for which (precedes? val pivot) holds,
;;; one for which (precedes? pivot val) holds, and
;;; one for which neither holds.
;;; Produces:
;;; (smaller-elements equal-elements larger-elements), A two element list
;;; Preconditions:
;;; precedes? can be applied to pivot and any value of lst.
;;; Postconditions:
;;; (append smaller-elements equal-elements larger-elements)
;;; is a permutation of lst.
;;; (precedes? (list-ref smaller-elements i) pivot)
;;; holds for every i, 0 < i < (length smaller-elements).
;;; (precedes? pivot (list-ref larger-elements j))
;;; holds for every j, 0 < j < (length larger-elements).
;;; Neither (precedes? (list-ref equal-elements k) pivot)
;;; nor (precedes? pivot (list-ref equal-elements k))
;;; holds for every k, 0 < k < (length equal-elements)
(define partition
(lambda (lst pivot precedes?)
(letrec ((kernel
(lambda (remaining smaller-elements equal-elements larger-elements)
(cond
((null? remaining)
(list (reverse smaller-elements)
(reverse equal-elements)
(reverse larger-elements)))
((precedes? (car remaining) pivot)
(kernel (cdr remaining)
(cons (car remaining) smaller-elements)
equal-elements
larger-elements))
((precedes? pivot (car remaining))
(kernel (cdr remaining)
smaller-elements
equal-elements
(cons (car remaining) larger-elements)))
(else
(kernel (cdr remaining)
smaller-elements
(cons (car remaining) equal-elements)
larger-elements))))))
(kernel lst null null null))))
These are some of the questions students have asked about the exam and my answers to those questions.
Problem 1
Problem 2
remove might be useful for part (c). I don't see a definition for remove. Where does it come from?remove is a built-in procedure. For an
example, try evaluating (remove 2 (list 1 2 3)) in
DrScheme. However, I meant to suggest select rather than
remove. select is defined above.
Problem 3
Do whatever you think is more appropriate.
Problem 4
This is a quote from question 4: "After we remove n elements, the smallest remaining element is the nth smallest." If you apply this logic to say the list (1 2 3 4 5 6 7 8 9 10), it would imply that the 5th smallest element, after removing (1 2 3 4 5) is 6. What does this make 1 then? Does this imply that 1 is the 0th smallest element?
Yes.
Do you want us to ignore or incorporate duplicates such that for example, the sixth smallest element of (1 2 2 3 3 4 4 5 5 6 6 7 8 9 10) should be 7 and not 4 as would be the case if we didn't ignore duplicates?
You should include duplicates; the 6th smallest element in your example is 4. This is what the algorithm we described will do.
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. I usually limit such extra credit to five points. However, if I make an astoundingly large number of errors, then I will provide more extra credit.
>(tally-vector symbol? (vector 2 'too "two" 'tutu))
should return 2 instead of 1 as it is in the exam.
It's been fixed. [Ernest, 1 point]
It's been fixed. [Gabe, 1 point]
Janet Davis (davisjan@cs.grinnell.edu)
Created April 26, 2007