Computer Science Fundamentals (CS153 2004S)

Exam 1: Recursion

Distributed: Monday, 9 February 2004
Due: 10:00 a.m., Wednesday, 18 February 2004
No extensions.

This page may be found online at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Exams/exam.01.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: From Numbers to Lists of Digits and Back Again

Key Topics: Numeric Recursion, List Recursion, Numeric Computation

Diane and Dion Digit very much like the string->list and list->string procedures. As you may recall, string->list converts a string to a list of its component characters and list->string converts a list of component characters back into a string. Diane and Dion suggest that we should be able to do the same with non-negative integers. In particular, they would like to see an integer->digits procedure that converts a non-negative integer to its component digits and a digits->integer that combines the digits back into an integer. For example,

> (integer->digits 12345)
(1 2 3 4 5)
> (integer->digits 53)
(5 3)
> (integer->digits 0)
(0)
> (integer->digits 10)
(1 0)
> (integer->digits 01)
(1)
> (digits->integer (list 2 4 1 3 7))
24137
> (digits->integer (list 2 0 0 0 0))
20000
> (digits->integer (list 0 0 0 0 2))
2

Write integer->digits and digits->integer.

Problem 2: Finding the Alphabetically First String

Key Topics: String Comparison, List Recursion, Variable Parameters

Alex and Alexa Alphabetizer have a lot of strings that they want to put into alphabetical order. They've decided that the key operation to alphabetizing is finding the alphabetically first value in a group of strings. They'd like your help writing a procedure to find such a value.

Write a procedure, (alphabetically-first str1 str2 ... strn), that finds and returns the parameter that comes alphabetically first.

> (alphabetically-first "bozo" "the" "clown" "ate" "his" "nose")
"ate"
> (alphabetically-first "crazy" "cheetahs" "confuse" "chunky" "clowns")
"cheetahs"
> (alphabetically-first "aardvark")
"aardvarks"

Problem 3: From Value to String

Key Topics: Typing, Conversion, Deep Recursion

Errol and Eryn Erroneous are irritated that they cannot make their error messages clearer. For example, they've written the following procedure to sum a list of values.

;;; Procedure:
;;;   sum
;;; Parameters:
;;;   lst, a list of numbers of the form (n1 n2 ... nk)
;;; Purpose:
;;;   sum the values in lst.
;;; Produces:
;;;   result, a number
;;; Preconditions:
;;;   lst is nonempty [Verified]
;;;   lst contains only numbers [Verified]
;;; Postconditions:
;;;   result = n1 + n2 + ... + nk.
;;;   If all of n1, n2, ..., nk are exact, so is result.
;;;   If any of n1, n2, ..., nk is inexact, so is result.
(define sum
  (letrec 
    ((verify (lambda (lst)
               (cond 
                 ((null? lst) (error "Cannot sum the empty list."))
                 ((not (number? (car lst))) (error "That's not a number"))
                 ((not (null? (cdr lst))) (verify (cdr lst))))))
     (kernel (lambda (lst)
               (if (null? (cdr lst))
                   (car lst)
                    (+ (car lst) (kernel (cdr lst)))))))
    (lambda (lst)
      (verify lst)
      (kernel lst))))

When they test it, they get the following results.

> (sum (list 1 2 3))
6
> (sum (list 1 2 'three 4))
That's not a number.

For the second, they'd much rather get

> (sum (list 1 2 'three 4))
three is not a number.

They think they can accomplish these better error messages if they have a procedure that can convert an arbitrary value to a string. They've asked you to write that procedure. (Have you noticed a pattern yet?)

Document, write, and test (value->string val) that converts reasonable Scheme values to strings. By reasonable, I include strings, characters, symbols, numbers, and lists (which can include any of those types). Lists can also include other lists (which can include lists, which can include lists, and so on and so forth).

Problem 4: Finding Entries

Key Topics: List recursion, equality

Liam and Lisa Librarian have decided to organize their collection of books as a list of lists in which each member list contains information about a book in the collection. The member lists have three entries:

For example,

(define our-books
  (list (list "Baron" "David" "The Beast in the Garden")
        (list "Walker" "Henry" "Abstract Data Types")
        (list "Stone" "John" "Higher Algorithms")
        (list "Walker" "Henry" "The Limits of Computation")
        (list "Rebelsky" "Samuel" "Experiments in Java")
        (list "Rebelsky" "Freda" "Growing Children")))

They'd like a procedure that finds all books by a particular author, given the last name. For example,

> (lookup "Rebelsky" our-books)
(("Rebelsky" "Samuel" "Experiments in Java")
 ("Rebelsky" "Freda" "Growing Children"))
> (lookup "Zelazny" our-books)
()

Some Questions and Answers

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

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

 

History

Sunday, 8 February 2004 [Samuel A. Rebelsky]

Friday, 13 February 2004 [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 Fri May 7 09:43:53 2004.
The source to the document was last modified on Fri Feb 13 10:01:48 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Exams/exam.01.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu