Functional Problem Solving (CSC 151 2015S) : Assignments

Exam 4: Higher, Deeper, and Faster


Assigned: Monday, 27 April 2015

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

Important! This examination has an initial code file that you should copy and use as the starting point for your work.

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 Friday evening before the exam is due. The prologue is intended to help you get started thinking about the examination. The prologue is required. Failure to fill in the prologue by the designated time will incur a penalty of five points on the examination.

This examination has an epilogue that must be completed by the evening after the exam is due. The epilogue is intended to help you reflect carefully on the examination. The epilogue is required. Failure to fill in the epilogue will incur a penalty of five points on the exam.

There are seven problems on this examination. Each problem is worth the same number of points. Although each problem is worth the same amount, problems are not necessarily of equal difficulty.

Please 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” on the cover sheet of the examination and you will earn at least the equivalent of 70% on this exam, provided you recorded the time spent on each problem, filled in the prologue by the specified deadline, filled in the epilogue, and arranged for a meeting with me within one week of receiving your graded exam. You may count the time you spend on the prologue toward those five hours, but not the time you spend on the epilogue.. With such evidence of serious intent, your score will be the maximum of (1) your actual score or (2) the equivalent of 70%. The bonus points for errors and recording time are not usually applied in the second situation, but penalties (e.g., for failing to number pages) usually are.

You should not count time reviewing readings, laboratories, or assignments toward the amount of time you spend on the exam or on individual problems.

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 for the exam. 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 as well as 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 write, sign, and date each statement separately. 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, physically and electronically.

For the physical copy, you must write all of your answers using the computer, print them out, number the pages, staple them together (except for the cover sheet), and hand me the printed copy. For your benefit and for ours, we are doing blind grading on this examination, so you have been assigned a number to use on your exam. Please make sure that your number appears at the top of every page. You should turn in a separate cover sheet along with your stapled and printed answers. The cover sheet should include (1) the two hand-written academic honesty statements (individually signed and dated, if it is appropriate for you to sign each), (2) your name, and (3) your assigned number. If you choose to invoke the “there's more to life than computer science” option, then you must indicate that option on the cover sheet, and you should indicate it only on the cover sheet.

The code and comments in your printed copy must use a fixed-width (a.k.a., monospaced or fixed-pitch) font; depending on what platform you use, viable candidates include Monospace, Courier, Courier New, Monaco, DejaVu Sans Mono, Free Mono, Liberation Mono, and Lucida Sans Typewriter. Failure to format your code with a monospace font will result in a penalty. You may read the instructions on printing for more details on how to create readable output.

Since there is a lot of utility code in the examination (placed at the end in the template), make a copy of your exam and remove that utility code before printing.

You must also submit the code for your examination at http://bit.ly/151-2015S-exam4. Ideally, you would put all of the code for the exam in a single Racket file. However, if you have created separate files for the separate parts of the exam, you can just paste them one after another when you submit, provided you put a clear separator, such as ; PROBLEM 2, between sections.

In both cases (physical and electronic), you should put your answers in the same order as the problems. Failure to number the printed pages will lead to a penalty. Failure to turn in both versions may lead to a much worse penalty.

While your electronic version is due at 10:30 p.m. Monday, your physical copy will be submitted in class on Tuesday. It is presumed the physical copy matches the electronic copy. Any discrepancies (other than formatting) will likely 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 informally (by looking at what value you get for various inputs) or formally (by using the Rackunit testing framework). In addition to the examples provided in the exam, you should also provide additional examples. Do not include resulting images; we should be able to regenerate those.

Unless we tell you otherwise, you should assume that you need to provide 6P-style documentation for each primary procedure you write. Most helper procedures should be local, in which case you need only document them with a sentence or so. If you write any non-local helper procedures, you must document them with 6P-style documentation using at least the first four P's (Procedure, Purpose, Parameters, Produces).

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 in the preliminaries and problems on this exam. We will limit that form of extra credit to five points.

We 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 classes the week the exam is due to discuss any general questions you have on the exam.

Checklist

Since many students regularly seem to miss different elements of the exam, this checklist serves as a way to help you remember everything that you have to do.

Big Picture

  • Have you made a copy of the exam code? (Goal: Wednesday; Expected: Friday)
  • Have you filled out the prologue? (Goal: Wednesday; Due: Friday)
  • Have you done your requisite work on the exam? (Due: Monday)
  • Have you checked over your work on the exam using the checklist below? (Due: Monday)
  • Have you submitted the code online? (Due: Monday)
  • Have you filled out the epilogue? (Due: Monday)
  • Have you written a cover sheet and gone over the cover sheet checklist? (Due: Tuesday)
  • Have you printed your examination and gone over the printed exam checklist? (Due: Tuesday)

Exam Contents

  • Have you reviewed the question and answer section of the exam?
  • Have you removed all identifying materials from your code, other than your assigned id number?
  • If you've copied any code, have you also copied the accompanying documentation?
  • Have you cited all the code that you've relied upon?
  • Have you recorded how long each problem took?
  • Have you included examples for each procedure you've written?
  • Have you documented each procedure you've written, including each helper procedure? (Your helper procedures only need the first four P's.)
  • Have you made all of your helpers local or documented them?
  • Have you checked your spelling?

Printed Version

  • Did you remove the supplied code from the end of the copy of the exam that you're printing?
  • Did you use a monospace font?
  • Have you written or printed your id on every page?
  • Have you written or printed the page number on every page?
  • Have you looked at the output to make sure that there are no instances in which the code wraps badly?

Cover Sheet

  • Have you written your id on the cover sheet?
  • Have you written your name on the cover sheet?
  • Have you written, signed, and dated the first academic honesty statement? (This item assumes you consider it appropriate to sign the first academic honesty statement.)
  • Have you written, signed, and dated the second academic honesty statement? (This item assumes you consider it appropriate to sign the second academic honesty statement.)

Background: Binary Trees, Revisited

Many of the problems on this examination use a modified version of the trees we've used recently. This section introduces those new trees and the operations available on them.

In our initial exploration of trees, we thought about the kinds of trees we could build with pairs. But when computer scientists typically think of trees, they don't just have values at the leaves, they also have values at each point in the tree, as in the following illustration.

For these trees, each part has three values, rather than two: the value, the left subtree, and the right subtree. Since we're storing three values, rather than two, we can't directly use pairs. Typically, computer scientists call the elements of trees “nodes”.

Sometimes, we store an entry consisting of multiple values in each node, typically in the form of a pair or list. These pairs or lists often start with a key that we use to refer to the entry, as in the following example.

How do you work with trees? For this examination, we've provided a library of procedures that you can find in the code for the examination.

In place of cons, we provide (node val left right), which builds one of these nodes. There is also a predicate, node?, that checks whether a given Scheme value is a node.

When working with pairs, we use car and cdr to extract the two parts of a pair. You will use root-value, left-subtree, and right-subtree to extract the three parts of a node.

When working with lists, we use null to represent the empty list. When working with trees, we will use nil to represent the empty tree. We will use nil? to check if a value represents the empty tree.

There is even a procedure, (visualize-tree tree width height), that makes a simple image that represents a tree. We've used that procedure to create the images of trees that appear in this examination.

You can find additional reference information on the important tree procedures at the end of this examination. You can see the code for these and other methods in the code for this examination.

Problems

Problem 1: Testing assoc

Topics: Testing, assoc

In writing tests, we are too often biased by the code we've written, that we see, or that we plan to write. But good tests can and should be written independent of the code being tested.

A few years ago, one of my colleagues wrote a new-and-improved, but not quite correct, version of assoc. More recently, I attempted to rewrite that version, relying on a variety of student implementations of assoc. My version was also not quite correct.

Your goal in this problem is to write a test suite for assoc that will find the errors in erroneous versions, but will not find errors in the standard version of assoc.

How can you access this new version? There are two steps.

First, in the terminal, type the following.

/opt/racket/bin/raco link /home/rebelsky/share/CSC151-2015S

Second, when you want to use the new (and perhaps not-so-improved) version of assoc, you will write the following in the definitions pane.

(require CSC151-2015S/a-sock)

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

> (assoc 5 (list (cons 1 2) (cons 3 4) (cons 5 6) (cons 7 8)))
'(5 . 6)
> (assoc 1 (list (cons 1 2) (cons 3 4) (cons 5 6) (cons 7 8)))
'(1 . 2)
> (define colors
    (list (list "red" (list 255 0 0))
          (list "purple" (list 255 0 255))
          (list "green" (list 0 128 0))
          (list "blue" (list 0 0 255))
          (list "black" (list 0 0 0))
          (list "white" (list 255 255 255))
          (list "grey" (list 128 128 128))))
> (assoc "blue" colors)
'("blue" (0 0 255))

Unfortunately, a few experiments are not enough. In fact, as noted above, the implementation has several bugs.

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

Your test suite should identify at least four different ways in which the buggy procedure produces an incorrect result. (It produces incorrect results in at least five ways, one of which is quite subtle. You may receive extra credit for finding the most subtle issue.)

Hint: Think about the many issues we explored in playing with assoc.

Problem 2: From tables to functions

Topics: assoc, higher-order procedures

We've seen that it's possible to use assoc to search a table for a value. We've also seen that in some cases, we want to “hard-code” the table.

Write, but do not document, a procedure, table->lookup that takes as input a list of pairs of the form that assoc expects and returns a procedure of one parameter. The returned function should take a key as a value and either (a) return the cdr of the entry corresponding to that key, if such an entry exists or (b) issue an error message if the entry does not exist.

Here are a few examples.

> (define lookup-grinnell-CS100
    (table->lookup (list (cons "CSC105" "The Digital Age")
                         (cons "CSC151" "Functional Problem Solving")
                         (cons "CSC161" "Imperative Problem Solving")
                         (cons "CSC195" "Special Topics"))))
> (lookup-grinnell-CS100 "CSC161")
"Imperative Problem Solving"
> (lookup-grinnell-CS100 "CSC105")
"The Digital Age"
> (lookup-grinnell-CS100 "CSC100")
. . Could not find "CSC100"
> (define cname->rgb-list
    (table->lookup (list (list "black" 0 0 0)
                         (list "white" 255 255 255)
                         (list "grey" 128 128 128)
                         (list "blue" 0 0 255)
                         (list "red" 255 0 0)
                         (list "green" 0 128 0))))
> (cname->rgb-list "red")
'(255 0 0)
> (cname->rgb-list "puce")
. . Could not find "puce"
> (cname->rgb-list "white")
'(255 255 255)

Here's some 6P-style documentation for table->lookup for those of you who find it helpful to see such documentation.

;;; Procedure:
;;;   table->lookup
;;; Parameters:
;;;   table, a list of pairs
;;; Purpose:
;;;   Create a lookup function for the table
;;; Produces:
;;;   lookup, a unary function
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   If there is an entry of table whose car is key, (lookup key) is the
;;;     cdr of the first such entry.
;;;   If there is no such entry, (lookup key) issues an error of the form
;;;     "Could not find ..."

Hint: You should use assoc in your definition.

Problem 3: Iteration

Topics: Higher-order procedures, repetition, numeric recursion

We frequently write the following pattern to do a series of actions with side effects.

(for-each (lambda (i)
            (_________ (* __ (+ __ i))))
          (iota __))

While this model is concise and readable, it also requires us to build a list that we don't really need, and perhaps even to do a bit of extra computation in turning the i values into the values we really want.

Write, but do not document, a procedure, (iterate proc! initial final offset), that calls proc! on initial, then calls proc! on initial+offset, then calls proc! on initial+offset+offset, and so on and so forth until we exceed final.

> (iterate (lambda (val) (display val) (display " "))
           7 11 1)
7 8 9 10 11 
> (iterate (lambda (val) (display val) (display " "))
           10 70 10)
10 20 30 40 50 60 70 
> (iterate (lambda (val) (display val) (display " "))
           7 70 10)
7 17 27 37 47 57 67 
> (iterate (lambda (val) (display val) (display " "))
           11 7 1)
; No output, because 7 is less than 11
> (iterate (lambda (val) (display val) (display " "))
           -20 -10 2)
-20 -18 -16 -14 -12 -10 

Although the examples all use a procedure that displays the values, any procedure is possible. We will more frequently use this procedure with turtles and other commonly-used operations with side effects.

Here's the for-each version of turtle-spiral!.

(define turtle-spiral!
  (lambda (turtle amt length)
    (for-each (lambda (angle)
                (turtle-forward! turtle amt)
                (turtle-turn! turtle angle))
              (cdr (iota (+ length 1))))))

In contrast, here's the improved version using iterate.

(define turtle-spiral!
  (lambda (turtle amt length)
    (iterate (lambda (angle)
               (turtle-forward! turtle amt)
               (turtle-turn! turtle angle))
             1 length 1)))

Hint: Use a local kernel that keeps track of the current parameter to proc!.

Problem 4: A tree predicate

Topics: Deep recursion, predicates, documentation

In the material above, we introduced a new model of trees, one in which the empty tree is called nil and we make new trees with node. Using this new model, we might write the following definition of trees.

A tree is either (1) the value nil or (2) a node whose left and right subtrees are, in fact, trees.

We've found it useful to use the list? predicate. Unfortunately, there is no such predicate for this new form of trees.

Document and write a predicate, (tree? val), that holds when val meets the preceding definition of trees. You will be judged not only on the correctness of the solution, but also its concision and elegance of expression.

Here are some examples of that procedure in use.

> (tree? nil)
#t
> (tree? (node 0 nil nil))
#t
> (tree? (node 0 1 nil))
#f
> (tree? (node 0 nil 1))
#f
> (tree? (node 0 (node 1 nil nil) nil))
#t
> (tree? (node 0 (node 1 nil nil) (node 2 nil nil)))
#t
> (tree? (node 0 (node 1 nil (node 3 nil nil)) (node 2 nil nil)))
#t
> (tree? (node 0 (node 1 nil (node 3 nil 5)) (node 2 nil nil)))
#f
> (tree? (cons 1 2))
#f
> (tree? null)
#f

Hint: We've written concise and elegant predicates to test if a pair structure is a list and if a pair structure is a number tree. Refer back to those predicates.

Problem 5: Building trees

Topics: Vectors, divide-and-conquer, numeric recursion

You've discovered how to determine if a value is a tree. But how do we build trees in the first place? One option is to start with a vector and build a tree by repeatedly dividing the vector in half. For example, given the vector #("this" "sample" "vector" "contains" "seven" "string" "values"), we'd note that "contains" is in the middle position, so it will be the top value in the tree. We'll then turn "this", "sample", and "vector" into the left subtree and "seven", "string", and "values" into the right subtree. For the left subtree, "sample" will be at the top, with "this" as its left subtree and "vector" as its right subtree.

Write, but do not document, a procedure, (vector->tree vec) that turns a vector into a tree using that strategy.

> (vector->tree (vector "this" "sample" "vector" "contains" "seven" "string" "values"))
'#(node
   "contains"
   #(node "sample" #(node "this" nil nil) #(node "vector" nil nil))
   #(node "string" #(node "seven" nil nil) #(node "values" nil nil)))
> (vector->tree (vector "this" "sample" "vector" "contains" "six" "values"))
'#(node
   "vector"
   #(node "this" nil #(node "sample" nil nil))
   #(node "six" #(node "contains" nil nil) #(node "values" nil nil)))
> (vector->tree (vector))
'nil
> (vector->tree (vector "aardvark"))
'#(node "aardvark" nil nil)
> (vector->tree (vector "aardvark" "baboon"))
'#(node "aardvark" nil #(node "baboon" nil nil))
> (vector->tree (vector "aardvark" "baboon" "chinchilla"))
'#(node "baboon" #(node "aardvark" nil nil) #(node "chinchilla" nil nil))
> (vector->tree (vector "aardvark" "baboon" "chinchilla" "dingo"))
'#(node
   "baboon"
   #(node "aardvark" nil nil)
   #(node "chinchilla" nil #(node "dingo" nil nil)))
> (vector->tree (vector "aardvark" "baboon" "chinchilla" "dingo" "emu"))
'#(node
   "chinchilla"
   #(node "aardvark" nil #(node "baboon" nil nil))
   #(node "dingo" nil #(node "emu" nil nil)))
> (vector->tree 
   (vector "aardvark" "baboon" "chinchilla" "dingo" "emu" "fox"))
'#(node
   "chinchilla"
   #(node "aardvark" nil #(node "baboon" nil nil))
   #(node "emu" #(node "dingo" nil nil) #(node "fox" nil nil)))

Hint: You may want to write a kernel that keeps track of the boundaries of the portion of the vector you are currently converting.

Problem 6: Binary search trees

Topics: Trees, binary search

What happens if we use vector->tree on a sorted vector, such as the kind we used for binary-search? We get a tree with two interesting characteristics. First, the keys of the entries in the left subtree all may precede the key of the root value, and the key of the root value may precede the keys of all the entries in the right subtree. Second, each subtree has the same characteristic. Basically “smaller things are in the left subtree, larger things are in the right subtree”. Trees with these characteristics are called binary search trees

Write, but do not document, a procedure, (bst-find key bst), that looks for the entry with the given key in the binary search tree bst. You can assume that each entry in the tree has the form (key . value), that the key is a string, and that the key/value pairs are organized as described, with smaller keys in the left subtree and larger keys in the right subtree.

The provided code in the examination includes a variety of sample binary search trees, including animals (animal names, indexed by letter of the alphabet), cs-faculty-first (CS faculty names, indexed by first name), cs-faculty-last (CS faculty names, indexed by last name), and some-grinnell-courses (some Grinnell courses, indexed by course id, such as "CSC151").

As the images suggest, not all binary search trees are perfectly balanced, but all binary search trees follow the organizational principal. As long as they follow that principal, we can search.

> (bst-find "P" animals)
"Polar Bear"
> (bst-find "Z" animals)
"Zorilla"
> (bst-find "J" animals)
"Jackalope"
> (bst-find "a" animals)
. . Could not find "a"
> (bst-find "Sam" cs-faculty-first)
"Rebelsky"
> (bst-find "Samuel" cs-faculty-first)
. . Could not find "Samuel"
> (bst-find "Weinman" cs-faculty-last)
"Jerod"
> (bst-find "Sam" cs-faculty-last)
. . Could not find "Sam"
> (bst-find "SST295" some-grinnell-courses) ; Duplicate keys
"Real Life Entrepreneurship"
> (bst-find "POL295" some-grinnell-courses)
"Intro to Network Analysis"
> (bst-find "CSC299" some-grinnell-courses)
. . Could not find "CSC299"

Hint: When you encounter a tree node, check its key and then recurse on either the left or right subtree.

Problem 7: Flattening trees

Topics: Trees, deep recursion, program analysis, code reading

In an earlier problem, we turned a vector into a tree. But sometimes we want to “flatten” trees back into linear structures, such as lists. Here's a procedure that does just that.

;;; Procedure:
;;;   tree-flatten
;;; Parameters:
;;;   tree, a tree
;;; Purpose:
;;;   "Flatten" the tree into the corresponding list.
;;; Produces:
;;;   lst, a list
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   Every value in the tree appears in the list.
;;;   No additional values appear in the list.
;;;   The values are in the same order in the list as they are
;;;     in the tree.  (Things in the left subtree appear before
;;;     the value in a node, which appears before things in the
;;;     right subtree.
(define tree-flatten
  (lambda (tree)
    (if (nil? tree)
        null
        (list-append (tree-flatten (left-subtree tree))
                     (cons (root-value tree)
                           (tree-flatten (right-subtree tree)))))))

And here's the procedure in action.

> (tree-flatten (node
                 "chinchilla"
                 (node "aardvark" 
                       nil 
                       (node "baboon" nil nil))
                 (node "emu" 
                       (node "dingo" nil nil) 
                       (node "fox" nil nil))))
'("aardvark" "baboon" "chinchilla" "dingo" "emu" "fox")

You may be worried that the procedure is potentially inefficient because it uses list-append, and you'd be right to be worried.

a. Update list-append and tree-flatten so that you can count the calls to cons.

b. Find the number of calls to cons for each of the following trees. You may also find it useful to visualize each of the trees with a command like (visualize-tree t3 200 200).

(define t1
  (node 4 (node 3 (node 2 (node 1 nil nil) nil) nil) nil))

(define t2
  (node 3 (node 2 (node 1 nil nil) nil) (node 4 nil nil)))

(define t3
  (node 3 (node 1 nil (node 2 nil nil)) (node 4 nil nil)))

(define t4
  (node 2 (node 1 nil nil) (node 4 (node 3 nil nil) nil)))

(define t5
  (node 2 (node 1 nil nil) (node 3 nil (node 4 nil nil))))

(define t6
  (node 1 nil (node 2 nil (node 3 nil (node 4 nil nil)))))

(define t7
  (node 1 nil (node 3 (node 2 nil nil) (node 4 nil nil))))

c. Which tree seems to require the most calls to cons? Why does it require so many calls?

d. Which tree seems to require the fewest call to cons? Why does it require so few calls?

e. Write a new version of tree-flatten (which you should call tree-flatten-new) in which the number of calls to cons is the same as the number of values in the list.

> (counter-reset! cons-counter) (tree-flatten-new t1) (counter-print! cons-counter)
'#("cons" 0)
'(1 2 3 4)
cons: 4
> (counter-reset! cons-counter) (tree-flatten-new t2) (counter-print! cons-counter)
'#("cons" 0)
'(1 2 3 4)
cons: 4
> (counter-reset! cons-counter) (tree-flatten-new t3) (counter-print! cons-counter)
'#("cons" 0)
'(1 2 3 4)
cons: 4
> (counter-reset! cons-counter) (tree-flatten-new t4) (counter-print! cons-counter)
'#("cons" 0)
'(1 2 3 4)
cons: 4
> (counter-reset! cons-counter) (tree-flatten-new t5) (counter-print! cons-counter)
'#("cons" 0)
'(1 2 3 4)
cons: 4
> (counter-reset! cons-counter) (tree-flatten-new t6) (counter-print! cons-counter)
'#("cons" 0)
'(1 2 3 4)
cons: 4
> (counter-reset! cons-counter) (tree-flatten-new t7) (counter-print! cons-counter)
'#("cons" 0)
'(1 2 3 4)
cons: 4

Hint: You will find that you will require fewer calls to cons if you (a) write a helper that permits you to pass around a partially flattened tree and (b) flatten the right half of the tree before the left half.

Reference for Tree Procedures

(left-subtree nod)
Experimental Mediascheme Tree Procedure. Gets the left subtree of the given binary tree node.
nil
Experimental Mediascheme Tree Constant. The empty tree.
(nil? val)
Experimental Mediascheme Tree Procedure. Determines if a Scheme value represents the empty tree.
(node val left right)
Experimental Mediascheme Tree Procedure. Create a new binary tree node with the specified value, left subtree, and right subtree.
(node? val)
Experimental Mediascheme Tree Procedure. Determines if a Scheme value is a node.
(right-subtree nod)
Experimental Mediascheme Tree Procedure. Gets the right subtree of the given binary tree node.
(root-value nod)
Experimental Mediascheme Tree Procedure. Gets the value from the given binary tree node.
(tree-depth tree)
Experimental Mediascheme Tree Procedure. Find the length of the longest direct path from the top of a tree to its farthest leaf.
(tree->code tree)
Experimental Mediascheme Tree Procedure. Builds something resembling Scheme code to construct tree. (Works best with trees with simple values, rather than compound values.)
(visualize-tree tree width height)
Experimental Mediascheme Tree Procedure. Creates a simple visualization of the tree in a new image of the specified width and height. The visualization is simple enough that if the entries in the tree are long, they will sometimes reach off the side of the image or overlap each other.

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 sections have the same exam?
Yes.
Does our exam need to be in the body of the email, or will you accept attachments?
Neither. You should submit via Web form.
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 (not on this exam, but on the class). There's likely some concept you're missing, and we can help figure that out.
What do you mean by “implement”?
Write a procedure or procedures that accomplish the given task.
Do we have to make our code concise?
You should strive for readable and correct code. If you can make it concise, that's a plus, but concision is secondary to readability and correctness. Long or muddled code is likely to lose points, even if it is correct.
Much of your sample 6P-style documentation has incomplete sentences. Can we follow that model? That is, can we use incomplete sentences in our 6P-style documentation?
Yes, you can use incomplete sentences in 6P-style documentation.
You tell us to start the exam early, but then you add corrections and questions and answers. Isn't that contradictory? Aren't we better off waiting until you've answered the questions and corrected any errors?
That's one of the reasons we give extra credit to those who work on the exam early. But you're also better able to get your questions answered early if you start early (or at least we think you are). Later questions will generally be told “See the notes on the exam”.
Is there a way to see our prologues?
I have not figured out how to make Google Docs give you your own data back. If you want your prologue, email me and I'll send it to you. In the future, you should probably answer the prologue questions in a separate document and then copy and paste into the Google Form.
How do we know what our random number is?
You should have received one in class. If you need a new one, there's a stack in the back of our classroom.
When we print the exam, do you prefer one problem per page (one on the front of the page, one on the back, unless more is needed)? If not, is it okay to have a blank line between each problem?
Either is fine. If you do go the less paper route, make sure to insert a few blank lines between problems, as well as a comment like ; --------- PROBLEM 2 -------------. I've put such separators in the template code.
To show we’ve tested the code informally, would you just like us to just post the inputs we used to test the procedure? If so, how should we list those?
Copy and paste the interactions pane into the appropriate place in the definitions pane. Select the text. Under the Racket menu, use "Comment out with semicolons."
Can I just write the first four P's for my helper procedures if I don't make them local?
Yes.
Have you changed the preliminary information on the exam since exam 3?
The substantial changes I've made to the preliminary information are (1) to add a link to a file that provides you with code for the examination, (2) to add some more detail to the checklist, and (3) to indicate that you should remove the utility code before printing your examination. The other changes are much less substantial. Among other things, I have changed a few words for clarity and I have changed due dates.
Have you tried to solve each of the problems? If so, how long did they take you?
I solved problems 2-7 while writing the exam, mostly so that I could generate sample output. (All of these problems are new!) Each of those problems took me under five minutes, but I've been writing procedures like these for a long time. I solved problem 1 the last time I gave that problem, which was fun because I don't know everything my colleague put in as an error. It usually takes me about thirty minutes to write a good test suite, and I think it took about that long to write the test suite.
I don't expect to be able to do every problem on this exam. Where should I start?
Everyone should be able to write a test suite, so I'd suggest starting with problem 1. The first half of problem 7 requires that you make a few small changes to a program and then record your results. That should be doable. Problem 4 derives directly from something we did in class.
I'm writing code something like the following. (Code modified so as to avoid giving away answers.)
(if (pred? (car (kernel values)))
    (car (kernel values))
    (cdr (kernel values)))
I know that you don't like repeated calls to the same procedure with the same parameters. How much are you likely to take off?
[2015-05-02] I will take off at least two points for each instance in which you write such inelegant and inefficient code.
Did someone really ask the previous question?
[2015-05-02] No, but someone sent me code that was that inefficient, and I thought I should reiterate just how much I object to such inefficiencies.

Problem 1

Can we do this problem on our virtual machines?
While you can certainly write the test suite on your virtual machine, the incorrect implementation exists only in MathLAN. You'll need to run your tests on a MathLAN workstation.
I've found that the revised assoc procedure sometimes gives an error rather than an expected result. Do I count that in the four (or five) problems?
[2015-04-30] Yes. Incorrect results and inappropriate errors are both illustrations of problems with the underlying implementation.
What should my test suite do with the original version of assoc?
[2015-04-30] Since the standard version of assoc should be correct, your test suite should find no errors in that version.
What is this “terminal” of which you speak?
[2015-04-30] It's the application that lets you type commands to interact with our Linux workstations. You can access it by clicking the small picture of a computer screen from the bar at the bottom of the window.
How can I see the source code of the version of assoc that we are testing?
[2015-05-02] You can't see the version of assoc you're supposed to be writing the test suite for. The goal is to write the test suite without knowing how it is implemented.
How many tests is enough?
[2015-05-04] Enough that you think you've tested assoc thoroughly. If you haven't found four different errors, you clearly haven't written enough tests. But I suppose it's possible to write tests that find four different errors and is still clearly incomplete. (That's unlikely, but possible.)

Problem 2

When I call the lookup procedure, it returns another procedure, rather than a value? What's going wrong?
[2015-04-30] This problem seems to be a common one. In each case I've seen, the programmer was using recursion, and calling table->lookup recursively. But table->lookup returns a function, so you end up with a function as a result. You should be able to solve this problem without using explicit recursion. If you do use recursion, you probably want to have a recursive kernel.
How do I get a procedure to return another procedure?
[2015-05-01] Read the reading on higher-order procedures. Focus on the section entitled “Returning Procedures”.
I'm using assoc in my solution, and it seems to work the same as yours, except that I get the wrong answer for (cname->rgb-list "white"). Do you think my solution is wrong.
[2015-05-02] I'm glad to hear that you are using assoc in your solution. However, you should make sure that you are using the correct version of assoc. If you still have the (require CSC151-2015S/a-sock), you'll be using the incorrect version.
Are you grading us on the quality of our test suite or the number of errors we find?
[2015-05-03] I'm grading you on the quality of your test suite. But a good test suite should find at least four different kinds of errors.
I get an error message if I search for 1 in '(1 2 3). Is that one of the problems with the buggy assoc?
[2015-05-03] No. '(1 2 3) is not an association list. If you don't meet the preconditions for a procedure, it can do almost anything.

Problem 3

Problem 4

Do you have any hints?
[2015-04-30] You wrote listp? in the lab on pairs and pair structures. You wrote color-tree? in the lab on trees. You should be able to use the ideas from those two procedures as a starting point.
I keep getting the following error message. Can you explain why?
. . vector-ref: contract violation
 expected: vector?
 given: 1
 argument position: 1st
 other arguments...:
[2015-05-02] It's likely that you are calling left-subtree or right-subtree on a value that is not a node.

Problem 5

Do you have any hints?
[2015-04-30] I've found that the following series of questions has been remarkably successful at eliciting an “a hah!” moment in students. 1. Suppose there are 71 elements in the vector. What are the indices of the elements? 2. Which of those elements becomes the root of the tree? (What is the index of the element that becomes the root of the tree?) 3. Which of those elements will be in the left subtree? (What are the indices of those elements?) 4. Which of those elements will be in the right subtree? (What are the indices of those elements?) 5. Now, let's focus on the right subtree. What element will be the root of its subtree? (What is its index?) How did you compute that index? 6. What elements will be in its left subtree? (What are their indices?) 7. What elements will be in its right subtree? (What are their indices?)
Does the output have to have the same indentation as in the example?
[2015-05-01] No. DrRacket controls that indentation, not you. As long as you build the correct structure, you should be fine.
Do you have any suggestions on how we should think about keeping track of indices into the vector for the recursive calls?
[2015-05-02] You should revisit the implementation of binary search for ideas.

Problem 6

Do you have any hints?
[2015-04-30] We went through an example in class on Friday, 1 May 2015.
Can we assume that the keys are strings?
[2015-05-02] Yes.
Is it okay if my binary search is case insensitive? Your examples suggest that it should be case sensitive.
[2015-05-04] Yes, you may write a case-insensitive binary search.

Problem 7

Do you have any hints on part 4?
[2015-04-30] We went through an example in class on Friday, 1 May 2015.
How should I flatten the subtrees?
[2015-05-01] With recursive calls to the kernel. Your goal is to get the ordering right and to figure out what the appropriate value the parameters are.
Any more hints?
[2015-05-02] Look at other tree procedures that require you to process both the left subtree and the right subtree. Think about how the parameters to those two recursive calls might differ.
Is everyone having as many problems with 7e as I am?
[2015-05-02] 7e is clearly the most challenging problem on the test. Yes, many people are having difficulty. But if you understand deep recursion (aka tree recursion) and you think carefully about the example from class, you should be able to solve it.
My solution has a test of the form (if (nil? (right-subtree tree)) ...). Do you think that's a good approach?
[2015-05-02] No. If you look at other procedures that use deep recursion, you'll find that we almost never ask whether or not the right subtree is empty.

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.)

  • Incorrect require instructions for a-sock. [CZ, 1 point]
  • The exam4.rkt file uses the wrong require for problem 1. It had (require CSC151/assoc); it should have (require CSC151-2015S/a-sock). [AL and YZ, 1 point]
  • Formatting issues on problem 3. [JMU, 1/2 point]
  • Not-quite-correct test description in the sample code. (The text says that we're searching for 6; we're actually searching with a key of 5.) [CR, 1/2 point]
  • The require for assoc has to be in the definitions pane. [SR, 1/2 point]
  • There are too many components in the table entry for "red". [SR, 0 points]
  • Sam is unable to spell “baboon”. [LB, 1/2 point]
  • flattened should be initialized as null rather than nil. [??, 1 point]

Citations

Some 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 difficult, if not impossible.

Some problems on this exam were inspired by conversations with our students and by correct and incorrect student solutions on a variety of problems. 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.