Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created March 4, 2021 07:26
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 amankharwal/e1d3291a9b01b612ca8f5ada74164fd1 to your computer and use it in GitHub Desktop.
Save amankharwal/e1d3291a9b01b612ca8f5ada74164fd1 to your computer and use it in GitHub Desktop.
class hashtable:
def __init__(self, items):
self.bucket_size = len(items)
self.buckets = [[] for i in range(self.bucket_size)]
self.assign_buckets(items)
def assign_buckets(self, items):
for key, value in elements:
hash_value = hash(key)
index = hash_value % self.bucket_size
self.buckets[index].append((key, value))
def get_value(self, input_keys):
hash_value = hash(input_keys)
index = hash_value % self.bucket_size
bucket = self.buckets[index]
for key, value in bucket:
if key == input_keys:
return(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment