@@ -16,53 +16,59 @@ def __init__(self, capacity=1000):
16
16
# The outmost list is the one which the hash function maps the index to. The next inner
17
17
# Array is the list of objects in that storage cell. The 3rd level is the individual
18
18
# item array, where the 1st item is the key, and the 2nd item is the value.
19
- self .data = [[] for i in range (capacity )]
19
+ self .data = [[] for _ in range (capacity )]
20
+
21
+ def _find_by_key (self , key , find_result_func ):
22
+ index = hash_function (key , self .capacity )
23
+ hash_table_cell = self .data [index ]
24
+ found_item = None
25
+ for item in hash_table_cell :
26
+ if item [0 ] == key :
27
+ found_item = item
28
+ break
29
+
30
+ return find_result_func (found_item , hash_table_cell )
20
31
21
32
def set (self , key , obj ):
22
- """ Insert object with key into hashtable . If key already exists, then the object will be
33
+ """ Insert object with key into hash table . If key already exists, then the object will be
23
34
updated. Key must be a string. Returns self. """
24
35
25
- index = hash_function (key , self .capacity )
26
- storage_cell = self .data [index ]
27
- for item in storage_cell :
28
- if item [0 ] == key :
29
- item [1 ] = obj
30
- else :
31
- storage_cell .append ([key , obj ])
32
- self .size += 1
36
+ def find_result_func (found_item , hash_table_cell ):
37
+ if found_item :
38
+ found_item [1 ] = obj
39
+ else :
40
+ hash_table_cell .append ([key , obj ])
41
+ self .size += 1
33
42
43
+ self ._find_by_key (key , find_result_func )
34
44
return self
35
45
36
46
def get (self , key ):
37
47
""" Get object with key (key must be a string). If not found, it will raise a KeyError. """
38
48
39
- index = hash_function ( key , self . capacity )
40
- storage_cell = self . data [ index ]
41
- for item in storage_cell :
42
- if item [ 0 ] == key :
43
- return item [ 1 ]
44
- else :
45
- raise KeyError (key )
49
+ def find_result_func ( found_item , _ ):
50
+ if found_item :
51
+ return found_item [ 1 ]
52
+ else :
53
+ raise KeyError ( key )
54
+
55
+ return self . _find_by_key (key , find_result_func )
46
56
47
57
def remove (self , key ):
48
58
""" Remove the object associated with key from the hashtable. If found, the object will
49
59
be returned. If not found, KeyError will be raised. """
50
60
51
- index = hash_function (key , self .capacity )
52
- storage_cell = self .data [index ]
53
- item_to_remove = None
54
- for item in storage_cell :
55
- if item [0 ] == key :
56
- item_to_remove = item
57
- if item_to_remove :
58
- storage_cell .remove (item )
61
+ def find_result_func (found_item , hash_table_cell ):
62
+ if found_item :
63
+ hash_table_cell .remove (found_item )
59
64
self .size -= 1
60
- return item [1 ]
61
- else :
62
- raise KeyError (key )
65
+ return found_item [1 ]
66
+ else :
67
+ raise KeyError (key )
68
+
69
+ return self ._find_by_key (key , find_result_func )
63
70
64
71
if __name__ == "__main__" :
65
72
import unittest
66
73
testsuite = unittest .TestLoader ().discover ('test' , pattern = "*hashtable*" )
67
74
unittest .TextTestRunner (verbosity = 1 ).run (testsuite )
68
-
0 commit comments