The problem
Build the growing list from lesson 7 yourself. A DynArray keeps its values in self.slots, a Python list whose length is always self.capacity. Unused slots hold None.
Write append(x). If the slots are full (size == capacity), make a new list of Nones twice as long, copy the stored values into it one by one, and add the number of values copied to self.copies. Then write x into the next free slot.
d = DynArray()
for x in [10, 20, 30, 40, 50]:
d.append(x)
d.size, d.capacity, d.copies # (5, 8, 7): copies of 1, 2 and 4
d.get(4) # 50
Do not call append on self.slots. The point is to see what Python’s own list does for you. The tests check the three counters and read every value back.
Limits
- Up to 200,000 appends.
Builds on BIGO-7 Amortised analysis.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | A new list | Not run | |
| 2 | Five appends | Not run | |
| 3 | Values read back | Not run | |
| 4 | Exactly full | Not run | |
| 5 | One more than full | Not run | |
| 6 | Real storage | Not run | |
| 7 | 200,000 appends | Not run |