Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyHashTable:
- def __init__(self, capacity):
- self.capacity = capacity
- self.slots = [None] * self.capacity
- def __str__(self):
- return str(self.slots)
- def __len__(self):
- count = 0
- for i in self.slots:
- if i != None:
- count += 1
- return count
- def hash_function(self, key):
- # index of the internal array
- index = key % self.capacity
- if key in self.slots: # return index if key already exists
- return index
- try_index = []
- for i in range(index, self.capacity):
- try_index.append(i)
- for i in range(index):
- try_index.append(i)
- for i in try_index:
- if self.slots[i] is None:
- return i
- return None
- def insert(self, key):
- # -2 if exist
- # full -1
- # none if inserted
- index = self.hash_function(key)
- if index is None:
- return -1
- elif self.slots[index] == key:
- return -2
- else:
- self.slots[index] = key
- return index
- def dump(self):
- for key in self.slots:
- print(key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement