11
2- 32from typing import Any , List
43
54
65class HashTable :
76
8- 97 def __init__ (self , capacity :int = 11 ) -> None :
10- self .capacity :int = capacity
8+ self .capacity :int = capacity
119 self .length : int = 0
1210 self .keys : List [int ] = [None ] * self .capacity
1311 self .values : List [Any ] = [None ] * self .capacity
1412
15- 16- def put (self , key : int , value : Any ) -> int :
17- index : int = self .hash (key )
18- if self .keys [index ] is None or self .keys [index ] == - 1 :
19- self .keys [index ] = key
13+ def put (self , key :int , value :Any ):
14+ index :int = self .put_helper (key )
15+ if index != - 1 :
2016 self .values [index ] = value
17+ return
18+ hash :int = self .hash (key )
19+ if self .keys [hash ] is None or self .keys [hash ] == - 1 :
20+ self .keys [hash ] = key
21+ self .values [hash ] = value
2122 self .length += 1
22- elif self .keys [index ] == key :
23- self .values [index ] = value
23+ elif self .keys [hash ] == key :
24+ self .values [hash ] = value
2425 else :
25- new_index : int = self .rehash (index )
26- while new_index != index and self .keys [new_index ] is not None and self .keys [new_index ] != - 1 and self .keys [new_index ] != key :
27- new_index = self .rehash (new_index )
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 :
28+ new_hash = self .rehash (new_hash )
2829
29- if self .keys [new_index ] is None or self .keys [new_index ] == - 1 :
30- self .keys [new_index ] = key
31- self .values [new_index ] = value
30+ if self .keys [new_hash ] is None or self .keys [new_hash ] == - 1 :
31+ self .keys [new_hash ] = key
32+ self .values [new_hash ] = value
3233 self .length += 1
33- elif self .keys [new_index ] == key :
34- self .values [new_index ] = value
34+ elif self .keys [new_hash ] == key :
35+ self .values [new_hash ] = value
36+ 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 )
3547
48+ if self .keys [new_hash ] == key :
49+ return new_hash
50+ else :
51+ return - 1
3652
37- def get (self , key : int ) -> Any :
38- index : int = self .hash (key )
39- if self .keys [index ] == key :
40- return self .values [index ]
41- elif self .keys [index ] is None :
53+ def get (self , key :int ) -> Any :
54+ hash :int = self .hash (key )
55+ if self .keys [hash ] is None :
4256 return None
57+ elif self .keys [hash ] == key :
58+ return self .values [hash ]
4359 else :
44- new_index : int = self .rehash (index )
45- while new_index != index and self .keys [new_index ] is not None and self .keys [new_index ] != key :
46- new_index = self .rehash (new_index )
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 )
4763
48- if self .keys [new_index ] == key :
49- return self .values [new_index ]
50- elif self . keys [ new_index ] is None :
64+ if self .keys [new_hash ] == key :
65+ return self .values [new_hash ]
66+ else :
5167 return None
5268
53- 54- def contains (self , key : int ) -> bool :
55- index : int = self .hash (key )
56- if self .keys [index ] == key :
57- return True
58- elif self .keys [index ] is None :
69+ def contains (self , key :int ) -> bool :
70+ hash :int = self .hash (key )
71+ if self .keys [hash ] is None :
5972 return False
73+ elif self .keys [hash ] == key :
74+ return True
6075 else :
61- new_index : int = self .rehash (index )
62- while new_index != index and self .keys [new_index ] is not None and self .keys [new_index ] != key :
63- new_index = self .rehash (new_index )
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 )
6479
65- if self .keys [new_index ] == key :
80+ if self .keys [new_hash ] == key :
6681 return True
67- elif self . keys [ new_index ] is None :
82+ else :
6883 return False
6984
70- 71- def delete (self , key : int ) -> None :
72- index : int = self .hash (key )
73- if self .keys [index ] == key :
74- self .values [index ] = - 1
75- self .values [index ] = None
85+ def delete (self , key :int ):
86+ 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
7692 self .length -= 1
77- return None
78- elif self .keys [index ] is None :
79- return None
8093 else :
81- new_index : int = self .rehash (index )
82- while new_index != index and self .keys [new_index ] is not None and self .keys [new_index ] != key :
83- new_index = self .rehash (new_index )
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 :
96+ new_hash = self .rehash (new_hash )
8497
85- if self .keys [new_index ] == key :
86- self .values [ new_index ] = - 1
87- self .values [new_index ] = None
98+ if self .keys [new_hash ] == key :
99+ self .keys [ new_hash ] = - 1
100+ self .values [new_hash ] = None
88101 self .length -= 1
89- return None
90102 else :
91103 return None
92104
105+ def size (self ) -> int :
106+ return self .length
93107
94- def hash (self , key :int ) -> int :
108+ def hash (self , key :int ) -> int :
95109 return key % self .capacity
96110
97- 98- def rehash (self , index : int ) -> int :
99- return (index + 1 ) % self .capacity
100- 101- 102- def size (self ) -> int :
103- return self .length
111+ def rehash (self , old_hash :int ) -> int :
112+ return (old_hash + 1 ) % self .capacity
104113
105114
106115
@@ -129,6 +138,9 @@ def size(self) -> int:
129138print ('Contains 22' , ht .contains (22 ))
130139print ('----------------------------------------' )
131140
141+ print ('Contains 44' , ht .contains (44 ))
142+ print (ht .keys )
143+ print (ht .values )
132144print ('Contains 77' , ht .contains (77 ))
133145ht .put (44 , 'string 144' )
134146ht .put (77 , 'string 77' )
@@ -137,3 +149,4 @@ def size(self) -> int:
137149print (ht .keys )
138150print (ht .values )
139151print ('Contains 77' , ht .contains (77 ))
152+ print ('Contains 44' , ht .contains (44 ))
0 commit comments