28/40
Part 5 of 7 · Inside a dictLearn and do
When every key has the same hash, each operation is O(n)
Now the worst case, on purpose. Give a class a hash that's always 1:
class Good:
def __init__(self, n):
self.n = n
def __eq__(self, other):
return self.n == other.n
def __hash__(self):
return hash(self.n)
class Bad(Good):
def __hash__(self):
return 1
Every Bad key wants the same slot, so each insert probes past all the keys before it.
Every key in bads hashes to the same value. Double the keys: how much longer does set(bads) take to build?
Pick an answer above to see the rest