Command
./perfect_hash.py -v --splitby=":" fruits.txt
fruits.txt
# fruit test data
Apple : 0
Apricot : 1
Avocado : 2
Banana : 3
Blueberry : 4
Blackberry : 5
Execution Log
keys_file = 'fruits.txt'
Reading table from file `fruits.txt' to extract keys.
Reader options:
comment: '#'
splitby: ':'
keycol: 1
Number os keys: 6
tmpl_file = None
outname = 'std'
NG = 7
Generating graphs NG = 7 .....
Generating graphs NG = 8 .....
Generating graphs NG = 9 ..
Acyclic graph found after 12 trials.
NG = 9
OK
Format options:
width: 76
indent: 4
delimiter: ', '
# =======================================================================
# ================= Python code for perfect hash function ===============
# =======================================================================
G = [0, 0, 5, 5, 6, 2, 0, 4, 7]
def hash_f(key, T):
return sum(ord(T[i % 10]) * ord(c) for i, c in enumerate(key)) % 9
def perfect_hash(key):
return (G[hash_f(key, "aPUw6kzCOV")] +
G[hash_f(key, "gfrifJM7cA")]) % 9
# ============================ Sanity check =============================
K = ["Apple", "Apricot", "Avocado", "Banana", "Blueberry",
"Blackberry"]
assert len(K) == 6
for h, k in enumerate(K):
assert perfect_hash(k) == h