CS200: Computer Science, Spring 2003
Exam 1 Out: 21 February 2003
Due: Monday, 24 February 2003, 2:00PM
DirectionsWork alone. You may not discuss these problems or anything else related to this course with anyone except for the course staff between receiving this exam and class Monday. You may send email to firstname.lastname@example.org to ask for clarifications on what the questions mean.
Open book. You may use any books you want, lecture notes and slides, your notes, and problem sets. If you use anything other than the course books and notes, cite what you used.
No DrScheme. You may not run DrScheme or use any other Scheme interpreter between now and when you turn in this exam.
Answer well. Answer all 10 questions. Write and number your answers clearly. The questions are not necessarily in order of increasing difficulty, so if you get stuck on one question you should continue on to the next question.
Staple the sheets you turn in and put your name on them. If your sheets are not stapled, I will only grade the first one.
Full credit depends on the clarity and elegance of your answer, not just correctness. Your answers should be as short and simple as possible, but not simpler.
Cost of Nothing, Value of EverythingFor each of these questions, explain what the given Scheme expression evaluates to or what error would be produced when it is evaluated.
You do not need to show all the evaluation steps, but the more clearly you show how the expression is evaluated, the more likely you are to receive some credit even if your answer is incorrect. If you think there would be an error, make sure to be especially clear about which evaluation or application rule produces the error.
Remember you may not use a Scheme interpreter during this exam!
1. (< 5 4)
2. (if (< 5 4) (+ + +) 12)
3. ((lambda (a b) (+ a b)) 3)
4.(car (cdr (map (lambda (x) (* x 2)) (cons 50 (cons 100 (cons 200 null))))))
The Root of All Evil...
Enron's accountants have requested a procedure that takes a list of numbers, and evaluates to the list with all the negative numbers removed. For example, (enronize (list 5 -17 4000 13 0 -23 12)) should evaluate to the list (5 4000 13 0 12).
5. Define the enronize procedure. For this question you should not use insertl (or anything similar) in your definition.
6. Define the enronize procedure using insertl. For this question, you must use insertl (as defined on Notes 8) to do most of the work.
7. How much work is the enronize procedure you defined in question 5? Express your answer using Θ notation, and include a brief explanation why it is correct. If you use n in your answer, be sure to explain what it means.
The SEC has caught on to Enron's accounting scheme, and Enron's new chief accountant has requested a more general version of the enronize procedure. Instead of always eliminating all negative values, it should be able to eliminate all values below a given value. To be able to produce all possible enronize procedures, define a make-enronize procedure that takes one parameter lowval, evaluates to a procedure that eliminates all values below lowval from the input list. For example, (make-enronize 0) should evaluate to the original enronize procedure that eliminates all the negative values, and((make-enronize -2) (list 5 -7 -2 12 -23))should evaluate to the list (5 -2 12).
8. Define the make-enronize procedure. You may use insertl if you want, but are not required to.
...Doesn't Grow on Trees
In Lecture 13 we used binary trees to sort quickly. For this problem, you will define a procedure that takes a binary tree as input, and evaluates to its mirror image: the tree we would see if we held up a mirror to the right side of the input tree. For example, if the input is the tree on the left, the output should be the tree on the right:5 | 5 / \ | / \ / \ | / \ 2 8 | 8 2 / \ / | \ / \ 1 4 7 | 7 4 1 input mirror outputNote that the null children are not shown. Observe that the mirror image tree is produced by swapping the left and right sides at every node.
9. Define the mirror-tree procedure that takes a tree as input, and evaluates to its mirror image tree. You may use the tree procedures we defined in Lecture 13: make-tree, get-left, get-element and get-right. If you are not sure your code is completely correct, you should explain in clear English what you are doing.
10. How much work is your mirror-tree procedure? Express your answer using Θ notation, where n is the number of nodes in the input tree. Include a brief explanation why it is correct. Also, explain why it is not possible for someone else to write a correct mirror-tree procedure that is significantly less work.
11. (Optional, no credit) Do you feel your performance on this exam will fairly reflect your understanding of the course material so far? If not, explain why.
End of Exam.
Bring your stapled answers to all questions to turn in at the beginning of class on Monday, 24 February.
Using these Materials