Skip to content

Instantly share code, notes, and snippets.

@tef
Created November 9, 2013 12:21
Show Gist options
  • Save tef/7384888 to your computer and use it in GitHub Desktop.
Save tef/7384888 to your computer and use it in GitHub Desktop.
class ShittyHashTable:
def __init__(self, buckets = 8):
self.buckets = []
for x in range(8):
self.buckets.append([])
def add(self, key, value):
n = shittyhash(key, len(self.buckets))
bucket = self.buckets[n]
for i, pair in enumerate(bucket):
k,v = pair
if k == key:
bucket[i] = (key, value)
return
bucket.append((key, value))
def lookup(self, key):
n = shittyhash(key, len(self.buckets))
bucket = self.buckets[n]
for pair in bucket:
k,v = pair
if k == key:
return v
def shittyhash(value, buckets):
# turn a string into a number between 0 and buckets
total = 31337
for char in value:
v = ord(char) # character to number
total = total ^ v
total = total * 23
total= total % buckets
print("Hash of", value, "is", total)
return total
if __name__ == '__main__':
print("Shitty hash table")
s = ShittyHashTable()
s.add("lol", "butts")
s.add("cat", "robot")
print(s.lookup("lol"))
print(s.lookup("cat"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment