The problem
Build the table from the figure in lesson 3: m slots, keys are whole numbers, and a key’s home slot is key % m. If a slot is taken, try the next one, (i + 1) % m. The table never grows.
containsstarts at the home slot and probes until it finds the key (True) or an empty slot (False). It skips'DEL'markers. It never looks at more thanmslots.removefinds the key the same way and puts'DEL'in its slot.insertdoes nothing if the key is already there, and returns its slot. Otherwise it puts the key in the first'DEL'slot it passed, or else in the empty slot where it stopped, and returns that slot. If it finds neither inmprobes, it returns-1.
T = ProbeTable(10)
T.insert(23); T.insert(43); T.insert(13) # 3, 4, 5
T.remove(43) # True
T.slots()
# [None, None, None, 23, 'DEL', 13,
# None, None, None, None]
T.contains(13) # True
The tests make an object and call its methods in order. A test shows the list of calls, and compares the list of everything they returned (methods that return nothing give None).
Limits
- Up to 131,072 slots and 100,000 keys. Keys may be negative.
Builds on HASH-3 Open addressing and load factor.
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 | The figure's inserts | Not run | |
| 2 | Find | Not run | |
| 3 | Insert a key twice | Not run | |
| 4 | Wrap around the end | Not run | |
| 5 | Remove leaves a marker | Not run | |
| 6 | Insert reuses a marker | Not run | |
| 7 | No copy after a marker | Not run | |
| 8 | Remove a missing key | Not run | |
| 9 | A full table | Not run | |
| 10 | Negative keys | Not run | |
| 11 | 100,000 keys | Not run |