Exam 3: Sophisticated Scheming


Assigned: Monday, 28 November 2011

Due: 11:59 p.m., Monday, 5 December 2011

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 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 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 if you give evidence of a serious attempt on at least 6 of the problems or come talk to me about the exam no later than one week after it is returned.

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.

While your electronic version is due at 11:59 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 be considered a misrepresentation of your work and referred to the Committee on Academic Standing.

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 use examples other than those that may be provided with the exam. Do not include images; I should be able to regenerate those.

Unless I state otherwise, you must document any top-level (i.e., not local) procedures you write using the six-P documentation style. You are strongly encouraged to use local procedure bindings to encapsulate helper procedures.

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 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: Observing the Distribution of Simulated Dice Rolls

Topics: Vectors, documentation, verifying preconditions, higher-order procedures, numeric recursion, randomness.

In this part of the exam, you will develop tools to record the number of occurrences of each possible value (2 through 12) in a series of simulated dice rolls.

Problem 1: Documenting vector-ref-mutate!

Write 6 Ps documentation for 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. Here are some sample runs of such a procedure.

> (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: Implementing vector-ref-mutate!

Implement the vector-ref-mutate! procedure. Your procedure should verify its preconditions to the extent possible. You need not verify any properties of proc other than its type.

Problem 3: Counting rolls

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, because 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: Listing Substrings

Topics: Strings, numeric recursion, local procedure bindings.

Problem 4: Listing fixed-length substrings

As you may recall, (substring str start finish) extracts a substring from str that runs from position start to position finish - 1. The string-length procedure lets you find the length of a string.

Write, but do not document, a procedure, (substrings str len), that generates a list of all the substrings of str of length len. For example,

> (substrings "television" 3)
("tel" "ele" "lev" "evi" "vis" "isi" "sio" "ion")

Problem 5: Listing all substrings

Write, but do not document, a procedure (all-substrings str) that finds all substrings of a given string. For example,

> (all-substrings "computer")
("c" "o" "m" "p" "u" "t" "e" "r" "co" "om" "mp" "pu" "ut" "te" "er" "com" "omp" 
"mpu" "put" "ute" "ter" "comp" "ompu" "mput" "pute" "uter" "compu" "omput" 
"mpute" "puter" "comput" "ompute" "mputer" "compute" "omputer" "computer")

Hint: Your procedure should call the substrings procedure from the previous problem.

Part C: Implementing a Cipher

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 6: Shifting Characters

Write, but do not document, a procedure (char-shifter amount), which produces a procedure that takes a character and shifts it by amount, accounting for any wrap-around. Your procedure only needs to work for upper-case letters.

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

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

Problem 7: 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, accounting for wrap-around. Again, your solution only needs to work for strings consisting of upper-case letters.

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 D: A Potpourri of Higher-Order Procedures

Problem 8: Checking trees

Topics: Deep recursion, pair structures, predicates.

You have written a predicate color-tree? in the lab on trees that determines whether every leaf in a tree is a color. Generalize this idea by writing the predicate (tree-all? pred? tree), which indicates whether the predicate pred? is satisfied for all leaves in tree. You need not document tree-all?.

For example,

> (tree-all? number? (cons (cons (cons 1 2) (cons 3 4)) (cons 5 (cons 6 7))))
#t
> (tree-all? even? (cons (cons (cons 1 2) (cons 3 4)) (cons 5 (cons 6 7))))
#f
> (tree-all? char? (cons (cons #\a #\b) #\c))
#t
> (tree-all? char? (cons (cons #\a #\b) "c"))
#f
> (tree-all? odd? 1)
#t
> (tree-all? char? 1)
#f

Problem 9: Further generalizing insertion sort

Topics: Sorting, vectors, numeric recursion, higher-order procedures.

In the reading on sorting and the lab on insertion sort, you encountered a procedure list-keyed-insertion-sort, which could sort records stored in a list by any element of the record, not just the first element. It would be helpful to have such a procedure for records stored in a vector, as well.

Write a procedure, (vector-keyed-insertion-sort! vec get-key may-precede?) that sorts a vector of compound objects by key. You do not need to document vector-keyed-insertion-sort!.

For example,

> (define elements-mythology 
          ; Source: http://www.enotes.com/topic/List_of_chemical_elements_named_after_people
          (vector (list "iridium" "Ir" 77 "Iris" "greek")
                  (list "niobium" "Nb" 41 "Niobe" "Greek")
                  (list "promethium" "Pm" 61 "Prometheus" "Greek")
                  (list "tantalum" "Ta" 73 "Tantalus" "Greek")
                  (list "thorium" "Th" 90 "Thor" "Norse")
                  (list "titanium" "Ti" 22 "Titans" "Greek")
                  (list "vanadium" "V" 23 "Vanadis (Freyja)" "Scandanavian")))
> (vector-keyed-insertion-sort! elements-mythology caddr <=)
> elements-mythology
#(("titanium" "Ti" 22 "Titans" "Greek")
  ("vanadium" "V" 23 "Vanadis (Freyja)" "Scandanavian")
  ("niobium" "Nb" 41 "Niobe" "Greek")
  ("promethium" "Pm" 61 "Prometheus" "Greek")
  ("tantalum" "Ta" 73 "Tantalus" "Greek")
  ("iridium" "Ir" 77 "Iris" "Greek")
  ("thorium" "Th" 90 "Thor" "Norse"))

While it is possible to write this procedure by converting the vector to a list, using list-keyed-insertion-sort!, and then converting the result back to a vector, you should not use this strategy. Rather, write this procedure directly using numeric recursion over the positions in the vector. You may want to use vector-insertion-sort! as a template.

Problem 10: Documenting list-subtract

Topics: Higher-order procedures, code reading, documentation.

Many implementations of Scheme contain the following procedures.

(define negate
  (lambda (pred?)
    (lambda (val)
      (not (pred? val)))))

(define select
  (lambda (lst pred?)
    (cond
      ((null? lst)
       null)
      ((pred? (car lst))
       (cons (car lst) (select (cdr lst) pred?)))
      (else
       (select (cdr lst) pred?)))))

(define member?
  (lambda (val lst)
    (and (not (null? lst))
         (or (equal? val (car lst))
             (member? val (cdr lst))))))

Suppose we've defined the following procedure using those standard procedures.

(define list-subtract
  (lambda (lst1 lst2)
    (select lst2 (negate (r-s member? lst1)))))

Read and experiment with list-subtract to understand its purpose and use. Then write 8 Ps documentation for list-subtract. In addition to the standard 6 Ps, include two additional sections:

  • Practicum, an example of how to use the procedure and the result that it produces, and
  • Process, a brief explanation of the algorithm (no more than two sentences, please).

Some questions and answers

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

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.

  • An earlier version of the exam had two Problem 8s and no Problem 6. [AH, 1 point]

Jerod Weinman

Copyright © 2007-2011 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License .