11
2- from typing import Any , List
2+ 3+ from typing import Any , List , Tuple
34
45
56class HashTable :
67
78 def __init__ (self , capacity :int = 11 ) -> None :
8- self .capacity :int = capacity
9- self .length : int = 0
9+ self .capacity :int = capacity
1010 self .keys : List [int ] = [None ] * self .capacity
11- self .values : List [Any ] = [None ] * self .capacity
11+ self .values : List [int ] = [None ] * self .capacity
12+ self .length : int = 0
13+ 1214
1315 def put (self , key :int , value :Any ):
14- index : int = self .put_helper (key )
15- if index != - 1 :
16+ index , contains = self .find (key )
17+ if contains :
1618 self .values [index ] = value
1719 return
1820 hash :int = self .hash (key )
19- if self .keys [hash ] is None or self .keys [hash ] == - 1 :
20- self .keys [hash ] = key
21+ if self .keys [hash ] == float ( 'inf' ) or self .keys [hash ] is None :
22+ self .keys [hash ] = key
2123 self .values [hash ] = value
2224 self .length += 1
23- elif self .keys [hash ] == key :
24- self .values [hash ] = value
2525 else :
26- new_hash :int = self .rehash (hash )
27- while new_hash != hash and self .keys [new_hash ] is not None and self .keys [new_hash ] != - 1 and self . keys [ new_hash ] != key :
26+ new_hash :int = self .rehash (hash )% self . capacity
27+ while self .keys [new_hash ] is not None and self .keys [new_hash ] != float ( 'inf' ) and new_hash != hash :
2828 new_hash = self .rehash (new_hash )
29- 30- if self .keys [new_hash ] is None or self .keys [new_hash ] == - 1 :
31- self .keys [new_hash ] = key
29+ if self .keys [new_hash ] == float ('inf' ) or self .keys [new_hash ] is None :
30+ self .keys [new_hash ] = key
3231 self .values [new_hash ] = value
3332 self .length += 1
34- elif self .keys [new_hash ] == key :
35- self .values [new_hash ] = value
3633
37- def put_helper (self , key ) -> int :
38- hash :int = self .hash (key )
39- if self .keys [hash ] is None :
40- return - 1
41- elif self .keys [hash ] == key :
42- return hash
43- else :
44- new_hash :int = self .rehash (hash )
45- while new_hash != hash and self .keys [new_hash ] is not None and self .keys [new_hash ] != key :
46- new_hash = self .rehash (new_hash )
34+ 35+ def contains (self , key :int ) -> bool :
36+ _ , contains = self .find (key )
37+ return contains
4738
48- if self .keys [new_hash ] == key :
49- return new_hash
50- else :
51- return - 1
5239
5340 def get (self , key :int ) -> Any :
54- hash :int = self .hash (key )
55- if self .keys [hash ] is None :
56- return None
57- elif self .keys [hash ] == key :
58- return self .values [hash ]
59- else :
60- new_hash :int = self .rehash (hash )
61- while new_hash != hash and self .keys [new_hash ] is not None and self .keys [new_hash ] != key :
62- new_hash = self .rehash (new_hash )
41+ index , contains = self .find (key )
42+ if contains :
43+ return self .values [index ]
44+ return None
6345
64- if self .keys [new_hash ] == key :
65- return self .values [new_hash ]
66- else :
67- return None
6846
69- def contains (self , key :int ) -> bool :
70- hash :int = self .hash (key )
71- if self .keys [hash ] is None :
72- return False
73- elif self .keys [hash ] == key :
74- return True
75- else :
76- new_hash :int = self .rehash (hash )
77- while new_hash != hash and self .keys [new_hash ] is not None and self .keys [new_hash ] != key :
78- new_hash = self .rehash (new_hash )
47+ def delete (self , key :int ):
48+ index , contains = self .find (key )
49+ if not contains :
50+ return
51+ self .keys [index ] = float ('inf' )
52+ self .values [index ] = None
53+ self .length -= 1
7954
80- if self .keys [new_hash ] == key :
81- return True
82- else :
83- return False
8455
85- def delete (self , key :int ):
56+ def find (self , key :int )-> Tuple [ int , bool ] :
8657 hash :int = self .hash (key )
87- if self .keys [hash ] is None :
88- return
89- elif self .keys [hash ] == key :
90- self .keys [hash ] = - 1
91- self .values [hash ] = None
92- self .length -= 1
58+ if self .keys [hash ] == key :
59+ return (hash , True )
60+ elif self .keys [hash ] is None :
61+ return (None , False )
9362 else :
94- new_hash :int = self .rehash (hash )
95- while new_hash != hash and self .keys [new_hash ] is not None and self . keys [ new_hash ] != key :
63+ new_hash :int = self .rehash (hash )% self . capacity
64+ while self . keys [ new_hash ] != key and self .keys [new_hash ] is not None and new_hash != hash :
9665 new_hash = self .rehash (new_hash )
9766
9867 if self .keys [new_hash ] == key :
99- self .keys [new_hash ] = - 1
100- self .values [new_hash ] = None
101- self .length -= 1
102- else :
103- return None
68+ return (new_hash , True )
69+ return (None , False )
70+ 10471
10572 def size (self ) -> int :
10673 return self .length
10774
10875 def hash (self , key :int ) -> int :
10976 return key % self .capacity
11077
78+ 11179 def rehash (self , old_hash :int ) -> int :
11280 return (old_hash + 1 ) % self .capacity
11381
11482
115- 11683ht : HashTable = HashTable ()
11784ht .put (11 , 'string 11' )
11885ht .put (22 , 'string 22' )
@@ -126,7 +93,7 @@ def rehash(self, old_hash:int) -> int:
12693print (ht .values )
12794print (ht .size ())
12895print ('Get 11' , ht .get (11 ))
129- print ('Get 33 ' , ht .get (33 ))
96+ print ('Get 22 ' , ht .get (22 ))
13097print ('Get 147' , ht .get (147 ))
13198print ('----------------------------------------' )
13299
@@ -150,3 +117,4 @@ def rehash(self, old_hash:int) -> int:
150117print (ht .values )
151118print ('Contains 77' , ht .contains (77 ))
152119print ('Contains 44' , ht .contains (44 ))
120+
0 commit comments