Fundamentals of Computer Science I (CS151 2003F)

Exam 3: Algorithms and Program Design

Distributed: Monday, 1 December 2003
Due: 2:15 p.m., Tuesday, 9 December 2003
No extensions.

This page may be found online at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Exams/exam.03.html.

Contents

Preliminaries

There are four problems on the exam. Some problems have subproblems. Each full problem is worth twenty-five points. The point value associated with a problem does not necessarily correspond to the complexity of the problem or the time required to solve the problem.

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than that limitation, you should feel free to use all reasonable resources available to you. As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web (at least not until after I return exams to you). And, in case it's not clear, you may not ask others (in person, via email, or by posting a please help message) to put answers on the Web.

This is a 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 exam is likely to take you about four to six hours, depending on how well you've learned topics and how fast you work. You should not work more than eight hours on this exam. Stop at eight hours and write There's more to life than CS and you will earn at least 80 points on this exam. I would appreciate it if you would write down the amount of time each problem takes. I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. Since I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they've spent at least five hours on the exam or completed the exam. (At that point, I may then change the exam.)

You must include both of the following statements on the cover sheet of the examination. Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. 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 myself or our teaching assistant.

1. I have neither received nor given inappropriate assistance on this examination.
2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

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

You must both answer all of your questions electronically and turn in a printed version of your exam. That is, you must write all of your answers on the computer, print them out, number the pages, put your name on every page, and hand me the printed copy. You must also email me a copy of your exam by copying your exam and pasting it into an email message. Put your answers in the same order as the problems. If you write your name at the top of each sheet of the printed copy, you will earn two points of extra credit.

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 document all of your primary procedures. In most cases, a few sentences will suffice. In a few cases, I'll ask you to provide the full documentation (including parameters, purpose, value produced, preconditions, and postconditions). If you write helper procedures (and you may certainly write helper procedures) you should document those with a few short notes. When appropriate, you should include short comments within your code. You should also take care to format your code carefully.

Just as you should be careful and precise when you write code 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.

I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by emphasizing your answer and including a clear set of work that you used to derive the answer.

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).

I will also reserve time at the start of classes this week and next to discuss any general questions you have on the exam.

Problems

Problem 1: Generating HTML Tags

Key Topics: Strings, Variable-Arity Procedures, Pairs

Tad and Talia Tagger have been looking at my webutils.scm library. They like the concept of the library in the abstract, but object to many of the particulars. They particularly dislike the markup procedure, which I've written as follows

;;; Procedure:
;;;   markup
;;; Parameters:
;;;   tag, the role that some text plays
;;;   text, some text to mark up
;;; Purpose:
;;;   Generates some nice happy HTML for me.
;;; Produces:
;;;   html-code, a string for HTML that represents the marked text.
;;; Preconditions:
;;;   Both parameters are strings.
;;;   The text is valid HTML.
;;;   The tag contains only alphanumeric characters and
;;;     corresponds to a valid HTML tag.
;;; Postconditions:
;;;   html-code is valid html.
(define markup
  (lambda (tag text)
    (string-append "<" tag ">" text "</" tag ">")))

What do they dislike about it?

a. The tag name must be a string. They think that symbols are also appropriate. Hence, they'd like to permit

> (markup 'p "Hello.")
"<p>Hello.</p>"

b. The markup procedure permits only one thing between the tags. While it is possible to use string-append to join things, they find that inconvenient. Hence, rather than

> (markup 'p (string-append "Hi " username "."))
"<p>Hi Sam.</p>"

they'd like to write

> (markup 'p "Hi " username ".")
"<p>Hi Sam.</p>"

c. The markup procedure does not permit singleton tags. If there is no text, they'd like markup to just return the singleton tag. For example,

> (markup 'hr)
"<hr>"

d. The markup procedure does not permit attributes to the tags. They would like to be able to include attributes as dotted pairs. For example, to format an image tag, they might write something like the following.

> (markup 'img (cons "src" "Images/logo.png"))
"<img src=\"Images/logo.png\">"

Note that the Taggers expect to be able to have both attributes and content, as in the following example:

> (markup 'a (cons "href" "foo.html") (cons "name" "foo") "Click me, " username)
"<a href=\"foo.html\" name=\"foo\">Click me, Sam</a>"

Fix markup to handle all of their concerns. Note that you should update both the code and the documentation. I also expect to see good testing.

Problem 2: Checking Trees

Key Topics: Deep Recursion, Predicates, Trees, Higher-Order Procedures

As you may have noted, we've designed a variety of kinds of trees. For example, a number tree is either (1) a number or (2) cons of two number trees. Similarly, a symbol tree is either (1) a symbol or (2) cons of two symbol trees. When we designed trees, we often found it useful to write a type predicate that determines whether a Scheme value is the particular kind of tree, such as number-tree? and symbol-tree? and the ilk.

Trent and Trina Tree suggest that instead of directly writing a new xxx-tree? predicate for each new kind of tree, it would be better to write a predicate generator, tree-of. The tree-of procedure should take one parameter, a unary predicate, and return a new unary predicate that determines whether its parameter is a tree, all of whose elements meet the first predicate.

For example,

> (define number-tree? (tree-of number?))
> (define symbol-tree? (tree-of symbol?))
> (number-tree? 1)
#t
> (number-tree? (cons 1 2))
#t
> (number-tree? (cons 1 'a))
#f
> (symbol-tree? (cons (cons (cons 'a 'b) 'c) (cons 1 'd)))
#f
> (symbol-tree? (cons (cons (cons 'a 'b) 'c) (cons 'e 'd)))
#t

You need not document this procedure.

Problem 3: Searching for Positions

Key Topics: Higher-Order Procedures, Vectors and Vector Recursion, Searching

As you may recall, we have written a variety of search procedures.

Seth and Selena Searcher suggest that we have yet to write a procedure that searches a vector for all elements that match a predicate. What would that procedure return? It could return all the elements that match or it could return the indices of all matching elements. (I suppose it could also return something else, but I'm not sure what.) Seth and Selena stipulate that it should return a list of indices of matching elements.

Document and write a procedure, search-vector that searches an unordered vector for all elements that match a predicate and returns a list of the indices of all matching elements.

Problem 4: Quicksort

Key Topics: Sorting algorithms, Divide and conquer, Quicksort

Quentin and Quilla Quick are unhappy that we've implemented a variety of sorting algorithms, but not their favorite algorithm, Quicksort. As you may recall, Quicksort is a relatively simple algorithm:

They're tempted to implement Quicksort as a higher-order procedure that takes the comparison operation as a parameter. However, before doing so, they would like to see an implementation of Quicksort that uses a fixed comparison operation. They asked their professor (that's me) and he's asked you to write that example.

Document and write a procedure, quicksort-numbers, that takes a list of real numbers as a parameter, sorts the list using the Quicksort technique, and returns the sorted list.

Some Questions and Answers

These are some of the questions students have asked about the exam and my answers to those questions.

If we use tail recursion, will we receive extra credit or just show our great love of Scheme?
I will not guarantee extra credit for writing things tail-recursively. However, if you show a great love of Scheme, you are more likely to receive extra credit.
It looks to me like tree-of accepts only one parameter, the predicate. How is the tree ever introduced as a variable to be evaluated?
You are correct that the tree-of procedure takes only one parameter. However, it returns a new procedure that takes one parameter. The parameter of the new procedure is a tree. Note that you've recently written a similar procedure, make-tally, which returns a procedure of one parameter.
In your comments for the original markup, you note that text is to be valid HTML. What do you mean by that?
I mean that all matching tags are matched, that special characters are represented in the ampersand-semicolon form, and such.
One of the steps in Quicksort is Sort each of the sublists. What sorting algorithm should we use?
The principle of recursion suggests that you use Quicksort to sort each sublist.

Errors

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. I limit such extra credit to six points.

 

History

Monday, 1 December 2003 [Samuel A. Rebelsky]

Tuesday, 2 December 2003 [Samuel A. Rebelsky]

Thursday, 4 December 2003 [Samuel A. Rebelsky]

Saturday, 6 December 2003 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue Dec 9 13:58:40 2003.
The source to the document was last modified on Sat Dec 6 19:27:31 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Exams/exam.03.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu