Skip to content

Instantly share code, notes, and snippets.

@mikaelsouza
Last active January 13, 2020 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikaelsouza/7e119894b9e2b2442b5d24150b324ade to your computer and use it in GitHub Desktop.
Save mikaelsouza/7e119894b9e2b2442b5d24150b324ade to your computer and use it in GitHub Desktop.
Implementação de um HashMap simples em Python
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