Fundamentals of Computer Science I (CS151 2003F)

Exam 2: Recursion

Distributed: Tuesday, 28 October 2003
Due: 4:00 p.m., Tuesday, 4 November 2003
No extensions.

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

Contents

Preliminaries

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, 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 conforms 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). 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 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.

Problems

Problem 1: Some Equality Issues

Key Topics: Equality Testing, Lists and List Representation, Efficiency

Shannon and Shelby Schemer wrote the following code.

(define fox (list 1 2 3))
(define giraffe (list 1 2 3))
(define hippo (cons 1 (cdr fox)))
(define iguana hippo)

a. Shannon and Shelby have found that Scheme considers all four values equal if one uses the equal? predicate but not if one uses the eqv? predicate. They can't understand why. Explain it to them. Note that pictures might help.

b. Theo and Theona Theoretician tell us that the problem above is why Schemers tend to avoid equal?. Explain why someone might be cautious about using equal? to compare values.

Problem 2: Determining Membership

Key Topics: List Recursion, Deep Recursion, Equality Testing, Predicates

Trudy and Trent Tree have designed a new data type that they call a tree of symbols. They tell you that a tree of symbols is either (1) a symbol, (2) null, or (3) cons of two trees of symbols. Write and test a procedure, (in-tree? symbol tree-of-symbols), which determines whether the simple value symbol is either equal to tree-of-symbols or appears somewhere in tree-of-symbols.

You need not document this procedure.

> (in-tree? 'jaguar null)
#f
> (in-tree? 'jaguar 'jaguar)
#t
> (in-tree? 'jaguar 'kitten)
#f
> (in-tree? 'jaguar (cons 'kitten 'kitten))
#f
> (in-tree? 'jaguar (cons 'jaguar 'kitten))
#t
> (in-tree? 'jaguar (cons 'kitten 'jaguar))
#t
> (in-tree? 'jaguar (cons 'kitten (cons 'jaguar 2)))
#t
> (in-tree? 'jaguar (list 'jaguar))
#t
> (in-tree? 'jaguar (list 'gnu 'mouse 'lemur 'kitten 'jaguar))
#t

Problem 3: Converting Lists to Strings

Key Topics: List Recursion, Strings

Steven and Sarah Stringer find it fascinating that Scheme can figure out how to print such a wide variety of Scheme types. They find it particularly interesting that Scheme is able to print out lists without knowing their length in advance. They've asked you to show them some code that explains what Scheme does.

Write a procedure, (list-of-symbols->string lst), which takes a list of symbols and converts it to the corresponding string.

For example,

> (list-of-symbols->string null)
"()"
> (list-of-symbols->string (cons 'orangutan null))
"(orangutan)"
> (list-of-symbols->string (list 'orangutan 'platypus))
"(orangutan platypus)"
> (list-of-symbols->string (cons 'orangutan (cons 'platypus (list 'rhino 'salamander))))
"(orangutan platypus rhino salamander)"

You will probably find it useful to use the built-in symbol->string procedure.

Problem 4: Reporting on Errors

Key Topics: Error checking, husk-and-kernel, recursion

Robert and Roberta Robust are concerned that you write code that crashes in an unfriendly manner when the parameters are not the appropriate type. Pick one of the procedures from the previous two problems (i.e., in-tree? or list-of-symbols->string) and extend it to do error checking. Your colleagues have suggested two ways in which you might do error checking. I'd like you to try both.

a. Henry and Karla prefer husk-and-kernel programming as a technique for checking for the errors all at once. Rewrite the original procedure so that it uses a husk-and-kernel technique to check for errors before doing the main work.

b. Ernie and Ellie Efficient are concerned that husk-and-kernel programs spend lots of time checking values that the program itself might never visit (e.g., if the first value in a tree is the symbol desired, there is no need to visit the rest of the tree to determine whether or not it contains only symbols). They suggest that it is better to make a simple (non-recursive) check at every step and stop only when one hits an error. Rewrite the original procedure so that it uses this technique to check for errors at every step.

c. Explain which technique you prefer and why.

Problem 5: 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.

Extra Credit

Those of you who have extra time and would like to try some more problems might do one or more of the following for a slight bit of extra credit.

a. Expand list-of-symbols->string to handle nested lists.

b. Expand list-of-symbols->string to handle nested trees of symbols (inserting the legendary dot where appropriate).

c. Write digits->integer.

Some Questions and Answers

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

Would you accept an answer that adds an extra space at the end of a list for problem 3? E.g., "(aardvark baboon chimp )" instead of "(aardvark baboon chimp)"?
That would not be a perfectly correct answer. Expect to lose about five points for an otherwise-correct answer that includes the extra space.
Do we have to document the procedure rewrites in Problem 4?
No.
One of the examples for in-tree? has an invalid tree.
The procedure does not need to test its preconditions. As you know, when the preconditions are not met and you haven't promised to report an error, you can do whatever you'd like.
How do I turn in my pictures electronically.
You don't. I'll deal with hardcopy only.
For problem 5, do we have to deal with negative numbers?
No.

Errors

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

 

History

Monday, 27 October 2003 [Samuel A. Rebelsky]

Tuesday, 28 October 2003 [Samuel A. Rebelsky]

Friday, 31 October 2003 [Samuel A. Rebelsky]

Saturday, 1 November 2003 [Samuel A. Rebelsky]

Sunday 2 November 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:39 2003.
The source to the document was last modified on Sun Nov 2 22:03:13 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Exams/exam.02.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu