This code is meant to implement a hash table class which uses linear probing.
I'm doing this to improve my style and to improve my knowledge of fundamental algorithms/data structures for an upcoming coding interview.
class Hash:
def __init__(self):
self.size = 11
self.vals = [None] * self.size
self.keys = [None] * self.size
self.step = 1
def put(self, key, val):
ind = self.hash(key)
if self.keys[ind] == None:
self.keys[ind] = key
self.vals[ind] = val
elif self.keys[ind] == key:
self.vals[ind] = val
else:
ind = self.rehash(ind)
while (self.keys[ind] != None and
self.keys[ind] != key):
ind = self.rehash(ind)
if self.keys[ind] == None:
self.keys[ind] = key
self.vals[ind] = val
elif self.keys[ind] == key:
self.vals[ind] = val
def get(self, key):
ind = self.hash(key)
start_ind = ind
val = None
done = False
while not done:
if self.keys[ind] == key:
val = self.vals[ind]
done = True
else:
ind = self.rehash(ind)
if ind == start_ind:
done = True
elif self.keys[ind] == None:
done = True
return val
def hash(self, key):
return key % self.size
def rehash(self, oldhash):
return (oldhash + self.step) % self.size
def __getitem__(self, key):
return self.get(key)
def __setitem__(self, key, val):
self.put(key, val)