You are being recorded and transcribed.
Approximate overview
Schedule needs updating. Stay tuned.
Academic/Scholarly
Cultural
Peer
Wellness
Misc
Should we test our custom sort?
I would recommend that you test it. You do want it to work correctly.
What are DRY and SOLID?
DRY: “Don’t Repeat Yourself”.
There are lots of ways programmers poorly repeat themselves.
Two identical function calls in the same function. That’s inefficient. For recursive functions, it can be doom.
Two or more sets of nearly identical code (copy-paste-change syndrome). In this case, you should write a more general function.
Polymorphism (both kinds) helps with addressing the second issue.
On to SOLID
Here’s what Wikipedia says.
The SOLID ideas are
- The Single-responsibility principle: “There should never be more than one reason for a class to change.” In other words, every class should have only one responsibility.
- The Open–closed principle: “Software entities … should be open for extension, but closed for modification.”
- The Liskov substitution principle: “Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.” See also design by contract.
- The Interface segregation principle: “Clients should not be forced to depend upon interfaces that they do not use.”
- The Dependency inversion principle: “Depend upon abstractions, [not] concretes.”
Our reinterpretation.
Single-Responsibility Principle. Anti-Microsoft philosphy (also The Linux Philosophy): A class should do one thing and do it well. (In Linux, a program should do one thing and do it well.)
- Open-closed Principle.
- You should be able to change a class w/o modifying it.
- And you do so by extending it (subclassing)
- Encapsulation
- Liskov Substitution Principle.
- Another name for subtype polymorphism.
- Side note: The idea is due to Barbara Liskov.
- Side note: From long before we really understood OOP. (ca. 1967)
- Interface Segregation Principle.
- This sounds to Sam like the Single-Responsibility principle as applied to interfaces.
- Dependency inversion.
- Encapsulation.
What are they?
Why do we use them?
Example of proof
for (int p = 0; p < stuff.len; p++);Example of design
p
+-------------+------------+
| sorted | unsorted |
+-------------+------------+
p
+-------------+-+----------+
| sorted |unsorted |
+-------------+-+----------+
p into the right place in the sorted
stuff and then advance p.Observations
You have an array with three values, which we’ll call red, white, and blue. They are in no particular order. Your goal is to rearrange it so that all of the red are at the left, all the blue are at the right, and all the white are in between.
You may not simply count the values; different reds represent different objects (similarly for blues and whites).
We rearrange it rather than copy to a new array or arrays because we are trying to limit space usage.
Sample input:
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Ba|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Rq|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
O(n^2): Use insertion sort with an appropriate comparator.
Can we do better? In particular, can we write an O(n) algorithm?
Idea: Two indices; one moves right from the beginning, one moves left from the end.
Goal: Red, white, blue
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Ba|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Rq|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
How do you approach the problem?
left middle right
| | |
v v . v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| | | | | | | | | | | | | | | | | |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
middle
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Ba|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Rq|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is blue. Swap and move right left. (swap(middle, --right))
middle
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is red. Swap and increment both left and right.
(swap(middle++, left++)).
middle
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is red. Swap and increment both left and right.
middle
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is white. Increment middle.
middle
left right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is white. Increment middle.
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Wc|Wd|Re|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is red. `swap(left++, middle++)
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Wd|Wc|Bf|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Wp|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is blue. swap(middle, --right)
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Wd|Wc|Wp|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
When do we stop?
Sam continues the example ….
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Wd|Wc|Wp|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is white. ++middle.
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Wd|Wc|Wp|Wg|Rh|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is red. swap(left++,middle++)
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Rh|Wc|Wp|Wg|Wd|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is white. ++middle. (Two steps skipped.)
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Rh|Wc|Wp|Wg|Wd|Wi|Wj|Wk|Rl|Wm|Bn|Bo|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is red. swap(left++,middle++)
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Rh|Rl|Wp|Wg|Wd|Wi|Wj|Wk|Wc|Wm|Bn|Bo|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is white. ++middle.
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Rh|Rl|Wp|Wg|Wd|Wi|Wj|Wk|Wc|Wm|Bn|Bo|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is blue. swap(middle, --right).
middle
left | right
| | |
v v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Rh|Rl|Wp|Wg|Wd|Wi|Wj|Wk|Wc|Wm|Bo|Bn|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The thing at middle is blue. swap(middle, --right).
middle
left right
| |
v v
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|Rq|Rb|Re|Rh|Rl|Wp|Wg|Wd|Wi|Wj|Wk|Wc|Wm|Bo|Bn|Bf|Ba|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The middle pointer has reached right. We’re done. Woo!