Design a fixed-size hash map in Python

We need some laughters.. Okay?

Question is…

implement a fixed-size hash map that associates string keys with arbitrary data object references.

hash_map.py

def hashing_func(key_item, fixed_size):
return sum(ord(ch) for ch in str(key_item)) % fixed_size
class hash_map():
fixed_size = 1000

def __init__(self, fixed_size):
if fixed_size <= 0:
raise ValueError('Please set a fixed size greater than 0')
self.fixed_size = fixed_size
self.capacity = 0 # used capacity

self.data = [None for _ in range(fixed_size)]

def get(self, key):
# if key: string or int, doesnt exist, throw keyError
index = hashing_func(key, self.fixed_size)
hash_table_item = self.data[index]

if hash_table_item and hash_table_item[0] == key:
return hash_table_item[1]
return None

def set(self, key, value):
index = hashing_func(key, self.fixed_size)

if self.data[index]:
if self.data[index][0] == key:
self.data[index] = [key, value]
return True
return False
if self.capacity == self.fixed_size:
return False
if not self.data[index]:
self.capacity += 1
self.data[index] = [key, value]

return True

def delete(self, key):
index = hashing_func(key, self.fixed_size)

if self.capacity == 0:
return None
hash_table_item = self.data[index]
if hash_table_item:
value = hash_table_item[1]
self.data[index] = None
return value
return hash_table_item


def load(self):
if self.capacity > self.fixed_size:
raise Exception('capacity has reached a maximium.')

return self.capacity / self.fixed_size

def __setitem__(self, key, value):
self.set(key, value)
def __getitem__(self, key):
return self.get(key)
def __delitem__(self, key):
return self.delete(key)

unit_tests.py (test cases for my hash_map)

#!/usr/bin/env python
import unittest
from hash_map import hash_map
class HashMapTestCases(unittest.TestCase):
# tests for my own hashmap
def test_contructor(self):
# does it create a hashmap with size?
self.assertTrue(hash_map(1) != None)

def test_successful_set_a_value(self):
# is set operation successfull with a value?
hashmap_with_size_1 = hash_map(1)
self.assertTrue(hashmap_with_size_1.set(1, 5))
def test_set_two_values_with_one_size(self):
# is set operation not successfull with two (key, value) pairs for 1 size?
hashmap_with_size_1 = hash_map(1)
hashmap_with_size_1.set(1, 5)
self.assertFalse(hashmap_with_size_1.set('abc', 5))

def test_get_with_wrong_key(self):
# is get operation None when trying to delete key that does not exist?
hashmap_with_size_50 = hash_map(50)
hashmap_with_size_50.set('5', 4)
hashmap_with_size_50.set('ha', 'asf')
self.assertTrue(hashmap_with_size_50.get('hulahla') == None)
def test_get_with_right_key(self):
# is get operation None when trying to delete key that does not exist?
hashmap_with_size_50 = hash_map(50)
hashmap_with_size_50.set('5', 4)
hashmap_with_size_50.set('ha', 'asf')
self.assertTrue(hashmap_with_size_50.get('ha') != None)
def test_delete_with_wrong_key(self):
# is delete operation None when trying to delete key that does not exist?
hashmap_with_size_50 = hash_map(50)
hashmap_with_size_50.set('5', 4)
hashmap_with_size_50.set('ha', 'asf')
self.assertTrue(hashmap_with_size_50.delete('hulahla') == None)
def test_delete_with_right_key(self):
# is delete operation is not None when trying to delete key that does exist?
hashmap_with_size_50 = hash_map(50)
hashmap_with_size_50.set('5', 4)
hashmap_with_size_50.set('ha', 'asf')
self.assertTrue(hashmap_with_size_50.delete('5') != None)
def test_load(self):
hashmap_with_size_50 = hash_map(50)
hashmap_with_size_50.set('5', 4)
hashmap_with_size_50.set('ha', 'asf')
hashmap_with_size_50.set('hah', 'aws')
hashmap_with_size_50.set('lambda', 'aws')
hashmap_with_size_50.set('paladin', 'cyber')
hashmap_with_size_50.set('h0h', 'kpcb')
self.assertTrue(hashmap_with_size_50.load() == 0.12)
if __name__ == '__main__':
unittest.main()

Do you want to download these files? Click here.