|
1 | 1 |
|
2 | | -class HashMap: |
3 | | - def __init__(self, length: int = 11): |
4 | | - self._length: int = length |
5 | | - self._keys: list = [None] * self._length |
6 | | - self._values: list = [None] * self._length |
7 | | - self._elements_count: int = 0 |
8 | | - |
9 | | - def put(self, key: int, value: str): |
| 2 | +from typing import Any, List, Tuple |
| 3 | + |
| 4 | + |
| 5 | +class HashTable: |
| 6 | + def __init__(self, capacity: int = 11) -> None: |
| 7 | + self.capacity: int = capacity |
| 8 | + self.keys: List[int] = [None] * self.capacity |
| 9 | + self.values: List[Any] = [None] * self.capacity |
| 10 | + self.length: int = 0 |
| 11 | + |
| 12 | + def size(self) -> int: |
| 13 | + return self.length |
| 14 | + |
| 15 | + def put(self, key:int, value: Any) -> None: |
| 16 | + (contains, index) = self._contains_and_index(key) |
| 17 | + if contains: |
| 18 | + self.values[index] = value |
| 19 | + return |
10 | 20 | hash: int = self.hash(key) |
11 | | - if self._keys[hash] is None or self._keys[hash] == "deleted" or self._keys[hash] == key: |
12 | | - if self._keys[hash] is None or self._keys[hash] == "deleted": |
13 | | - self._elements_count += 1 |
14 | | - self._keys[hash] = key |
15 | | - self._values[hash] = value |
| 21 | + if self.keys[hash] is None or self.keys[hash] == 'deleted': |
| 22 | + self.keys[hash] = key |
| 23 | + self.values[hash] = value |
| 24 | + self.length += 1 |
| 25 | + elif self.keys[hash] == key: |
| 26 | + self.values[hash] = value |
16 | 27 | else: |
17 | 28 | new_hash: int = self.rehash(hash) |
18 | | - while new_hash != hash and self._keys[new_hash] is not None and self._keys[new_hash] != "deleted"\ |
19 | | - and self._keys[new_hash] != key: |
| 29 | + while self.keys[new_hash] is not None and self.keys[new_hash] != 'deleted' and self.keys[new_hash] != key and new_hash != hash: |
20 | 30 | new_hash = self.rehash(new_hash) |
21 | | - if self._keys[new_hash] is None or self._keys[new_hash] == "deleted" or self._keys[new_hash] == key: |
22 | | - if self._keys[new_hash] is None or self._keys[new_hash] == "deleted": |
23 | | - self._elements_count += 1 |
24 | | - self._keys[new_hash] = key |
25 | | - self._values[new_hash] = value |
26 | | - |
27 | | - def get(self, key: int): |
| 31 | + if self.keys[new_hash] is None or self.keys[new_hash] == 'deleted': |
| 32 | + self.keys[new_hash] = key |
| 33 | + self.values[new_hash] = value |
| 34 | + self.length += 1 |
| 35 | + elif self.keys[new_hash] == key: |
| 36 | + self.values[new_hash] = value |
| 37 | + |
| 38 | + def get(self, key:int) -> Any: |
28 | 39 | hash: int = self.hash(key) |
29 | | - if self._keys[hash] is None: |
| 40 | + if self.keys[hash] is None: |
30 | 41 | return None |
31 | | - elif self._keys[hash] == key: |
32 | | - return self._values[hash] |
| 42 | + elif self.keys[hash] == key: |
| 43 | + return self.values[hash] |
33 | 44 | else: |
34 | 45 | new_hash: int = self.rehash(hash) |
35 | | - while new_hash != hash and self._keys[new_hash] is not None and self._keys[new_hash] != key: |
| 46 | + while self.keys[new_hash] is not None and self.keys[new_hash] != key and new_hash != hash: |
36 | 47 | new_hash = self.rehash(new_hash) |
37 | | - if self._keys[new_hash] is None: |
38 | | - return None |
39 | | - elif self._keys[new_hash] == key: |
40 | | - return self._values[new_hash] |
| 48 | + if self.keys[new_hash] == key: |
| 49 | + return self.values[new_hash] |
41 | 50 | else: |
42 | 51 | return None |
43 | 52 |
|
44 | | - def contains(self, key: int) -> bool: |
| 53 | + def contains(self, key:int) -> bool: |
45 | 54 | hash: int = self.hash(key) |
46 | | - if self._keys[hash] is None: |
| 55 | + if self.keys[hash] is None: |
47 | 56 | return False |
48 | | - elif self._keys[hash] == key: |
| 57 | + elif self.keys[hash] == key: |
49 | 58 | return True |
50 | 59 | else: |
51 | 60 | new_hash: int = self.rehash(hash) |
52 | | - while new_hash != hash and self._keys[new_hash] is not None and self._keys[new_hash] != key: |
| 61 | + while self.keys[new_hash] is not None and self.keys[new_hash] != key and new_hash != hash: |
53 | 62 | new_hash = self.rehash(new_hash) |
54 | | - if self._keys[new_hash] is None: |
55 | | - return False |
56 | | - elif self._keys[new_hash] == key: |
| 63 | + if self.keys[new_hash] == key: |
57 | 64 | return True |
58 | 65 | else: |
59 | 66 | return False |
60 | 67 |
|
61 | | - def delete(self, key): |
| 68 | + def _contains_and_index(self, key:int) -> Tuple[bool, int]: |
62 | 69 | hash: int = self.hash(key) |
63 | | - if self._keys[hash] is None: |
64 | | - return |
65 | | - elif self._keys[hash] == key: |
66 | | - self._elements_count -= 1 |
67 | | - self._keys[hash] = "deleted" |
68 | | - self._values[hash] = None |
| 70 | + if self.keys[hash] is None: |
| 71 | + return (False, -1) |
| 72 | + elif self.keys[hash] == key: |
| 73 | + return (True, hash) |
69 | 74 | else: |
70 | 75 | new_hash: int = self.rehash(hash) |
71 | | - while new_hash != hash and self._keys[new_hash] is not None and self._keys[new_hash] != key: |
| 76 | + while self.keys[new_hash] is not None and self.keys[new_hash] != key and new_hash != hash: |
72 | 77 | new_hash = self.rehash(new_hash) |
73 | | - if self._keys[new_hash] == key: |
74 | | - self._elements_count -= 1 |
75 | | - self._keys[new_hash] = "deleted" |
76 | | - self._values[new_hash] = None |
77 | | - |
78 | | - def size(self) -> int: |
79 | | - return self._elements_count |
80 | | - |
81 | | - def hash(self, key: int) -> int: |
82 | | - return key % self._length |
| 78 | + if self.keys[new_hash] == key: |
| 79 | + return (True, new_hash) |
| 80 | + else: |
| 81 | + return (False, new_hash) |
83 | 82 |
|
84 | | - def rehash(self, old_hash: int): |
85 | | - return (old_hash + 1) % self._length |
86 | 83 |
|
| 84 | + def delete(self, key:int) -> None: |
| 85 | + hash: int = self.hash(key) |
| 86 | + if self.keys[hash] is None: |
| 87 | + return |
| 88 | + elif self.keys[hash] == key: |
| 89 | + self.keys[hash] = 'deleted' |
| 90 | + self.length -= 1 |
| 91 | + else: |
| 92 | + new_hash: int = self.rehash(hash) |
| 93 | + while self.keys[new_hash] is not None and self.keys[new_hash] != key and new_hash != hash: |
| 94 | + new_hash = self.rehash(new_hash) |
| 95 | + if self.keys[new_hash] == key: |
| 96 | + self.keys[new_hash] = 'deleted' |
| 97 | + self.length -= 1 |
| 98 | + else: |
| 99 | + return False |
87 | 100 |
|
88 | | -hm: HashMap = HashMap() |
89 | | -hm.put(11, "string 11") |
90 | | -hm.put(22, "string 22") |
91 | | -hm.put(33, "string 33") |
92 | | -hm.put(44, "string 44") |
93 | | -hm.put(12, "string 12") |
94 | | -hm.put(21, "string 21") |
95 | 101 |
|
96 | | -print(hm._keys) |
97 | | -print(hm._values) |
| 102 | + def hash(self, key:int) -> int: |
| 103 | + return key % self.capacity |
98 | 104 |
|
99 | | -print("Get 11", hm.get(11)) |
100 | | -print("Get 33", hm.get(33)) |
101 | | -print("Get 21", hm.get(21)) |
102 | | -print("Get 7", hm.get(7)) |
| 105 | + def rehash(self, old_hash) -> int: |
| 106 | + return (old_hash + 1) % self.capacity |
103 | 107 |
|
104 | | -print("Contains key 7", hm.contains(7)) |
105 | | -print("Contains key 33", hm.contains(33)) |
106 | 108 |
|
| 109 | +ht: HashTable = HashTable() |
| 110 | +ht.put(11, 'string 11') |
| 111 | +ht.put(22, 'string 22') |
| 112 | +ht.put(33, 'string 33') |
| 113 | +ht.put(44, 'string 44') |
107 | 114 |
|
108 | | -print("Delete key 7", hm.delete(7)) |
109 | | -print("Delete key 33", hm.delete(33)) |
| 115 | +ht.put(21, 'string 21') |
| 116 | +ht.put(12, 'string 12') |
110 | 117 |
|
111 | | -print("Contains key 33", hm.contains(33)) |
| 118 | +print(ht.keys) |
| 119 | +print(ht.values) |
| 120 | +print(ht.size()) |
| 121 | +print('Get 11', ht.get(11)) |
| 122 | +print('Get 33', ht.get(33)) |
| 123 | +print('Get 147', ht.get(147)) |
| 124 | +print('----------------------------------------') |
112 | 125 |
|
113 | | -print(hm._keys) |
114 | | -print(hm._values) |
| 126 | +print('Contains 22', ht.contains(22)) |
| 127 | +ht.delete(22) |
| 128 | +print(ht.size()) |
| 129 | +print(ht.keys) |
| 130 | +print(ht.values) |
| 131 | +print('Contains 22', ht.contains(22)) |
| 132 | +print('----------------------------------------') |
115 | 133 |
|
116 | | -hm.put(14, "string 14") |
| 134 | +print('Contains 77', ht.contains(77)) |
| 135 | +ht.put(44, 'string 144') |
| 136 | +ht.put(77, 'string 77') |
117 | 137 |
|
118 | | -print("Contains key 33", hm.contains(33)) |
| 138 | +print(ht.size()) |
| 139 | +print(ht.keys) |
| 140 | +print(ht.values) |
| 141 | +print('Contains 77', ht.contains(77)) |
119 | 142 |
|
120 | | -print(hm._keys) |
121 | | -print(hm._values) |
|
0 commit comments