# Class 31: Discussion of Exam 2

Back to The Design of Lists. On to Project Discussion.

Held Monday, October 25, 1999

Overview

Notes

• I'm returning exam 2 today.
• I'll give you about ten minutes to look over your exams
• Then we'll go over them.
• Don't forget to get interfaces and stub classes to me by 9 a.m. Wednesday.
• Because of the difficulties many of you have had with running time analysis, I've decided not to grade assignment 3. Sorry for the late decision. I'm happy to go over your assignments on an individualized basis, if you'd like.

Contents

Handouts

Summary

• Discussion of exam 2

## Exam 2

• Some basic data:
• Information on times included in the notes on the exam
• A number of you spent approximately 10 hours on the exam. That's more than I'd hoped for. What were my expectations? That A, B, and D would each take 30 minutes or so (along with some passive rumination about B) and that C would take about an hour.
• A minor note:
• Some of you did not put your answers in order, in spite of a requirement on the exam that you do so. Please make sure to do so in the future.
• Two particularly important issues:
• I noticed a few cases in which answers were quite similar, not only in the technique used but also in the mistakes made. I'm hoping this was just coincidence. Do not work together on exams or otherwise share your work.
• Many of you are taking code from elsewhere and failing to cite in, in spite of instructions on the exam and the introductory handouts. It is a violation of Grinnell's academic honesty policies to use the work of others without citing it. For this exam, I've treated such violations as a ``lack of craft''. Please be more careful in the future.
• When we go over the exam in class, I may ask for volunteers to come up to the front of the room. In general, you do not need to volunteer if you got over 15 on a problem (nor should you call out answers).

### Some Background Mathematics

• What is the value of 1 + 2 + ... + n? (I'll go through the proof for this one.)
• What is the value of 1 + 1/2 + 1/4 + 1/8 + ... + 1/2k?
• What is the value of n*(1 + 1/2 + ... + 1/2k)?
• What is the value of 1 + 2 + 4 + 8 + 16 + ... + 2k?
• Hint: Use the previous formula

### Problem A

• Thinking about the problem: We have the formula f1(n) = c2n + f1(n/2). What does the formula suggest about the design of the code (i.e., what the code does)? Consider each part.
• Now, let's try the more formal analysis.
• What were the greatest difficulties students had with this problem?
• The sum mentioned above
• ``Dropping constants'', which sometimes went so far as to drop a term involving n. Most of the time, when we `` drop constants'', we simply turn them into the value 1.
• A few related questions
• if f(n) = f(n/2) and f(1) is 1, what does this say about f?
• if f(n) = f(n-1), what does this say about f?

### Problem B

• Most of you got this substantially right. The greatest difference between two values in a collection is the difference between the largest and smallest values in that collection. Hence, we can find the largest in O(n) and the smallest in O(n) and subtract them.

### Problem C

• I was surprised to see that some of you decided to rewrite partition from scratch rather than use the version in the book. Often, those who did so took particularly long to do the problem.
• One of the more common mistakes was to substitute `equals` for `lessEqual`, as in
```  protected int partition(int lb, int ub, Comparator compare)
throws IncomparableException
{
Object pivot = this.get(lb);        // X: A to get
int first = lb;
int last = ub;
while (first < last) {
while ( (compare.lessThan(pivot, get(last)))
&& (first < last) ) {
last--;
} // while there are big elements on the right
while ( compare.equals(get(first), pivot)
&& (first < last) ) {
first++;
} // while there are small elements on the left
if (compare.lessThan(get(first),get(last) {
swap(first,last);
}
} // while
this.swap(lb,first);
return first;
} // partition
```
• Will this still work? Will it work differently?
• I took off a little if you used `objects[i]` rather than `get(i)`. Let's brainstorm reasons to prefer the second over the first.

### Problem D

• A casual analysis was fine.
• Note that the 2 in f(n) = c + 2*f(n/2) was particularly important.

### Extra Credit

• Random selection
• Least difference
• Integer comparator

## History

Tuesday, 10 August 1999

• Created as a blank outline.

Monday, 25 October 1999

• Filled in the details.

Back to The Design of Lists. On to Project Discussion.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.