Skip to content

Commit 73cd894

Browse files
committed
Refactored a bit
1 parent e024825 commit 73cd894

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

Diff for: hashtable.py

+35-29
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,59 @@ def __init__(self, capacity=1000):
1616
# The outmost list is the one which the hash function maps the index to. The next inner
1717
# Array is the list of objects in that storage cell. The 3rd level is the individual
1818
# 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)
2031

2132
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
2334
updated. Key must be a string. Returns self. """
2435

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
3342

43+
self._find_by_key(key, find_result_func)
3444
return self
3545

3646
def get(self, key):
3747
""" Get object with key (key must be a string). If not found, it will raise a KeyError. """
3848

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)
4656

4757
def remove(self, key):
4858
""" Remove the object associated with key from the hashtable. If found, the object will
4959
be returned. If not found, KeyError will be raised. """
5060

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)
5964
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)
6370

6471
if __name__ == "__main__":
6572
import unittest
6673
testsuite = unittest.TestLoader().discover('test', pattern="*hashtable*")
6774
unittest.TextTestRunner(verbosity=1).run(testsuite)
68-

Diff for: test/test_hashtable.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,27 @@ def testCreate(self):
3232
self.assertEqual(ht.capacity, 1000)
3333

3434
def testSetAndGet(self):
35+
# Basic set and get
3536
ht = hashtable.HashTable(10)
3637
ht.set('a', 1)
3738
self.assertEqual(ht.get('a'), 1)
3839
self.assertEqual(ht.size, 1)
3940

41+
# Check update functionality
4042
ht.set('a', 2)
4143
self.assertEqual(ht.get('a'), 2)
42-
#self.assertEqual(ht.size, 1)
44+
self.assertEqual(ht.size, 1)
4345

46+
# Make sure we can add a 2nd element
4447
ht.set('b', 10)
4548
self.assertEqual(ht.get('b'), 10)
4649
self.assertEqual(ht.get('a'), 2)
47-
#self.assertEqual(ht.size, 2)
50+
self.assertEqual(ht.size, 2)
4851

4952
# Assert ht.set returns itself (for fluent calls)
5053
self.assertEqual(ht.set('c', 5), ht)
5154

55+
# Test fluent set functionality
5256
ht.set('d', 100).set('e', 200).set('f', 300)
5357
self.assertEqual(ht.get('d'), 100)
5458
self.assertEqual(ht.get('e'), 200)
@@ -67,7 +71,6 @@ def testRemove(self):
6771
self.assertEqual(removed_item, 1)
6872
self.assertEqual(ht.size, 0)
6973

70-
7174
if __name__ == '__main__':
7275
unittest.main()
7376

0 commit comments

Comments
 (0)