Fundamentals of Computer Science I (CS151 2003F)

Exam 1: Scheme Basics

Distributed: Monday, 22 September 2003
Due: 11:59 p.m., Tuesday, 30 September 2003
No extensions.

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

Contents

Preliminaries

There are six problems on the exam. Some problems have subproblems. Each full problem is worth sixteen 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 four points of extra credit. You will find these points useful, since a perfect score on the primary problems will only earn you 96 points.

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: Determining Types

Topics: Types, Predicates

As you know, we've encountered a number of types in Scheme, including lists, procedures, and strings. Write a procedure, (type-of val) that returns the type of value val. If the value has a type that you don't know, report a type of unknown. For example,

> (type-of 1)
integer
> (type-of null)
list
> (type-of list?)
procedure
> (type-of (cons 1 2))
unknown

Problem 2: Extending Sentences

Write a procedure, (extend sentence), that adds an extra sentence to the end of sentence that depends on the punctuation used to end sentence. If sentence ends with a period, end the sentence with , I think. If sentence ends with an exclamation point, add Yahoo! after the sentence. If sentence ends with a question mark, add Well? after the sentence.

For example,

> (extend "I can solve this problem.")
"I can solve this problem, I think."
> (extend "Grinnell is a great college.")
"Grinnell is a great college, I think."
> (extend "MathLAN rules!")
"MathLAN rules!  Yahoo!"
> (extend "Can you help me on this problem?")
"Can you help me on this problem? Well?"

Problem 3: Singleton Lists

In some problems, we need to determine if a list has only one element. While Scheme includes a procedure, null?, that determines whether a list has no elements, it lacks a procedure that determines whether a list has only one element.

Write (singleton? val) that returns true (#t) if val is a one-element list and #f otherwise. You may not use length to implement singleton?.

Problem 4: Close Numbers

As you've noted, Scheme sometimes approximates numbers. It is therefore possible that two computations that should produce identical answers may produce similar, but not identical answers. Hence, rather than checking whether two numbers are equal, we should check whether they are close. Write a predicate (close? num1 num2) that returns true (#t) if num1 and num2 are within 1% of each each other and false (#f) otherwise.

Problem 5: Choosing an Element

We often write programs that simulate unpredictable situations (e.g., how customers might choose a line at Wal-Mart, what word a Pirate might choose for a particular person). To make it easier to write such programs, we might choose to develop a procedure, (choose lst), that chooses an element of lst, and may choose a different element each time. For example,

> (choose (list 'a 'b 'c 'd 'e))
d
> (choose (list 'a 'b 'c 'd 'e))
a
> (choose (list 'a 'b 'c 'd 'e))
e
> (choose (list 'a 'b 'c 'd 'e))
d
> (choose (list 'a 'b 'c 'd 'e))
c

Write choose.

Problem 6: Replacing if with and, or, and not

You may recall that I've said that I don't like to see if statements that explicitly return #t or #f since you can easily rewrite them using some combination of not, or, and and. For example,

(if test1 #t test2) => (or test1 test2)
(if test1 #f #t) => (not test1)
(if test1 (if test2 #t #f) #f) => (and test1 test2)
(if test1 test2 test3) => (or (and test1 test2) test3)

Rewrite the following expression so that it uses and, or, and not rather than if, #t, and #f.

(if test0
    (if test1
        #f
        (if test2
            (if test3 #t test4)
            (if test5 #f test7)))
    (if #f test8 test9))

Some Questions and Answers

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

Do we have to write the six P's for every procedure we write?
Yes.
Even though the original instructions said A few sentences should suffice?
Yes.
Do we have to document number six?
No. It's not a procedure.
Where should we put the printed (and signed) copies?
Under my door.

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

 

History

Sunday, 21 September 2003 [Samuel A. Rebelsky]

Saturday, 27 September 2003 [Samuel A. Rebelsky]

Sunday, 28 September 2003 [Samuel A. Rebelsky]

Monday, 29 September 2003 [Sameul 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 Mon Sep 29 14:21:08 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Exams/exam.01.html.

Valid HTML 4.0 ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu