MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _add_item

Method _add_item

data_structures/hashing/hash_map.py:134–175  ·  view source on GitHub ↗

Try to add 3 elements when the size is 5 >>> hm = HashMap(5) >>> hm._add_item(1, 10) >>> hm._add_item(2, 20) >>> hm._add_item(3, 30) >>> hm HashMap(1: 10, 2: 20, 3: 30) Try to add 3 elements when the size is 5 >>> hm = HashMap

(self, key: KEY, val: VAL)

Source from the content-addressed store, hash-verified

132 ind = self._get_next_ind(ind)
133
134 def _add_item(self, key: KEY, val: VAL) -> None:
135 """
136 Try to add 3 elements when the size is 5
137 >>> hm = HashMap(5)
138 >>> hm._add_item(1, 10)
139 >>> hm._add_item(2, 20)
140 >>> hm._add_item(3, 30)
141 >>> hm
142 HashMap(1: 10, 2: 20, 3: 30)
143
144 Try to add 3 elements when the size is 5
145 >>> hm = HashMap(5)
146 >>> hm._add_item(-5, 10)
147 >>> hm._add_item(6, 30)
148 >>> hm._add_item(-7, 20)
149 >>> hm
150 HashMap(-5: 10, 6: 30, -7: 20)
151
152 Try to add 3 elements when size is 1
153 >>> hm = HashMap(1)
154 >>> hm._add_item(10, 13.2)
155 >>> hm._add_item(6, 5.26)
156 >>> hm._add_item(7, 5.155)
157 >>> hm
158 HashMap(10: 13.2)
159
160 Trying to add an element with a key that is a floating point value
161 >>> hm = HashMap(5)
162 >>> hm._add_item(1.5, 10)
163 >>> hm
164 HashMap(1.5: 10)
165
166 5. Trying to add an item with the same key
167 >>> hm = HashMap(5)
168 >>> hm._add_item(1, 10)
169 >>> hm._add_item(1, 20)
170 >>> hm
171 HashMap(1: 20)
172 """
173 for ind in self._iterate_buckets(key):
174 if self._try_set(ind, key, val):
175 break
176
177 def __setitem__(self, key: KEY, val: VAL) -> None:
178 """

Callers 2

_resizeMethod · 0.95
__setitem__Method · 0.95

Calls 2

_iterate_bucketsMethod · 0.95
_try_setMethod · 0.95

Tested by

no test coverage detected