Fundamentals of Computer Science 1 (CS151 2003S)

Exam 3: Algorithms and Higher-Order Procedures

Distributed: Monday, 28 April 2003
Due: 2:15, Friday, 9 May 2003
No extensions.

This page may be found online at



There are three problems on the exam. Some problems have subproblems. Each full problem is worth thirty-three 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. Make sure that your solution confirms to the format for laboratory writeups (except that you should not tell me the location in the MathLAN or on the Web, since your code should not be published).

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 to five points.

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'll be gone from noon Friday to 5 p.m. Sunday, so don't call during those times.]

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 Generalized less-than? Procedure

Topics: Algorithms, Strings, Comparison, Deep Recursion, Vectors

Your old pals, Sarah and Steven Schemer, are upset that they have to keep rewriting similar less-than? predicates. For example, they must remember to use

They've decided that someone should write a general less-than? predicate that can compare any reasonable pairs of values. 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 less-than? for them. Your sentient and malicious instructor agrees, although he is willing to let you forgo the careful documentation that he usually requires. He is also willing to forgo having you implement every possible type.

Here's what he'd like you to do for less-than?.

Extra Credit

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

You should not use vectors in solving this problem.

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

This problem is worth 2 points of extra credit.

2. Extend less-than? to handle symbols. This problem is worth 2 points of extra credit.

Problem 2: Grading

Topics: Higher-Order Procedures, Vectors, Files

One of the tasks faculty frequently encounter is the assignment of a letter grade based on the numeric grades that students have received over the course of a semester. Grading rubrics differ greatly. Some faculty simply average grades; others sum grades; others give different aspects different weights.

Suppose we've decided to store all the grades for a class in a file. One mechanism would be to have one vector for each student in the class. The vector will have the form

#(last-name first-name email grade1 grade2 ... graden)

For example, here are the grades for a fictitious class in which students have received four grades (presumably for three exams and a final).

#("Aardvark"  "Arnie" "" (100 50 80 90))
#("Beetle"    "Betty" "" (85 85 90 85))
#("Chihuahua" "Chuck" "" (90 90 90 0))
#("Dingo"     "Donna" "" (95 95 95 80))
#("Elephant"  "Ernie" "" (5 0 0 0))
#("Fox"       "Fiona" "" (80 80 80 100))
#("Giraffe"   "Gene"  "" (50 50 50 50))

You can find this example in the file samplegrades.

How will we process the file of grades? By reading each entry from the file, calling an appropriate grading procedure, and doing something with the result. For example, we might write the results to a file.

If we write the results to a file, the procedure we write for processing grades needs at least two parameters: the name of the input file and the name of the output file.

Are there others? To make our procedure as general as possible, we can also make the grading procedure a parameter. For example, if a faculty member's rubric is average the grades, the procedure can be

(lambda (grades) (divide (sum grades) (length grades)))

Similarly, if a faculty member's rubric is average the grades after dropping the lowest, the procedure can be

(lambda (grades)
  (divide (- (sum grades) (min grades))
          (- (length grades) 1))

For the more complex best two of three exams count 50%; final counts 50%, we might write

(lambda (grades)
  (let* ((sedarg (reverse grades))
         (final (car sedarg))
         (exams (cdr sedarg)))
    (+ (divide final 2)
       (divide (- (sum exams) (min exams)) 4))))

Note that for computing grades, we don't care about the name or email; we care only about the numbers.

Problem: Document, write, and test a procedure, (process-grades infile outfile gradeproc), that reads a sequence of grade entries from the file named infile, processes each entry with gradeproc, creates a new vector that includes the result (along with the original components), and writes all the new vectors to outfile.

For example, for the example above with the rubric of use the best grade, the output file might contain

#("Aardvark" "Arnie" "" (list 100 50 80 90) 100)
#("Beetle" "Betty" "" (list 85 85 90 85) 90)
#("Chihuahua" "Chuck" "" (list 90 90 90 0) 90)
#("Dingo" "Donna" "" (list 95 95 95 80) 95)
#("Elephant" "Ernie" "" (list 5 0 0 0) 5)
#("Fox" "Fiona" "" (list 80 80 80 100) 100)
#("Giraffe" "Gene" "" (list 50 50 50 50) 50)


In grading this problem, I will look at how well you process the input file, how well you generate the output file, and how you deal with procedures as parameters. If you cannot do everything right, you should attempt to show your ability to do some aspects by simplifying some parts of the problem (e.g., by reading grades from a list rather than a file, by generating a list rather than a file, or by hard-coding the grading procedure).

Problem 3: Inserting into Vectors

Topics: Sorting, Higher-Order Procedures, Vectors

Vern and Violet Vector love Amanda-sort (which most computer scientists typically call Insertion Sort). They are, however, somewhat frustrated that the standard insertion-sort procedure we've written only works on lists. They realize that they can sort a vector by converting it to a list, sorting the list, and converting it back to a vector. However, they'd rather sort the vector directly.

They happily notice that I've written and documented such a procedure, insertion-sort!, in the first reading on sorting. Here's what it looks like:

(define insertion-sort!
  (lambda (may-precede? vec)
    (let ((len (vector-length vec)))
      (let kernel ((boundary 1)) ; The index of the first unsorted value
        (if (< boundary len) ; If we have elements left to sort
              (insert! (vector-ref vec boundary)
              (kernel (+ boundary 1))))))))

To their dismay, they quickly discover that I've failed to implement the needed insert! procedure. As you may guess, they've turned to you for help.

a. Document that procedure.

b. Write that procedure.

Hint: Work backward from boundary.

c. Show how to use insertion-sort! to sort the following vector of faculty by last name. When two or more last names are the same, order within those last names by first name. You may not use your less-than? procedure from problem 1.

(define some-faculty
  #(("Ferguson"   "Pam"   "Mathematics")
    ("Moore"      "Tom"   "Statistics")
    ("Ferguson"   "Bill"  "Economics")
    ("Montgomery" "Mark"  "Economics")
    ("Moore"      "Emily" "Mathematics")
    ("Strauber"   "Ira"   "Political Science")
    ("Ferguson"   "Susan" "Sociology")
    ("Strauber"   "Susan" "Art")

Extra Credit

For those of you who really like sorting and who have some spare time, here's a question that can earn you five points of extra credit on the exam. You may not count this problem toward your eight-hour limit.

Assume that we've decided that all list-sorting procedures should take two parameters, a list to sort and a may-precede? predicate. These sorting procedures return a new sorted list.

Write a procedure, (verify-sort sorting-proc may-precede? unsorted-list unsorted-list), that verifies that sorting-proc correctly sorts unsorted-list using may-precede? to order elements.

Some Questions and Answers

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

Questions on Problem 1

Which vector is smaller, #(1 4 1) or #(1 2 3 4)?
#(1 2 3 4), even though it is longer. Why? Because the 2 in position 1 is smaller than the 4 in position 1 in the other vector.
It's really a pain that I sometimes need strict inequality and sometimes nonstrict inequalty for less-than?.
Agreed. Use only strict inequalty (variants of < rather than <=).
When one paramter is a vector and the other is not, what can the other be?
A number or string.
Will you add some more examples to the exam?
My DSL is down and I'm spending much of the day at home, waiting for the repairman to come. So ... some day, but perhaps not today.

Questions on Problem 3

Why doesn't insertion-sort! have the else-part of the if expression?
Remember, ! procedures need not return anything. For example, vector-set! doesn't return anything. Hence, insertion-sort! does not need to return anything, since its purpose is to change the vector in place. It certainly doesn't not need the second part of the if expression, because once it's done sorting the vector, it has nothing to return.


Here you will find the errors of spelling, grammar, and external design. Remember, each error found gets the whole class a point of extra credit on the exam (with a maximum of five such points).



Sunday, 27 April 2003 [Samuel A. Rebelsky]

Monday, 28 April 2003 [Samuel A. Rebelsky]

Tuesday, 29 April 2003 [Samuel A. Rebelsky]

Wednesday, 30 April 2003 [Samuel A. Rebelsky]

Thursday, 1 May 2003 [Samuel A. Rebelsky]

Friday, 2 May 2003 [Samuel A. Rebelsky]

Monday, 5 May 2003 [Samuel A. Rebelsky]

Tuesday, 6 May 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 May 6 09:22:19 2003.
The source to the document was last modified on Tue May 6 09:22:13 2003.
This document may be found at

Valid HTML 4.0 ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,