Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Distributed: Wednesday, 5 March 2003
Due: 10 a.m., Friday, 14 March 2003
No extensions.
This page may be found online at
http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Exams/exam.02.html
.
Contents
There are five problems on the exam. Some problems have subproblems. Each full problem is worth twenty 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. If you write down the amount of time you spend on each problem and the total time you spend on the exam, I'll give you two points of extra credit.
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 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.
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, or by posting a please help
message) to put answers
on the Web.
This is a takehome 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. It is likely to take you about five to ten hours, depending on how well you've learned topics and how fast you work. I would appreciate it if you would write down the amount of time each problem takes. 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. Since I worry about the amount of time my exams take, I will give three points of extra credit to the first two people who honestly report that they've spent at least seven hours on 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. Note also that
inappropriate assistance
is assistance from (or to) anyone
other than myself or our teaching assistant.
1. I have neither received nor given inappropriate assistance on this examination.
2. 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 both answer all of your questions electronically and turn them in in hardcopy. That is, you must write all of your answers on the computer, print them out, and hand me the printed copy with your name and the page number on every page. You must also email me a copy of your exam by copying your exam and pasting it into an email message. Put your answers in the same order as the problems. Make sure that your solution confirms to the format for laboratory writeups (except that you should not specify the location of your file).
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 fully document all of the primary procedures (including parameters, purpose, value produced, preconditions, and postconditions). If you write helper procedures (and you may certainly write helper procedures) you should document those, too, although you may opt to write less documentation. When appropriate, you should include short comments within your code. You should also take care to format your code carefully.
Just as you should be careful and precise when you write code, 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 10 p.m. and after 8 a.m.), feel free to try to call me in the office (2694410) or at home (2367445).
I will also reserve time at the start of classes this week and next to discuss any general questions you have on the exam.
You may have noted that we permit procedures to have zero, one, two, or more parameters. Some time ago, a Mathematician named Haskell B. Curry suggested that all procedures be designed to take exactly one parameter. Is that possible? Certainly. One technique would be to have procedures take a list as a parameter. However, it is also possible to simulate procedures of multiple arguments by having the singleargument procedures return other single argument procedures. We call this process Currying.
In general, instead of defining a procedure of n parameters as
(define proc (lambda (param1 param2 ... paramn) body))
we use
(define proc (lambda (param1) (lambda (param2) ... (lambda (paramn) body) ...)))
Similarly, instead of calling that procedure with
(proc arg1 arg2 ... argn)
we use
((...((proc arg1) arg2) ...) argn)
For example, here is a curried version of the twoparameter
max
(defined
using some nonCurried builtin procedures).
(define cmax (lambda (a) (lambda (b) (if (> a b) a b))))
We would use it as follows:
> ((cmax 5) 6) 6 > ((cmax 2) 1) 2
Similarly, here are Curried versions of add
and expt
,
again defined using their nonCurried counterparts.
;;; Procedure: ;;; cadd ;;; Parameters (curried): ;;; a, a number ;;; b, a number ;;; Purpose: ;;; Adds a and b. ;;; Produces: ;;; sum, a number ;;; Preconditions: ;;; a and b are numbers. ;;; a+b is computable. ;;; Postconditions: ;;; sum = a+b (define cadd (lambda (a) (lambda (b) (+ a b)))) ;;; Procedure: ;;; cexpt ;;; Parameters (Curried): ;;; base, a number ;;; power, a natural numbers ;;; Purpose: ;;; Multiplies power copies of base. ;;; Produces: ;;; result, a number ;;; Preconditions: ;;; base and power are numbers. ;;; power is a nonnegative integer ;;; The result is computable. ;;; Postconditions: ;;; result = base * base * ... * base, where there are ;;; power copies of base. (define cexpt (lambda (base) (lambda (power) (expt base power))))
We can even write a curried version of map
;;; Procedure: ;;; cmap ;;; Parameters (Curried): ;;; proc, a procedure ;;; lst, a list ;;; Purpose: ;;; Applies proc to each element of lst. ;;; Produces: ;;; newlst, a list. ;;; Preconditions: ;;; proc takes one parameter ;;; proc can be legally applied to each element of lst. ;;; Postconditions: ;;; newlst is of the same length of lst ;;; Each element, n, of newlst, is the result of applying ;;; proc to the corresponding element of lst. (define cmap (lambda (proc) (lambda (lst) (if (null? lst) null (cons (proc (car lst)) ((cmap proc) (cdr lst)))))))
What are the advantages of Currying? For the implementer, it is often easier to design a system in which all procedures have only one parameter. However, there are also advantages for the programmer. In particular, it's very easy to form the left section of any curried procedure. All you do is give it the first parameter!
For example, the procedure that adds two to any argument can be written
as (cadd 2)
(rather than the more cumbersome
(leftsection + 2)
. Similarly, the procedure
that adds two to all members of a list can be written
(cmap (cadd 2))
.
a. Using cmap
and cexpt
, compute the list
of the first eleven powers of 2 (that is, 1, 2, 4, 8, 16, ..., 512, 1024).
b. Document, write, and test csub
and
cmult
, curried procedures of two parameters that
respectively subtract and multiply their parameters.
c. Document, write, and test cmember?
, a curried procedure
of two parameters that checks whether the first parameter (a value) is a
member of the second parameter (a list).
d. Document, write, and test crightsection
, a curried
version of right section that expects a curried twoparameter procedure
as its first parameter and a value as a second parameter. Your
procedure should then fill in the second parameter of the twoparameter
procedure.
For example, given the cexpt
defined above,
you might write
(define square ((crightsection cexpt) 2))
When you applied square
to 3, you would get 9.
e. One of the most commonly used higherorder procedures is
compose
. Compose takes two singleparameter procedures
as parameters and creates a procedure that applies the second and then
the first. For example, if we wanted a procedure to multiply a number
by 5 and then add 2, we might write the following:
(compose (cadd 2) (cmult 5))
or, in curried form
((ccompose (cadd 2)) (cmult 5))
For example,
> (define scale (compose (cadd 2) (cmult 5))) > (scale 3) 17 > (scale 1/5) 3
Document, write, and test ccompose
.
Robin and Robin Recurrer took astoundingly bad notes when we discussed
recurrence relations. They also don't really care about the details of
computing the nonrecursive definition of recursivelydefined functions.
They say Just give us a table in which we can look up the form of
the recursivelydefined running time and find its bigO counterpart.
As a concerned colleague, you feel that you should help your lazy friends. Make a reference table that gives forms of recursively defined functions (ones that might represent running times of recursive functions), their nonrecursive definitions, and their smallest enclosing BigO set.
For example, one entry might be
Equations  Solution  Bound 

t(0) = c
t(n) = d +t(n1) 
t(n) = c + d(n1)  O(n) 
Your table should include any that we've covered in class and any other variations you think might be useful.
Consider the following undocumented procedure which Vierra and Vince Vector wrote is part of their attempt to Quicksort vectors.
(define partition! (let ((swap! (lambda (vec i j) (let ((tmp (vectorref vec i))) (vectorset! vec i (vectorref vec j)) (vectorset! vec j tmp))))) (lambda (vec lower upper cancomebefore?) (if (= lower upper) lower (begin (let kernel! ((l lower) (u upper)) (cond ((= u l) (begin (swap! vec lower u) u)) ((not (cancomebefore? (vectorref vec u) (vectorref vec lower))) (kernel! l ( u 1))) ((cancomebefore? (vectorref vec l) (vectorref vec lower)) (kernel! (+ l 1) u)) (else (begin (swap! vec l u) (kernel! l u))))))))))
They've told you that partition!
is intended to work
with lessthanorequaltolike functions, such as <=
and stringci<=?
. It does not not necessarily work with
lessthanlike functions, such as <
or
(lambda (val1 val2) (< (abs val1) (abs val2)))
.
a. Document the partition!
procedure. Note that you may
find it useful to experiment with the procedure as you try to figure
out what it does. You may certainly insert lines to display intermediate
results (provided you comment them out at some point).
b. One problem with partition!
is that it doesn't use
random
. Insert an appropriate call to random
.
c. Vierra and Vince never quite got around to finishing their Quicksort
algorithm. Hence, they've asked you to help. (And yes, they will
cite your assistance.) Use partition!
to implement Quicksort
for vectors. You need not document that algorithm.
Steven and Sarah Stringer find it fascinating that Scheme can figure out how to print such a wide variety of Scheme types. They find it even more fascinating that Scheme can successfully read those types. Their computer science professor (that's me) has told them that what Scheme does, in essence, is read strings and then converts them to values. They're not convinced that such a technique is possible. Given that I'm overwhelmed by other work, I'm asking you to show them.
Write a procedure, (string>value str)
that converts
the string str
to a Scheme value. You need not document
this procedure. You may not use eval
, apply
,
or read
in your answer to this problem.
For example,
> (string>value "123") 123 > (string>value "a") a > (string>value "\"hello\"") "hello" > (string>value "(a b c (1 2))") (a b c (1 2)) > (string>value "(a b . b)") (a b . b)
Your procedure should handle integers, symbols, strings, lists, and pairs. Your procedure need not handle characters, procedures, Boolean values, and the ilk.
The CSC151 students occasionally complain that I penalize them for
inelegant
code. Hence, they'd like more guidelines for writing
elegant code. Write five (5) tips, maxims, rubrics, mantras, or the
like that will help those students write more elegant Scheme code.
Your recommendations can be specific (e.g., a comment on returning
#t
or #f
from conditionals) or general (e.g.,
a comment on indentation).
These are some of the questions students have asked about the exam and my answers to those questions.
\"
)
in the midst of strings?escapethem with a backslash.
Each error identified by any student counts as one point of extra credit for everyone in the class. This form of extra credit has a limit of five points.
string>value
. [EC, 5 March 2003]
string>value
.
[BM, 12 March 2003]
Monday, 3 March 2003 [Samuel A. Rebelsky]
Tuesday, 4 March 2003 [Samuel A. Rebelsky]
Wednesday, 5 March 2003 [Samuel A. Rebelsky]
Monday, 10 March 2003 [Samuel A. Rebelsky]
flurb!
to partition!
.
If you dealt with the bad name, tell me you deserve extra
credit so that I can get a good laugh.
partition!
procedure.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue May 6 09:19:05 2003.
The source to the document was last modified on Thu Mar 13 08:19:07 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Exams/exam.02.html
.
; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu