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: str) -> 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 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: str) -> 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: str, 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: str): 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: str): 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
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: str) -> 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 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: str) -> 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: str, 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: str): 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: str): 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
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: str) -> 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 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: str) -> 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: str, 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: str): 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: str): 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.