Homework
What to hand in
algo with load and stats, passing go test ./..., plus a measurement
table.
1. Your own growth curve
Reproduce step 2's table on your machine, n = 1,000 … 1,000,000:
| n | reallocations | items copied | average per append |
|---|---|---|---|
The last column must stop growing. Say at which n it settles, and what it settles at.
2. Reserved capacity
Do it again starting from make([]Item, 0, n). Fill in the same table.
How many reallocations? How many copies? Explain why in one sentence.
3. Where amortization stops helping
Construct a case where append's amortization fails: every time you reach
capacity, cut the slice back to cap-1 and append again.
How many copies per append now? Explain why the amortized analysis does not
apply here.
4. Insert versus append
Build a 100,000-item library two ways:
- a)
appendat the end, 100,000 times; - b)
InsertAt(lib, 0, it), always at the front.
Count the shifts for each. The difference should be four orders of magnitude. Give both numbers and their ratio.
5. When an array is the wrong choice
One paragraph, with numbers: describe a concrete usage pattern for which
[]Item is the wrong structure, and name the operations that ruin it.
(Lesson 3 opens with exactly that scenario.)