. Consider the following definition of a hashtable with a partially implemented
ID: 3740758 • Letter: #
Question
. Consider the following definition of a hashtable with a partially implemented rehash method, which will either grow or shrink the buckets array to the provided value n_buckets (while keeping all existing key/value mappings).
class Hashtable:
def rehash(self, n_buckets):
new_buckets = [None] * n_buckets
for b in self.buckets:
while b:
b_next = b.next
________________________________
________________________________
________________________________
b = b_next
self.buckets = new_buckets
Which correctly completes the rehash method?
(a) idx = hash(b.key) % n_buckets
b.next = new_buckets[idx]
new_buckets[idx] = b
(b) idx = hash(b.key) % len(self.buckets)
b.next = new_buckets[idx]
self.buckets[idx] = b
(c) idx = hash(b.key) % len(self.buckets)
self.buckets[idx] = new_buckets[idx]
b.next = new_buckets[idx]
(d) idx = hash(b.key) % n_buckets
new_buckets[idx].next = b
b.next = new_buckets[idx]
(e) idx = hash(b.key) % n_buckets
b.next = new_buckets[idx].next
new_buckets[idx].next = b
Explanation / Answer
d) idx = hash(b.key) % n_buckets
new_buckets[idx].next = b
b.next = new_buckets[idx]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.