Fundamentals of CS I (CS151 2002F)

Exam 3: Algorithms and Data Structures

Distributed: Wednesday, December 4, 2002
Due: Wednesday, December 11, 2002
No extensions.

This page may be found online at



There are five problems on the exam. Some problems have subproblems. Each full problem is worth twenty 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, by posting a please help message, or by any other means) 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. It is likely to take you about five to ten hours, depending on how well you've learned topics and how fast you work. You must write down the amount of time each problem takes you. 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 three points of extra credit to the first two people who honestly report that they've spent at least eight hours on the exam or who note that they've completed the exam in under eight hours. (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. 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 aware of no 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 must both answer all of your questions electronically and turn them in hardcopy. That is, you must enter all of your answers on the computer, print them out, and hand me the printed copy. You must write your name on the top of every page of the printed exam. 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. Make sure that your solution confirms to the format for laboratory writeups

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.

Unless I specify otherwise, you should fully document all of the primary procedures (including parameters, purpose, value produced, preconditions, and postconditions). If you write helper procedures (and you may certainly write helper procedures) you should document those, too, although you may opt to write less documentation. 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, so should you be careful and precise when you write prose. Please check your spelling and grammar. I am likely to penalize you for bad spelling and grammar.

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.

Although the problems are numbered from 1 to 5, you need not do them in order. In fact, experience shows that students tend to do better if they are willing to put aside problems that give them difficulty, work on other problems, and then come back to the difficult problems later.

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 can be answered. 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). You can also send me electronic mail.

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


Problem 1: A General less-than? Predicate

Your old pals, Sarah and Steven Schemer, are upset that they have to keep rewriting similar may-precede? predicates (e.g., they must remember to use string-ci<=? for strings, <= for numbers, (lambda (x y) (string-ci<=? (car x) (car y))) for lists whose car is their key, and so on and so forth). They've decided that someone should write a general less-than? predicate that can compare any reasonable pairs of values.

What do they think is reasonable?

Of course, Sarah and Steven are much better at deciding what they'd like to do than at actually doing it. Hence, they've asked you to implement the less-than? they've described for them. Your sentient and malicious instructor agrees, although he is willing to let you forgo the careful documentation that he usually requires. Implement less-than?.

Problem 2: Problems with less-than?

Theodore and Thelma Theoretician take one look at Sarah and Steven's suggestions and start to chuckle. They say

One of the key characteristics of the standard may-precede? parameter to sorting and searching algorithms is that it is transitive. That is, if a may precede b and b may precede c, then a may precede c. Unfortunately, there are values for which less-than? is not transitive.

Find three values, a, b, and c, such that

> (less-than? a b)
> (less-than? b c)
> (less-than? a c)

Note that you don't need to implement less-than? to answer this question, you just need to look at the guidelines for less-than?.

Hint: Consider cases in which you need to modify one or both of the parameters.

Problem 3: Documenting Procedures

An and Al Abbrev object to the overly-long procedure names that Sam likes to use. Hence, they tend to choose one-character names. They also avoid the sixP Ps that I like. Here's a procedure they've recently written.

(define r
  (lambda (l p?)
    (letrec ((c (lambda (p v)
                  (let ((x (if (p? v) 1 0)))
                    (cons (+ (car p) x) (+ (cdr p) (- 1 x))))))
             (r (lambda (q m)
                  (if (null? m) 
                      (/ (car q) (cdr q))
                      (r (c q (car m)) (cdr m))))))
      (r (cons 0 0) l))))

Document the procedure r. If you'd like, you can change the names used within r (including r) to something more readable.

Problem 4: Selection Sort

After your terrifying encounters with Theodore and Thelma and An and Al, you're almost happy to hear that Sarah and Steven Schemer want to work with you again. They've started a project to implement as many sorting algorithms as they can think of. Their current project is selection sort, the algorithm that works by repeatedly selecting the smallest remaining value.

Believe it or not, they've written most of a selection sort procedure for lists.

(define selection-sort
  (lambda (lst may-precede?)
    (if (null? lst) null
        (let ((small (smallest lst may-precede?)))
          (cons small (selection-sort (remove small lst) may-precede?))))))

a. Finish this implementation of selection sort. You will need to implement or find smallest and remove. You may need to modify their code.

b. Rewrite selection-sort so that it is tail recursive.

You do not need to document anything. However, you should show appropriate tests.

Problem 5: Student Records or Objects

Sarah and Steven Schemer have started to implement a new online student information system for the registrar. For each student, they'd like to store the following information:

Implement a make-student procedure that builds a record or object to store information on students. You should also provide related procedures (or methods) to support extracting each field, changing major, adding new courses, and deleting courses. You should also provide a predicate that checks whether a student took a particular course.

Extra Credit

I've added a new paragraph to the preliminaries. Summarize that paragraph. [2 points]

Identify problems of grammar or spelling in the main body of the exam (not including the preliminaries). [1 point each given to the whole class; 5 points max]



Tuesday, 3 December 2002 [Samuel A. Rebelsky]

Wednesday, 4 December 2002 [Samuel A. Rebelsky]

Thursday, 5 December 2002 [Samuel A. Rebelsky]

Friday, 6 December 2002 [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 Sun Dec 8 22:31:05 2002.
The source to the document was last modified on Sun Dec 8 22:31:01 2002.
This document may be found at

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

Samuel A. Rebelsky,