Last active
January 13, 2020 20:43
-
-
Save mikaelsouza/7e119894b9e2b2442b5d24150b324ade to your computer and use it in GitHub Desktop.
Implementação de um HashMap simples em Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashMap(object): | |
def __init__(self, bucket_size=10): | |
self.bucket_size = bucket_size | |
self.buckets = [None] * self.bucket_size | |
def insert(self, key, value): | |
hashed_key = hash(key) | |
bucket_index = hashed_key % self.bucket_size | |
self.buckets[bucket_index] = value | |
def retrieve(self, key): | |
hashed_key = hash(key) | |
bucket_index = hashed_key % self.bucket_size | |
return self.buckets[bucket_index] | |
hm = HashMap() | |
hm.insert("Teste", 12345) | |
print(hm.retrieve("Teste")) | |
print(hm.retrieve("Teste2")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment