Exam 3: Sophisticated Scheming


Assigned: Monday, 29 November 2010

Due: Beginning of class, 11 a.m Monday, 6 December 2010

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.

There are ten 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 two to three hours, depending on how well you've learned the topics and how fast you work. You should not work more than four hours on this exam. Stop at four hours and write “There's more to life than CS” and you will earn at least a 70 on this exam.

I would also appreciate it if you would write down the amount of time each problem takes. Each person who does so will earn two points of extra credit. Since I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they have completed the exam in three hours or less or have spent at least three hours on the exam. In the latter case, they should also report on what work they've completed in the three hours. After receiving such notices, I may change the exam.

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 the other members of your group. If you use code that you found on the course Web site, be sure to cite that code. You need not cite the code provided in the body of the examination.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web. And, in case it's not clear, you may not ask others (in person, via email, via IM, by posting a please help message, or in any other way) to put answers on the Web.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say “This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam.” You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must include both of the following statements on the cover sheet of the examination.

  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 Weinman (that's me) or Professor Davis.

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. You should create the emailed version by copying the various parts of your exam and pasting them into an email message. In both cases, you should put your answers in the same order as the problems. Failure to name and number the printed pages will lead to a penalty of two points. Failure to turn in both versions may lead to a much worse penalty.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code. Do not include images; I should be able to regenerate those.

Unless I state otherwise, you should document any procedures you write using the six-P documentation style.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.

I will give partial credit for partially correct answers. I am best able to give such partial credit if you include a clear set of work that shows how you derived your answer. You ensure the best possible grade for yourself by clearly indicating what part of your answer is work and what part is your final answer.

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 (7 a.m. - 6 p.m.), feel free to try to call me in the office (269-9812).

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

Part A: Vectors

Problem 1: Vector Mutation

Topics: Vectors, verifying preconditions, higher-order procedures

Write a procedure (vector-ref-mutate! vec index proc) that applies proc to the item in vec at index, storing the result in the same location.

The procedure vector-ref-mutate! should verify its preconditions. However, you need not verify any properties of proc other than its type.

> (define numbers (make-vector 4 0))
> (vector-ref-mutate! numbers 2 increment)
> numbers
#(0 0 1 0)
> (vector-ref-mutate! numbers 3 (l-s + 5))
> numbers
#(0 0 1 5)
> (vector-ref-mutate! numbers 4 decrement)
vector-ref-mutate!: index 4 out of bounds for vector #(0 0 1 5)
> (vector-ref-mutate! numbers 0 5)
vector-ref-mutate!: expected procedure for 3rd parameter; given 5

Problem 2: Counting Rolls

Topics: Vectors, numeric recursion, randomness

The lab on randomized drawing used the following two procedures to simulate rolling a pair of dice.

;;; Procedure:
;;;   roll-a-die
;;; Parameters:
;;;   None
;;; Purpose:
;;;   To simulate the rolling of one six-sided die.
;;; Produces:
;;;   An integer between 1 and 6, inclusive.
;;; Preconditions:
;;;   [None]
;;; Postconditions:
;;;   Returns an integer between 1 and 6, inclusive.
;;;   It should be difficult (or impossible) to predict which
;;;     number is produced.
(define roll-a-die
  (lambda ()
    (+
      (random 6)    ; a value in the range [0 .. 5]
      1)))          ; now in the range [1 .. 6]

;;; Procedure:
;;;   pair-a-dice
;;; Parameters:
;;;   [None]
;;; Purpose:
;;;   Roll two six-sided dice and find their sum.
;;; Produces:
;;;   roll, an integer
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   2 <= roll <= 12
;;;   Across multiple calls, the various rolls have the same probabilities 
;;;     as we would get from rolling two dice.
(define pair-a-dice
  (lambda ()
    (+ (roll-a-die) (roll-a-die))))

Computational scientists often find it difficult to analyze quantities mathematically and resort to simulation to get an approximate answer.

Write, but do not document, a procedure (count-rolls n) that rolls a pair of six-sided dice n times. It should produce a vector of length 13, where the value at each index is the number of times that total (the index) came up.

Hints: Indices 0 and 1 will always contain zero, since it is not possible to roll two dice and produce a zero or a one. You should use the procedures pair-a-dice and vector-ref-mutate! in your solution.

> (count-rolls 1)
#(0 0 0 0 0 0 0 0 0 0 1 0 0)
> (count-rolls 100)
#(0 0 2 6 15 12 10 15 12 12 11 4 1)

Part B: Strings, Characters, and Ciphers

Topics: Characters, strings, higher-order procedures

A substitution cipher is a code that simply replaces one character with another in a one-to-one fashion. This is the kind of code that must be broken when solving the typical newspaper puzzle called a "cryptogram." A special case of this code is called a Caesar cipher, named for the Roman emperor Julius Caesar, who used it to communicate with his generals. A Caesar cipher simply shifts each character to another a certain number of places down the alphabet. In a Caesar cipher with a shift of 3, the letter A (character 0) would become D (character 3), P (character 15) would become S (character 18), and so on. Typically, Y (character 24) would wrap-around to the front of the alphabet to become B (character 27, or, actually character 1).

Problem 3: Shifting Characters

Write and document a procedure (char-shifter amount), which produces a procedure that takes a character and shifts it by amount, ignoring any wrap-around.

You should strive to make your procedure as concise as possible.

> (define char-encode-1 (char-shifter 5))
> (char-encode-1 #\H)
#\M

Problem 4: Making Encoders

Of course, Caesar ciphers are much more interesting when applied to strings, rather than to individual characters. It is also natural to think of a cipher as a function from strings to strings, rather than from numbers to strings. Hence, we'd like a procedure that builds ciphers.

Write, but do not document, a procedure (shift-encoder amount) that returns a procedure that takes a string as input and produces a string where each character is replaced by another whose collating sequence number differs from the original by amount.

You should strive to make your procedure as concise as possible.

> (define encode-1 (shift-encoder 5))
> (encode-1 "HELLO")
"MJQQT"
> (define encode-2 (shift-encoder 2))
> (encode-2 "MJQQT")
"OLSSV"
> (define decode-12 (shift-encoder -7))
> (decode-12 "OLSSV")
"HELLO"
> ((o decode-12 encode-2 encode-1) "WORLD")
"WORLD"

Part C: Testing and analysis

Topics: Unit tests, analysis, list recursion, searching, sorting.

Problem 5: Testing assoc

Consider the following implementation of assoc:

(define assoc
  (lambda (key dict)
    (letrec ((kernel
              (lambda (remaining)
                (cond ((null? remaining) null)
                      ((= key (caar remaining)) (car remaining))
                      (else (kernel (cdr remaining))))))
             (all-pairs? 
               (lambda (lst)
                 (or (null? lst) 
                     (and (pair? (car lst)) (all-pairs? (cdr lst)))))))
      (if (or (not (list? dict)) (not (all-pairs? dict)))
          (error "assoc: expected a list of key-value pairs; given" dict)
          (kernel (cdr dict))))))

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)

However, our implementation has several bugs. Using begin-tests!, end-tests!, test!, and test-error!, write a test suite for the assoc procedure. Consult the reading on association lists for a refresher on the correct behavior of assoc.

Note: You should include a comment with each test indicating its purpose. For example:

; Known equilateral triangle
(test! (classify-triangle 1 1 1) "equilateral") 

; Impossible triangle: hypotenuse exceeds sum of other two sides
(test-error! (classify-triangle 1 1 3)) 

Hint: Your test suite should include at least three tests that pass for the built-in assoc procedure and fail for our buggy procedure.

Problem 6: Analyzing splits

Note: This problem was revised on Tue, Nov 30.

Recall that merge sort is a divide-and-conquer algorithm: We make the sorting problem smaller by dividing the list into two halves, which are then sorted.

In the reading on merge sort, you were given the following implementation of a split procedure:

;;; Procedure:
;;;   split
;;; Parameters:
;;;   lst, a list
;;; Purpose:
;;;   Split a list into two nearly-equal halves.
;;; Produces:
;;;   halves, a list of two lists
;;; Preconditions:
;;;   lst is a list.
;;; Postconditions:
;;;   halves is a list of length two.
;;;   Each element of halves is a list (which we'll refer to as
;;;     firsthalf and secondhalf).
;;;   lst is a permutation of (append firsthalf secondhalf).
;;;   The lengths of firsthalf and secondhalf differ by at most 1.
;;;   Does not modify lst.
;;;   Either firsthalf or secondhalf may share cons cells with lst.
(define split
  (lambda (lst)
    ;;; kernel
    ;;;   Remove the first count elements of a list.  Return the
    ;;;   pair consisting of the removed elements (in order) and
    ;;;   the remaining elements.
    (let kernel ((remaining lst) ; Elements remaining to be used
                 (removed null)  ; Accumulated initial elements 
                 (count          ; How many elements left to use
                  (quotient (length lst) 2)))
      ; If no elements remain to be used,
      (if (= count 0)
          ; The first half is in removed and the second half
          ; consists of any remaining elements.
          (list (reverse removed) remaining)
          ; Otherwise, use up one more element.
          (kernel (cdr remaining)
                  (cons (car remaining) removed)
                  (- count 1))))))

Consider the following alternative, split-v2:

(define split-v2
  (lambda (lst)
    (let kernel ((rest lst)
                 (left null)
                 (right null))
      (if (null? rest)
          (list left right)
          (kernel (cdr rest) (cons (car rest) right) left)))))

In this problem, you will use define$ and analyze to count the number of steps taken by each version of split, and then you will interpret the results. Recall that computer scientists analyze how many steps it takes to run an algorithm based on the size of its parameters. That is, if an algorithm is given a list of length N, we might want to know that that algorithm takes 4N+3, or N(N-1)/2, or log2(N) steps.

(a) Consider calling the procedure split on a list of length N. How many calls to the kernel procedure are required? Express the number of calls to kernel in terms of N.

(b) Consider calling the procedure split-v2 on a list of length N. How many calls to the kernel procedure are required? Express the number of calls to kernel in terms of N.

Now consider using split or split-v2 in the context of the merge-sort procedure from the reading on merge sort.

(c) Again using define$ and analyze, compare how split and split-v2 perform in the context of the merge-sort function.

  • How many calls to split's kernel procedure are made when merge sorting lists of length 2, 4, 8, 16, and 32?
  • Modify the code for merge-sort so that it uses the split-v2 function instead of split. Now, how many calls to split-v2's kernel procedure are made when merge sorting lists of length 2, 4, 8, 16, and 32?
  • Which is preferable, split or split-v2?

Note: After you complete the rest of the exam, you may wish to attempt the OPTIONAL parts for bonus points, below.

(d) (OPTIONAL) Consider calling (merge-sort (random-numbers N) <=). Express the number of calls to split's kernel procedure in terms of N.

(e) (OPTIONAL) Modify the code for merge-sort so that it uses the split-v2 function instead of split. Consider calling (merge-sort (random-numbers N) <=). Express the number of calls to split-v2's kernel procedure in terms of N.

Part D: Trees

Topics: Trees, recursion

Problem 7: Shortest Branch

Recall from the the reading on trees that the depth of a tree can be defined as the greatest number of hops (or links) from the root to a leaf. One might also be interested in the shortest number of hops from the root to a leaf. Write, but do not document, a procedure (tree-min-depth tree) that calculates the distance to the shallowest leaf of its argument.

> (tree-min-depth (cons 'a 'b))
1
> (tree-min-depth (cons 'a (cons 'b 'c)))
1
> (tree-min-depth (cons (cons 'a 'b) (cons 'c 'd)))
2
> (tree-min-depth (cons (cons 'a 'b) (cons (cons 'c 'd) 'e)))
2
> (tree-min-depth 'a)
0

Problem 8: Tree Balancing

A "balanced" tree is one which has no subtrees (i.e., it is simply a leaf) or whose two subtrees have the same height 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?
 (lambda (tree)
   (or (not (pair? tree))
       (let ((left-height (tree-depth (car tree)))
             (right-height (tree-depth (cdr tree))))
         (and (= left-height right-height)
              (tree-balanced? (car tree))
              (tree-balanced? (cdr tree)))))))

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

> (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

Part E: Matrices

Topics: Higher-order procedures

It is common to store a matrix (a two-dimensional grid of numbers) as a list of lists, with each inner list containing the numbers for an entire row. For example we might represent a 3x4 matrix (three rows and four columns) as a list like the following:

;; Store the matrix 
;; | 1 2 3 4 |
;; | 3 1 3 0 |
;; | 5 8 7 3 |
(define mtx-example (list (list 1 2 3 4)
                          (list 3 1 3 0)
                          (list 5 8 7 3)))

Problem 9: Tabulating Rows

Write, but do not document, a procedure (matrix-row-sum matrix) that returns a list containing the totals of all the numbers in each row of matrix, a list of lists as described above.

> (matrix-row-sum mtx-example)
(10 7 23)

Note: You should NOT implement this procedure using recursion.

Problem 10: Tabulating Columns

Write, but do not document, a procedure (matrix-col-sum matrix) that returns a list containing the totals of all the numbers in each column of matrix, a list of lists as described above.

> (matrix-col-sum mtx-example)
(9 11 13 7)

Note: You should NOT implement this procedure using recursion. In addition to map and apply, you will find it helpful to use cons.

Some questions and answers

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

Problem 1

Question: Do I have to match the error messages exactly?
Answer: No. It's possible to, but you don't have to.

Problem 3

Question: In Problem 3 of our exam, we were asked to write the procedure "ignoring any wrap-around", I'm not sure what this means, could you elaborate? My first guess would be that all shifts should not surpass, or stop at the character #\Z but I'm not sure.
Answer: You are correct. This means you need not worry about shifts that may surpass/exceed #\Z or any other character value.

Problem 5

Question: On question 5, you ask to build a suite of tests. Will it suffice to just show three cases in which the buggy assoc doesn't work like it should? Or do I need to show cases of when it does work as well?
Answer: In short, the latter. Building a test suite involves writing a set of inputs (and outputs or errors) that would convince someone the code is completely correct. Or, as the reading puts it, "a collection of tests that are unlikely to all succeed if the code being tested is erroneous." It may help to answer the question: what are the ways a program could fail to be correct?

Problem 6

Question: can we use the floor and ceiling procedures for expressing the number of steps it takes?
Answer: If you wish, but we will accept answers that are only strictly correct for even numbers.

Problems 9 and 10

Question: can we assume that the matrix will always be the same size, or should it be able to preform its operation on any matrix size?
Answer: The matrix should be any size, but of course that still means that each row in a given matrix has the same number of columns in it.

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.

  • There was a missing parenthesis in the example runs for problem 5 [EL, 1 point]
  • The final error message shown in the examples for Problem 1 was inappropriate to the input. [DU, 1 point]
  • The name of the merge-sort function was spelled without its hyphen. [AM, 1 point]
  • Problem 2 referred to "die" (singular) where it should have said "dice" (plural). [AG, 1 point]

Jerod Weinman