-
-
Save codecademydev/357d0309183c75006b11e07a9ef7ee34 to your computer and use it in GitHub Desktop.
Codecademy export
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: | |
def __init__(self, size): | |
self.array_size = size | |
self.array=[None for number in range(size)] | |
def hash(self, key): | |
return sum(key.encode()) | |
def compress(self, hash_code): | |
return hash_code % self.array_size | |
def assign(self, key, value): | |
array_index = self.compress(self.hash(key)) | |
self.array[array_index] = [key, value] | |
def retrieve(self, key): | |
array_index = self.compress(self.hash(key)) | |
payload = self.array[array_index] | |
if payload[0] == key: | |
return payload[1] | |
return None | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment