## Session 10

### Opening Exercise

Today let's do a little retrospective of Homework 2. Work in groups of two or three. Discuss these questions and share your answers.

• What ambiguities or open questions did you find in the specification for the assignment?
• What design or programming choices went well?
• What was the biggest challenge you faced?

### Discussion

We often spend too little time discussing programs... Students sometimes discuss problems and programs while doing assignments, but... Take stock. Share.

What was unclear in the spec?

• zoomin() -- playing second. A matter of context! Or an ambiguity in the algorithm itself (sort of).
• experiment() -- the size and make-up of the game boards. How important are these?
• Others?

Good design or programming choices?

• The high-level design was specified in the assignment. This won't always be the case.
• Others?

What were the biggest challenges in this assignment?

• Mechanical: passing functions, using command-line. These are important, too. Work in the trenches matters.
• Others?

A couple of other questions: What did you learn? What else do you hope to learn?

### Code, Code, Code

Look at my Ruby solution...

• How could we make the algorithms more efficient?

Look at my Java solution...

• using a class to define objects and modules
• trade-offs between Java and Python/Ruby
• writing code to do the job... TableDriver.
• FP vs OO -- growth, reuse, change

How might we use this code to count the number of base operations executed -- to help us analyze these algorithms empirically? Consider ways to "tool" the code...

Now a Big Question:

How do you know your code is correct?

Look at my tests: Ruby versus Java versus Python.

### Follow-up on Exam 1

Let's make a quick pass through Exam 1.

Problem 1: Anagram

(I have code!

Problem 2: Definitions.

Problem 3: Mismatch

• sort + split -- O(n log n) + O(1)
• partition a la quickselect -- O(n) + O(1) !!

Problem 4: Insertion Sort

• The basic operation is either the comparison initial[j-1] > initial[j] or the 3-line swap operation that follows. The algorithm does one more comparison than swap, so the choice won't affect our answer.
• A loop of averaging size n/2 inside a loop of size n gives n²/2 comparisons and n²/2 - 1 swaps.
• O(n²). The -1 and the /2 affect the count increasingly less as n → ∞.

(How can we improve this algorithm? Swap only once!)

Problem 5: Recursion

This is quite similar to a problem we did when learning to analyze a recursive algorithm in Session 8:
R(0)                    = 0
R(n) =                    R(n-1) + 1
= (R(n-2) + 1) + 1 = R(n-2) + 2
= (R(n-3) + 1) + 2 = R(n-3) + 3
= (R(n-4) + 1) + 3 = R(n-4) + 4
...
...              = R(n-i) + i
[substitute i = n]     = R(n-n) + n
= R(0)   + n
=  0     + n
= n

That shouldn't be two surprising -- the algorithm makes a linear walk from n to 0!

(You can use the same technique to compute the value of foo(n)... Try it!)

After an exam, it is a really good idea to solve the problems again in the leisure of your own room. Write code. Ask questions.

### Wrap Up

• Reading -- None for now.

• Homework -- Homework 2 was due today.

• Exam -- Our next exam won't come for a few weeks. More details later.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... February 13, 2014