Skip to content

Instantly share code, notes, and snippets.

@frederni
Created June 16, 2021 12:58
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 frederni/9362571d99a247c5ce385abced74467b to your computer and use it in GitHub Desktop.
Save frederni/9362571d99a247c5ce385abced74467b to your computer and use it in GitHub Desktop.
TDT4145 Extendible hashing
# Simple script used in extendible hashing tasks for course TDT4145 at NTNU
import math
inputs = [int(n) for n in input("Paste list of keys, sep with ',': ").split(',')]
n = input("Enter hash function, k mod ")
if n == '':
# Fallback if no hash function is given
m = max(inputs)
n = math.floor(math.sqrt(m))**2
else: n = int(n)
binlen = int(math.log(n, 2))
def hsh(k):
return k % n
bin_vals = []
print("k", "h(k)", "bin", sep='\t')
for inp in inputs:
bin_vals.append(bin(hsh(inp))[2:].zfill(binlen))
print(inp, hsh(inp), bin_vals[-1], sep='\t')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment