Quick actions

cmd+k|ctrl+k

Navigation

Languages

HashMapImplementation

Snippet info

Language

Python

Visibility

public

Author

arthurpc02

Created

2024-09-23T12:35:41.533608Z

Updated

2024-09-23T14:21:25.273643Z

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!")
INFO