1+ class KeyValue :
2+ def __init__ (self , key : None , value : None ):
3+ self .key = key
4+ self .value = value
15
26class HashMap :
3- def __init__ (self , size : int = 11 ):
4- self .size = size
5- self .keys : list = [None ] * self .size
6- self .values : list = [None ] * self .size
7+ def __init__ (self , size :int = 11 ):
8+ self .size : int = size
9+ self .items : list = [None ] * self .size
710 self .length : int = 0
811
912 def put (self , key , value ):
13+ keyValue : KeyValue = KeyValue (key , value )
1014 hash : int = self .hash (key )
11- if self .keys [hash ] is None :
12- self .keys [hash ] = [key ]
13- self .values [hash ] = [value ]
15+ if self .items [hash ] is None :
16+ self .items [hash ] = [keyValue ]
1417 self .length += 1
1518 else :
16- found : bool = False
1719 count : int = 0
18- while count < len (self .keys [hash ]) and not found :
19- if self .keys [hash ][count ] == key :
20+ found : bool = False
21+ while count < len (self .items [hash ]) and not found :
22+ if self .items [hash ][count ].key == keyValue .key :
2023 found = True
2124 else :
2225 count += 1
2326 if found :
24- self .values [hash ][count ] = value
27+ self .items [hash ][count ] = keyValue
2528 else :
26- self .keys [hash ].append (key )
27- self .values [hash ].append (value )
29+ self .items [hash ].append (keyValue )
2830 self .length += 1
29-
31+
3032 def get (self , key ):
3133 hash : int = self .hash (key )
32- if self .keys [hash ] is None :
34+ if self .items [hash ] is None :
3335 return None
3436 else :
35- found : bool = False
3637 count : int = 0
37- while count < len (self .keys [hash ]) and not found :
38- if self .keys [hash ][count ] == key :
38+ found : bool = False
39+ while count < len (self .items [hash ]) and not found :
40+ if self .items [hash ][count ].key == key :
3941 found = True
4042 else :
4143 count += 1
4244 if found :
43- return self .values [hash ][count ]
45+ return self .items [hash ][count ]. value
4446 else :
4547 return None
4648
47- def delete (self , key ):
49+
50+ def contains (self , key )-> bool :
4851 hash : int = self .hash (key )
49- if self .keys [hash ] is None :
50- return
52+ if self .items [hash ] is None :
53+ return False
5154 else :
52- found : bool = False
5355 count : int = 0
54- while count < len (self .keys [hash ]) and not found :
55- if self .keys [hash ][count ] == key :
56+ found : bool = False
57+ while count < len (self .items [hash ]) and not found :
58+ if self .items [hash ][count ].key == key :
5659 found = True
5760 else :
5861 count += 1
59- if found :
60- self .keys [hash ].pop (count )
61- self .values [hash ].pop (count )
62- self .length -= 1
63- else :
64- return None
62+ return found
6563
66- def contains (self , key )-> bool :
64+ def delete (self , key ):
6765 hash : int = self .hash (key )
68- if self .keys [hash ] is None :
69- return False
66+ if self .items [hash ] is None :
67+ return None
7068 else :
71- found : bool = False
7269 count : int = 0
73- while count < len (self .keys [hash ]) and not found :
74- if self .keys [hash ][count ] == key :
70+ found : bool = False
71+ while count < len (self .items [hash ]) and not found :
72+ if self .items [hash ][count ].key == key :
7573 found = True
7674 else :
7775 count += 1
78- return found
76+ if found :
77+ return self .items [hash ].pop (count )
78+ else :
79+ return None
7980
8081 def hash (self , key ):
8182 return key % self .size
82-
83+
84+
8385ht : HashMap = HashMap ()
8486
8587ht .put (11 , "string 11" )
@@ -90,11 +92,7 @@ def hash(self, key):
9092
9193ht .put (21 , "string 21" )
9294
93- print (ht .keys )
94- print (ht .values )
95-
9695print (ht .contains (11 ), ht .contains (33 ), ht .contains (21 ), ht .contains (117 ))
97- print (ht .delete (11 ))
96+ print (ht .delete (11 ). value )
9897print (ht .contains (22 ))
9998print (ht .get (22 ))
100-
0 commit comments