Skip to content

Commit 5bb6696

Browse files
committed
Tweaks to the hash table code during final QA
1 parent 35da9ed commit 5bb6696

File tree

16 files changed

+30
-39
lines changed

16 files changed

+30
-39
lines changed

hashtable/01_hashtable_prototype/07_use_defensive_copying/hashtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get(self, key, default=None):
4646

4747
@property
4848
def pairs(self):
49-
return [pair for pair in self._pairs.copy() if pair]
49+
return [pair for pair in self._pairs if pair]
5050

5151
def _index(self, key):
5252
return hash(key) % len(self)

hashtable/01_hashtable_prototype/08_get_the_keys_and_values/hashtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get(self, key, default=None):
4646

4747
@property
4848
def pairs(self):
49-
return {pair for pair in self._pairs.copy() if pair}
49+
return {pair for pair in self._pairs if pair}
5050

5151
@property
5252
def values(self):

hashtable/01_hashtable_prototype/09_report_the_hash_tables_length/hashtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get(self, key, default=None):
4848

4949
@property
5050
def pairs(self):
51-
return {pair for pair in self._slots.copy() if pair}
51+
return {pair for pair in self._slots if pair}
5252

5353
@property
5454
def values(self):

hashtable/01_hashtable_prototype/10_make_the_hash_table_iterable/hashtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get(self, key, default=None):
5151

5252
@property
5353
def pairs(self):
54-
return {pair for pair in self._slots.copy() if pair}
54+
return {pair for pair in self._slots if pair}
5555

5656
@property
5757
def values(self):

hashtable/01_hashtable_prototype/11_represent_the_hash_table_in_text/hashtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def get(self, key, default=None):
6868

6969
@property
7070
def pairs(self):
71-
return {pair for pair in self._slots.copy() if pair}
71+
return {pair for pair in self._slots if pair}
7272

7373
@property
7474
def values(self):

hashtable/01_hashtable_prototype/12_test_the_equality_of_hash_tables/hashtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get(self, key, default=None):
7878

7979
@property
8080
def pairs(self):
81-
return {pair for pair in self._slots.copy() if pair}
81+
return {pair for pair in self._slots if pair}
8282

8383
@property
8484
def values(self):

hashtable/02_linear_probing/hashtable.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ def get(self, key, default=None):
9595

9696
@property
9797
def pairs(self):
98-
return {
99-
pair for pair in self._slots.copy() if pair not in (None, DELETED)
100-
}
98+
return {pair for pair in self._slots if pair not in (None, DELETED)}
10199

102100
@property
103101
def values(self):

hashtable/02_linear_probing/test_hashtable.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def test_should_compare_equal_different_capacity():
305305
assert h1 == h2
306306

307307

308-
@patch("builtins.hash", return_value=42)
308+
@patch("builtins.hash", return_value=24)
309309
def test_should_detect_and_resolve_hash_collisions(mock_hash):
310310
hash_table = HashTable(capacity=100)
311311
hash_table["hola"] = "hello"
@@ -314,9 +314,9 @@ def test_should_detect_and_resolve_hash_collisions(mock_hash):
314314

315315
assert len(hash_table) == 3
316316

317-
assert hash_table._slots[42] == ("hola", "hello")
318-
assert hash_table._slots[43] == (98.6, 37)
319-
assert hash_table._slots[44] == (False, True)
317+
assert hash_table._slots[24] == ("hola", "hello")
318+
assert hash_table._slots[25] == (98.6, 37)
319+
assert hash_table._slots[26] == (False, True)
320320

321321

322322
@patch("builtins.hash", side_effect=[2, 1, 1])
@@ -358,7 +358,7 @@ def test_should_raise_memory_error_when_not_enough_capacity():
358358
assert exception_info.value.args[0] == "Not enough capacity"
359359

360360

361-
@patch("builtins.hash", return_value=42)
361+
@patch("builtins.hash", return_value=24)
362362
def test_should_get_collided_values(mock_hash):
363363
hash_table = HashTable(capacity=3)
364364
hash_table["hola"] = "hello"

hashtable/03_autoresize/hashtable.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ def get(self, key, default=None):
9696

9797
@property
9898
def pairs(self):
99-
return {
100-
pair for pair in self._slots.copy() if pair not in (None, DELETED)
101-
}
99+
return {pair for pair in self._slots if pair not in (None, DELETED)}
102100

103101
@property
104102
def values(self):

hashtable/03_autoresize/test_hashtable.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def test_should_compare_equal_different_capacity():
309309
assert h1 == h2
310310

311311

312-
@patch("builtins.hash", return_value=42)
312+
@patch("builtins.hash", return_value=24)
313313
def test_should_detect_and_resolve_hash_collisions(mock_hash):
314314
hash_table = HashTable(capacity=100)
315315
hash_table["hola"] = "hello"
@@ -318,9 +318,9 @@ def test_should_detect_and_resolve_hash_collisions(mock_hash):
318318

319319
assert len(hash_table) == 3
320320

321-
assert hash_table._slots[42] == ("hola", "hello")
322-
assert hash_table._slots[43] == (98.6, 37)
323-
assert hash_table._slots[44] == (False, True)
321+
assert hash_table._slots[24] == ("hola", "hello")
322+
assert hash_table._slots[25] == (98.6, 37)
323+
assert hash_table._slots[26] == (False, True)
324324

325325

326326
@patch("builtins.hash", side_effect=[2, 1, 1])
@@ -369,7 +369,7 @@ def test_should_double_capacity():
369369
}
370370

371371

372-
@patch("builtins.hash", return_value=42)
372+
@patch("builtins.hash", return_value=24)
373373
def test_should_get_collided_values(mock_hash):
374374
hash_table = HashTable(capacity=3)
375375
hash_table["hola"] = "hello"

0 commit comments

Comments
 (0)