Functional Problem Solving (CSC 151 2014S) : Assignments

Exam 3: Sophisticated Scheming


Assigned: Friday, 25 April 2014

A draft version of this examination may be distributed before Friday, 25 April 2014. We will not accept corrections on the exam before class time on Friday.

Due: The due dates for various tasks are as follows.

Preliminaries

Exam format

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.

This examination has a prologue that must be completed by the Sunday evening before the exam is due. The prologue is intended to help you get started thinking about the examination. The prologue is optional. However, if you intend to invoke the “there's more to life” option (see below), you must turn in the prologue by the specified deadline.

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 four hours, depending on how well you've learned the topics and how fast you work. You should not work more than five hours on this exam. Stop at five hours and write “There's more to life than CS” and you will earn at least a 70 on this exam, provided you filled in the prologue by the specified deadline. You may count the time you spend on the prologue toward those five hours. With such evidence of serious intent, your score will be the maximum of (1) your actual score out of 100 or (2) 70. The bonus points for errors are not usually applied in the second situation.

We 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. Because we worry about the amount of time our exams take, we will give two points of extra credit to the first two people who honestly report that they have completed the exam in four hours or less or have spent at least four hours on the exam. In the latter case, they should also report on what work they've completed in the four hours. After receiving such notices, we may change the exam.

Academic Honesty

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 any students who worked with you. 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, via IRC, 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.” 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.

  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.

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.

Exams can be stressful. Don't let the stress of the exam lead you to make decisions that you will later regret.

Presenting Your Work

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 with the subject CSC 151.02 Exam 3 (Your Name). 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 at least two points. Failure to turn in both versions may lead to a much worse penalty.

While your electronic version is due at 10:30 p.m. Thursday, your physical copy will be submitted in class on Friday. 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, we ask you to write code. Unless we 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 resulting images; we should be able to regenerate those.

Unless we explicitly tell you not to document your procedures, you must write 6P-style documentation for every globally accessible procedure. (Thus, anonymous and local helper procedures are exempted.)

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 we 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. We will limit that form of extra credit to five points.

I will give partial credit for partially correct answers. We are 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.

Getting Help

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 (8am-10pm), feel free to try to call me (cell phone (text only) - 641-990-2947).

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.

Problems

Problem 1: Testing assoc

Topics: Testing, assoc.

Run the following command in a terminal window to tell Racket where to find a supposedly new-and-improved version of assoc Professor Weinman wrote while simultaneously excited and sleep deprived.

/opt/racket/bin/raco link /home/weinman/courses/CSC151

To use that new (and perhaps not-so-improved) version of assoc, you will need to include the following line in your definitions.

(require CSC151/assoc)

We have manually tested this procedure using the following input, and it appears to work correctly:

> (assoc 3 (list (cons 1 2) (cons 3 4) (cons 5 6)))
'(3 . 4)
> (assoc 1 (list 1 2 3))
assoc: expected a list of key-value pairs; given (1 2 3)

Unfortunately, one or two experiments are not enough. In fact, the implementation has several bugs.

Using the RackUnit library, write a complete test suite for assoc, as documented in the reading on association lists.

Your test suite should include at least three tests for which the built-in assoc procedure passes and our buggy procedure fails.

Problem 2: Swapping Vector Elements

Topics: Vectors.

Implement the following procedure.

;;; Procedure:
;;;   vector-swap!
;;; Parameters:
;;;   vec, a vector
;;;   i, an integer
;;;   j, an integer
;;; Purpose:
;;;   Swap the elements in positions i and j of vector.
;;; Produces:
;;;   [Nothing; called for the side effect]
;;; Preconditions:
;;;   0 <= i < (vector-length vec)
;;;   0 <= j < (vector-length vec)
;;;   I is (vector-ref vec i)
;;;   J is (vector-ref vec j)
;;; Postconditions:
;;;   (vector-ref vec i) is J.
;;;   (vector-ref vec j) is I.
;;;   No other elements in the vector have changed.

For example,

> (define vec (list->vector (iota 5)))
> vec
'#(0 1 2 3 4)
> (vector-swap! vec 0 3)
> vec
'#(3 1 2 0 4)
> (vector-swap! vec 0 4)
> vec
'#(4 1 2 0 3)
> (vector-swap! vec 1 3)
> vec
'#(4 0 2 1 3)

Problem 3: Permuting Vectors

Topics: Vectors, iteration, randomness, higher-order procedures.

In many situations, it is useful to permute a vector, that is, rearrange the order of elements. For example, permutation is at the root of shuffle-play in many music applications (and is probably subject to way too many patents). For classrooms, many faculty like to permute the list of students in class when making pairs or calling on students in order.

Write, but do not document, a procedure (vector-permute! vec), that rearranges the elements of the vector by repeatedly swapping elements in two random indices. How many times should you swap? If there are n elements in the vector, you should swap n times.

You may not implement vector-permute! recursively.

Problem 4: Representing Turtle Instructions

Topics: Turtles, characters.

One way to produce images with the appearance of chaotic randomness is by thinking of characters similar to the way biologists might. Rather than thinking of the characters G, C, T, and A as the instructional DNA for organisms, we can think of all characters as instructional commands for turtles.

Write, but do not document, the procedure (turtle-dna! turtle char) that moves the turtle forward by the character's collating sequence number if the character is uppercase, turns the turtle by the character's collating sequence number if the character is lowercase, and otherwise turns the turtle by the negative of the character's collating sequence number.

Problem 5: Illustrating Texts

Topics: Turtles, strings, iteration, higher-order procedures.

What happens when we use the turtle-dna! procedure to visualize a string of text? Write, but do not document, a procedure (turtle-express-text! turtle str) that takes a string and applies turtle-dna! to each character in the string (in order).

For example, this is the image we get when expressing the first ten paragraphs of Melville's Moby Dick.

(define the-sea (image-new 1024 1024))
(define ishmael (turtle-new the-sea))
(turtle-teleport! ishmael 512 512)
(turtle-express-text! ishmael "Call me Ishmael. ...")

Problem 6: Balanced Trees

Topics: Trees, predicates, tree recursion.

A “balanced” tree is one that has no subtrees (i.e., it is simply a leaf) or whose two subtrees have the same depth and are themselves both balanced.

The following version of a predicate tree-balanced? is correct, but extremely inefficient because it involves multiple traversals of the tree. At each pair, one traversal is done with the tree-depth procedure, and this is followed by a traversal done with recursive calls to tree-balanced?.

(define tree-balanced-v0?
 (lambda (tree)
   (if (pair? tree)
       (let ([left-depth (tree-depth (car tree))]
             [right-depth (tree-depth (cdr tree))])
         (and (= left-depth right-depth)
              (tree-balanced-v0? (car tree))
              (tree-balanced-v0? (cdr tree))))
       #t)))

Write a more efficient tree-balanced? procedure that traverses the tree only once by returning the tree's depth when the tree is balanced and #f otherwise.

Hint: You should not call tree-depth anywhere in your solution. Instead, note (and use!) the specified value produced by the recursive calls to tree-balanced?.

Note: The procedure tree-depth appeared in the first reading on trees.

> (tree-balanced? (cons 'a 'b))
1
> (tree-balanced? (cons 'a (cons 'b 'c)))
#f
> (tree-balanced? (cons (cons 'a 'b) (cons 'c 'd)))
2
> (tree-balanced? (cons (cons 'a 'b) (cons (cons 'c 'd) 'e)))
#f
> (tree-balanced? 'a)
0

Problem 7: Analyzing Balanced Trees

Topics: Annotating code, analysis.

a. Using the counters from the reading on analyzing procedures, annotate tree-depth and tree-balanced-v0? to count the total number of calls made to those two procedures for balanced trees of depths 0, 1, 2, 3, and 4.

b. Similarly annotate your own improved version of tree-balanced? to count the total number of calls made to that procedure for balanced trees of depths 0, 1, 2, 3, and 4.

c. Using your results from (a) and (b), complete the following table.

Balanced Depth Total calls to tree-depth and tree-balanced-v0? Total calls to tree-balanced?
0
1
2
3
4

d. Compare the absolute efficiencies of the two approaches. That is, briefly explain how and why they differ in the number of procedure calls each must make as the number of items in the tree (a function of the depth) grows.

Problem 8: Nesting a Procedure

Topics: Higher-order programming, documentation, recursion

Document, but do not write, a procedure, (nest fun n). 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 fun. For example, (nest square 3) is equivalent to (lambda (val) (square (square (square val)))).

Note that n must be at least 1 for nest to make sense.

Here are some examples using nest.

> (define plus5 (nest (l-s + 1) 5))
> (plus5 6)
11
> (define list5 (nest list 5))
> (list5 6)
'(((((6)))))
> (define sqqqr (nest square 3))
> (sqqqr 2) 
256
> (sqqqr 3)
6561
> (nest list 0)
nest: parameter n must be a positive integer, given 0
> (define duplicate (lambda (val n) ((nest (l-s c
ons val) n) null)))
> (duplicate "hello" 5)
'("hello" "hello" "hello" "hello" "hello")

Note that in documenting nest, you may find it useful to express the postconditions recursively.

Problem 9: Nesting a Procedure, Revisited

Topics: Higher-order programming, documentation, recursion, precondition testing.

Implement nest. In your implementation, make sure that you verify the preconditions regarding n using a husk-and-kernel style.

Problem 10: Mystery Procedure

Topics: Code reading, procedures as parameters, binary search.

At times, programmers make decisions that lead to programs that are particularly hard to read. For example, many programmers like to use single-character variable names (less typing!) and don't think that indentation makes much difference (“The computer doesn't care, why should I?”).

Consider the following procedure, a form of which often appears in libraries for searching and sorting.

(define f (lambda (l m n) (let
([s (vector-length l)]) (let k ([p 1])
(or (>= p s) (and (n (m (vector-ref l
(- p 1))) (m (vector-ref l p))) (k (+
p 1))))))))

a. Figure out what f does, then rewrite the code for clarity. That is, fix the indentation and rename the variables and procedure name.

b. Write 6P documentation for the renamed procedure.

c. Write a seventh P, Process, that explains the details of the underlying algorithm and how the code above manifests an implementation of that algorithm.

Some Questions and Answers

Here we will post answers to questions of general interest. Please check here before emailing your questions!

General Questions

What is a general question?
A question that is about the exam in general, not a particular problem.
Do the two classes have the same exam?
Yes, although we format it differently.
Does our exam need to be in the body of the email, or will you accept attachments?
Rebelsky accepts attachments. Weinman does not.
Can we still invoke the “There's more to life” clause if we spend more than five hours on the exam?
Yes. However, we really do recommend that you stop at five hours unless you are very close to finishing. It's not worth your time or stress to spend more effort on the exam. It is, however, worth your time to come talk to us, and perhaps to get a mentor or more help. There's likely some concept you're missing, and we can help figure that out.

Problem 3

Can we use our solution for problem 2 in solving problem 3?
Certainly. That's the intent of the problem.

Problem 6

What should we return for the null tree?
Since you are returning the depth of the tree, 0 seems appropriate.

Problem 7

How should we format a table in DrRacket?

Whatever way seems most sensible/legible. For example, you could use plenty of whitespace (e.g., tabs or spaces) to separate columns. For example, you might make it a comment.

; +----------------+-----------------------------+--------------------+
; | Balanced depth | Total calls to td? and tbv0 | Total calls to tb? |
; +----------------+-----------------------------+--------------------+
; |       0        |                             |                    |
; +----------------+-----------------------------+--------------------+
; |       1        |                             |                    |
; +----------------+-----------------------------+--------------------+
; |       2        |                             |                    |
; +----------------+-----------------------------+--------------------+
; |       3        |                             |                    |
; +----------------+-----------------------------+--------------------+
; |       4        |                             |                    |
; +----------------+-----------------------------+--------------------+

You might also make it a Scheme structure, which would allow us to process the data computationally.

(define balanced-data
  (vector (vector "Balanced depth" 
                  "Total calls to tree-depth? and tree-balanced-v0?"
                  "Total calls to tree-balanced?")
          (vector 0 ___ ___)
          (vector 1 ___ ___)
          (vector 2 ___ ___)
          (vector 3 ___ ___)
          (vector 4 ___ ___)))

Problem 8

When you say “express the postconditions recursively”, what do you mean?
You might have one postcondition that explains what (nest fun 1) is and one that defines (nest fun n) in terms of (nest fun (smaller n)), for n greater than greater than one and some definition of smaller.

Errata

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. (And no, we don't count errors in the errata section or the question and answer sections.)

  • Predicate tree-balanced-v0? had the wrong base case value. [JW, 1 point]
  • Parameter fun to nest was inconsistently named. [AC, 1 point]
  • The behavior of turtle-dna! shown in the image was different than that given in the description. Lowercase letters should represent turning. Uppercase letters should represent moving forward. [Various, 1 point]

Citations

Many of the problems on this exam are based on (and, at times, copied from) problems on previous exams for the course. Those exams were written by Janet Davis, Rhys Price Jones, Samuel A. Rebelsky, John David Stone, Henry Walker, and Jerod Weinman. Many were written collaboratively, or were themselves based upon prior examinations, so precise credit is impossible.

Much of the introductory and concluding text on this examination has a similarly collaborative background. Sections were likely written by different faculty and then updated by others.

Some problems on this exam were inspired by conversations with our students. We thank our students for that inspiration. Usually, a combination of questions or discussions inspired a problem, so it is difficult and inappropriate to credit individual students.


Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-2014 Janet Davis, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials are copyright by John David Stone or Henry Walker and are used with permission.)

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.