HashMapImplementation

Run Settings
LanguagePython
Language Version
Run Command
class HashTable(): def __init__(self, max_size): self.size = max_size self.data = [] for idx in range(max_size): self.data.append(0) def _hash(self, key): hash_value = 0 for idx in range(len(key)): hash_value = (hash_value + ord(key[idx])*idx) % self.size return hash_value def set(self, key, value): position = self._hash(key) # print(f"position: {position}") self.data[position] = list((key,value)) pass def get(self, key): position = self._hash(key) return self.data[position][0] pass def keys(self): pass iterable = [] for item in self.data: if item != 0: key = item[0] iterable.append(key) return iterable print("Hello World!") myHashTable = HashTable(50) myHashTable.set("grapes", 10000) myHashTable.set("apples", 54) myHashTable.set("oranges", 2) print(myHashTable.get("grapes")) print(myHashTable.get("apples")) print(myHashTable.get("oranges")) print(myHashTable.keys()) print("Good bye cruel World!")
Editor Settings
Theme
Key bindings
Full width
Lines