-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_map.py
More file actions
54 lines (33 loc) · 1.02 KB
/
Copy pathhash_map.py
File metadata and controls
54 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import unittest
def int_hash_function(an_int, size):
index = an_int % size
return index
class MyDict(object):
def __init__(self, size=10):
self.size = size
self._array = [None] * size
def my_set(self, key, val):
index = int_hash_function(key, self.size)
self._array[index] = val
def my_get(self, key):
index = int_hash_function(key, self.size)
return self._array[index]
def my_in(self, val):
for existing_val in self._array:
if val == existing_val:
return True
return False
class TestHashTable(unittest.TestCase):
def test_can_hash_integer(self):
got = int_hash_function(0, 10)
assert got == 0
def test_can_use_int_as_key(self):
a_dict = MyDict()
got = a_dict.my_get(4)
assert got == None
a_dict.my_set(4, 4)
got = a_dict.my_get(4)
assert got == 4
a_dict.my_set(4, 5)
got = a_dict.my_get(4)
assert got == 5