dsa.hashtable
Module containing hash table class.
1""" Module containing hash table class. """ 2 3class HashTable: 4 """ 5 A hashtable implementation. 6 """ 7 def __init__(self, capacity=20): 8 """ 9 Initialize a hashtable with a given capacity. 10 11 Args: 12 capacity: The capacity of the hashtable. 13 """ 14 self.capacity = capacity 15 16 self.array = [] 17 for _ in range(self.capacity): 18 self.array.append([]) 19 20 #: the number of items in the hashtable 21 self.count = 0 22 23 def hash_function(self, key) -> int: 24 """ 25 Return a hash value based on a given key. 26 27 Args: 28 key: The key to convert to a hashvalue. 29 Returns: 30 Hash value modded to the hashtable capacity. 31 """ 32 mult = 31 33 hash_val = 0 34 for character in str(key): 35 hash_val *= mult 36 hash_val += ord(character) 37 hash_val %= (2**32) 38 39 return hash_val % self.capacity 40 41 def key_exists(self, key) -> bool: 42 """ 43 Returns a Boolean on whether a key exists in the hashtable or not . 44 45 Args: 46 key: The key to check for in the hashtable. 47 Returns: 48 Boolean of key existence. 49 """ 50 bucket = self.hash_function(key) 51 52 for e in self.array[bucket]: 53 if e[0] == key: 54 return True 55 return False 56 57 def set(self, key, value): 58 """ 59 Set a key-value pair in the hashtable. 60 61 If key exists, replace the value otherwise, create a new key-pair. 62 63 Args: 64 key: The key to check for. 65 value: The value to set or create. 66 """ 67 68 bucket = self.hash_function(key) 69 70 # linear searh for key 71 for e in self.array[bucket]: 72 if e[0] == key: 73 e[1] = value 74 break 75 else: 76 self.array[bucket].append([ key, value ]) 77 self.count += 1 78 79 def get(self, key): 80 """ 81 Get corresponding value of a given key in the hash table. 82 83 Args: 84 key: The key to check for. 85 value: The value to set or create. 86 Returns: 87 corresponding value of key. 88 None if key is not found. 89 """ 90 bucket = self.hash_function(key) 91 92 for e in self.array[bucket]: 93 if e[0] == key: 94 return e[1] 95 96 return None 97 98 def delete(self, key): 99 """ 100 Delete key-value pair if specified key is found. 101 102 Args: 103 key: The key to check for. 104 """ 105 bucket = self.hash_function(key) 106 107 for i in range(len(self.array[bucket])): 108 kvpair = self.array[bucket][i] 109 if kvpair and kvpair[0] == key: 110 del self.array[bucket][i] 111 self.count -= 1 112 break 113 114 def __repr__(self): 115 """ 116 Return a string representation of the hashtable. 117 """ 118 s = "{" 119 pairs = [] 120 for bucket in self.array: 121 if bucket: 122 for chain_link in bucket: 123 pairs.append(f"{chain_link[0]}:{chain_link[1]}") 124 s += ", ".join(pairs) 125 return s + "}" 126 127 def show_buckets(self): 128 """ 129 Return a string displaying the contents of all buckets in the hashtable. 130 """ 131 s = "" 132 for i, bucket in enumerate(self.array): 133 s += f"Bucket {i}: {bucket}\n" 134 return s 135 136 def __len__(self): 137 """ 138 Return the number of items in the hashtable. 139 """ 140 return self.count 141 142 def __getitem__(self, key): 143 """ 144 Get the value associated with the key using indexing. 145 146 Args: 147 key: The key to look up. 148 Returns: 149 The value associated with the key. 150 """ 151 return self.get(key) 152 153 def __setitem__(self, key, value): 154 """ 155 Set the value associated with the key using indexing. 156 157 Args: 158 key: The key to set. 159 value: The value to associate with the key. 160 """ 161 self.set(key, value) 162 163 def __delitem__(self, key): 164 """ 165 Delete the key-value pair associated with the key using indexing. 166 167 Args: 168 key: The key to delete. 169 """ 170 self.delete(key) 171 172 def __contains__(self, key): 173 """ 174 Check if the key exists in the hashtable using 'in' operator. 175 176 Args: 177 key: The key to check for existence. 178 Returns: 179 True if key exists, False otherwise. 180 """ 181 return self.key_exists(key) 182 183 def pop(self, key, default=None): 184 """ 185 Remove specified key and return the value. 186 If key is not found, return default. 187 """ 188 bucket = self.hash_function(key) 189 for i, kvpair in enumerate(self.array[bucket]): 190 if kvpair and kvpair[0] == key: 191 value = kvpair[1] 192 del self.array[bucket][i] 193 self.count -= 1 194 return value 195 return default
4class HashTable: 5 """ 6 A hashtable implementation. 7 """ 8 def __init__(self, capacity=20): 9 """ 10 Initialize a hashtable with a given capacity. 11 12 Args: 13 capacity: The capacity of the hashtable. 14 """ 15 self.capacity = capacity 16 17 self.array = [] 18 for _ in range(self.capacity): 19 self.array.append([]) 20 21 #: the number of items in the hashtable 22 self.count = 0 23 24 def hash_function(self, key) -> int: 25 """ 26 Return a hash value based on a given key. 27 28 Args: 29 key: The key to convert to a hashvalue. 30 Returns: 31 Hash value modded to the hashtable capacity. 32 """ 33 mult = 31 34 hash_val = 0 35 for character in str(key): 36 hash_val *= mult 37 hash_val += ord(character) 38 hash_val %= (2**32) 39 40 return hash_val % self.capacity 41 42 def key_exists(self, key) -> bool: 43 """ 44 Returns a Boolean on whether a key exists in the hashtable or not . 45 46 Args: 47 key: The key to check for in the hashtable. 48 Returns: 49 Boolean of key existence. 50 """ 51 bucket = self.hash_function(key) 52 53 for e in self.array[bucket]: 54 if e[0] == key: 55 return True 56 return False 57 58 def set(self, key, value): 59 """ 60 Set a key-value pair in the hashtable. 61 62 If key exists, replace the value otherwise, create a new key-pair. 63 64 Args: 65 key: The key to check for. 66 value: The value to set or create. 67 """ 68 69 bucket = self.hash_function(key) 70 71 # linear searh for key 72 for e in self.array[bucket]: 73 if e[0] == key: 74 e[1] = value 75 break 76 else: 77 self.array[bucket].append([ key, value ]) 78 self.count += 1 79 80 def get(self, key): 81 """ 82 Get corresponding value of a given key in the hash table. 83 84 Args: 85 key: The key to check for. 86 value: The value to set or create. 87 Returns: 88 corresponding value of key. 89 None if key is not found. 90 """ 91 bucket = self.hash_function(key) 92 93 for e in self.array[bucket]: 94 if e[0] == key: 95 return e[1] 96 97 return None 98 99 def delete(self, key): 100 """ 101 Delete key-value pair if specified key is found. 102 103 Args: 104 key: The key to check for. 105 """ 106 bucket = self.hash_function(key) 107 108 for i in range(len(self.array[bucket])): 109 kvpair = self.array[bucket][i] 110 if kvpair and kvpair[0] == key: 111 del self.array[bucket][i] 112 self.count -= 1 113 break 114 115 def __repr__(self): 116 """ 117 Return a string representation of the hashtable. 118 """ 119 s = "{" 120 pairs = [] 121 for bucket in self.array: 122 if bucket: 123 for chain_link in bucket: 124 pairs.append(f"{chain_link[0]}:{chain_link[1]}") 125 s += ", ".join(pairs) 126 return s + "}" 127 128 def show_buckets(self): 129 """ 130 Return a string displaying the contents of all buckets in the hashtable. 131 """ 132 s = "" 133 for i, bucket in enumerate(self.array): 134 s += f"Bucket {i}: {bucket}\n" 135 return s 136 137 def __len__(self): 138 """ 139 Return the number of items in the hashtable. 140 """ 141 return self.count 142 143 def __getitem__(self, key): 144 """ 145 Get the value associated with the key using indexing. 146 147 Args: 148 key: The key to look up. 149 Returns: 150 The value associated with the key. 151 """ 152 return self.get(key) 153 154 def __setitem__(self, key, value): 155 """ 156 Set the value associated with the key using indexing. 157 158 Args: 159 key: The key to set. 160 value: The value to associate with the key. 161 """ 162 self.set(key, value) 163 164 def __delitem__(self, key): 165 """ 166 Delete the key-value pair associated with the key using indexing. 167 168 Args: 169 key: The key to delete. 170 """ 171 self.delete(key) 172 173 def __contains__(self, key): 174 """ 175 Check if the key exists in the hashtable using 'in' operator. 176 177 Args: 178 key: The key to check for existence. 179 Returns: 180 True if key exists, False otherwise. 181 """ 182 return self.key_exists(key) 183 184 def pop(self, key, default=None): 185 """ 186 Remove specified key and return the value. 187 If key is not found, return default. 188 """ 189 bucket = self.hash_function(key) 190 for i, kvpair in enumerate(self.array[bucket]): 191 if kvpair and kvpair[0] == key: 192 value = kvpair[1] 193 del self.array[bucket][i] 194 self.count -= 1 195 return value 196 return default
A hashtable implementation.
8 def __init__(self, capacity=20): 9 """ 10 Initialize a hashtable with a given capacity. 11 12 Args: 13 capacity: The capacity of the hashtable. 14 """ 15 self.capacity = capacity 16 17 self.array = [] 18 for _ in range(self.capacity): 19 self.array.append([]) 20 21 #: the number of items in the hashtable 22 self.count = 0
Initialize a hashtable with a given capacity.
Args: capacity: The capacity of the hashtable.
24 def hash_function(self, key) -> int: 25 """ 26 Return a hash value based on a given key. 27 28 Args: 29 key: The key to convert to a hashvalue. 30 Returns: 31 Hash value modded to the hashtable capacity. 32 """ 33 mult = 31 34 hash_val = 0 35 for character in str(key): 36 hash_val *= mult 37 hash_val += ord(character) 38 hash_val %= (2**32) 39 40 return hash_val % self.capacity
Return a hash value based on a given key.
Args: key: The key to convert to a hashvalue. Returns: Hash value modded to the hashtable capacity.
42 def key_exists(self, key) -> bool: 43 """ 44 Returns a Boolean on whether a key exists in the hashtable or not . 45 46 Args: 47 key: The key to check for in the hashtable. 48 Returns: 49 Boolean of key existence. 50 """ 51 bucket = self.hash_function(key) 52 53 for e in self.array[bucket]: 54 if e[0] == key: 55 return True 56 return False
Returns a Boolean on whether a key exists in the hashtable or not .
Args: key: The key to check for in the hashtable. Returns: Boolean of key existence.
58 def set(self, key, value): 59 """ 60 Set a key-value pair in the hashtable. 61 62 If key exists, replace the value otherwise, create a new key-pair. 63 64 Args: 65 key: The key to check for. 66 value: The value to set or create. 67 """ 68 69 bucket = self.hash_function(key) 70 71 # linear searh for key 72 for e in self.array[bucket]: 73 if e[0] == key: 74 e[1] = value 75 break 76 else: 77 self.array[bucket].append([ key, value ]) 78 self.count += 1
Set a key-value pair in the hashtable.
If key exists, replace the value otherwise, create a new key-pair.
Args: key: The key to check for. value: The value to set or create.
80 def get(self, key): 81 """ 82 Get corresponding value of a given key in the hash table. 83 84 Args: 85 key: The key to check for. 86 value: The value to set or create. 87 Returns: 88 corresponding value of key. 89 None if key is not found. 90 """ 91 bucket = self.hash_function(key) 92 93 for e in self.array[bucket]: 94 if e[0] == key: 95 return e[1] 96 97 return None
Get corresponding value of a given key in the hash table.
Args: key: The key to check for. value: The value to set or create. Returns: corresponding value of key. None if key is not found.
99 def delete(self, key): 100 """ 101 Delete key-value pair if specified key is found. 102 103 Args: 104 key: The key to check for. 105 """ 106 bucket = self.hash_function(key) 107 108 for i in range(len(self.array[bucket])): 109 kvpair = self.array[bucket][i] 110 if kvpair and kvpair[0] == key: 111 del self.array[bucket][i] 112 self.count -= 1 113 break
Delete key-value pair if specified key is found.
Args: key: The key to check for.
128 def show_buckets(self): 129 """ 130 Return a string displaying the contents of all buckets in the hashtable. 131 """ 132 s = "" 133 for i, bucket in enumerate(self.array): 134 s += f"Bucket {i}: {bucket}\n" 135 return s
Return a string displaying the contents of all buckets in the hashtable.
184 def pop(self, key, default=None): 185 """ 186 Remove specified key and return the value. 187 If key is not found, return default. 188 """ 189 bucket = self.hash_function(key) 190 for i, kvpair in enumerate(self.array[bucket]): 191 if kvpair and kvpair[0] == key: 192 value = kvpair[1] 193 del self.array[bucket][i] 194 self.count -= 1 195 return value 196 return default
Remove specified key and return the value. If key is not found, return default.