Hash Table
"""
Hashtables (associative arrays)
"""
# linear probing
class HashTable(object):
def __init__(self):
self.size = 10
self.keys = [None] * self.size
self.values = [None] * self.size
def put(self, key, data):
index = self.hashfunction(key)
# not None -> it is a collision !!!
while self.keys[index] is not None:
if self.keys[index] == key:
self.values[index] = data # update
return
# rehash try to find another slot
index = (index + 1) % self.size
# insert
self.keys[index] = key
self.values[index] = data
def get(self, key):
index = self.hashfunction(key)
while self.keys[index] is not None:
if self.keys[index] == key:
return self.values[index]
index = (index + 1) % self.size
# it means the key is not present in the associative array
return None
def hashfunction(self, key):
sum = 0
for pos in range(len(key)):
sum = sum + ord(key[pos])
return sum % self.size
table = HashTable()
table.put("apple", 10)
table.put("orange", 20)
table.put("car", 30)
table.put("table", 40)
print(table.get("car"))
Hash Table
Describe an implementation of a least-used cache, and big-O notation of it.
A question involving an API's integration with hash map where the buckets of hash map are made up of linked lists.
Implement data structure
Mapstoring pairs of integers (key, value) and define following member functions in O(1) runtime:void insert(key, value),void delete(key),int get(key),int getRandomKey().
Last updated
Was this helpful?