Created
June 16, 2021 12:58
-
-
Save frederni/9362571d99a247c5ce385abced74467b to your computer and use it in GitHub Desktop.
TDT4145 Extendible hashing
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
# 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