Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created March 20, 2021 08:35
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/84c550cabd0ae40396a32d2928f40796 to your computer and use it in GitHub Desktop.
Save amankharwal/84c550cabd0ae40396a32d2928f40796 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