BIGO-P4

A growing platform

Practice 4 of 5Cost: Big-O and friends7 tests

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

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

7 tests, not run yet
Test results
#TestResultReads · time
1A new listNot run
2Five appendsNot run
3Values read backNot run
4Exactly fullNot run
5One more than fullNot run
6Real storageNot run
7200,000 appendsNot run