fib with memoization
This commit is contained in:
@@ -23,6 +23,10 @@ def hash(word):
|
||||
return hash_key % 16
|
||||
|
||||
|
||||
def get_at_hash(key, container):
|
||||
return container[hash(key)]
|
||||
|
||||
|
||||
def insert_at_hash(key, value, container):
|
||||
key_hash = hash(key)
|
||||
|
||||
@@ -32,11 +36,11 @@ def insert_at_hash(key, value, container):
|
||||
return container
|
||||
else:
|
||||
# resolve collision
|
||||
# but I don't know what was the key, it could any permutation
|
||||
# of the letters BAD in the example I am working through
|
||||
existing_value = container[key_hash]
|
||||
|
||||
|
||||
def get_at_hash(key, container):
|
||||
return container[hash(key)]
|
||||
container[key_hash] = [[existing_value], [key, value]]
|
||||
return container
|
||||
|
||||
|
||||
container = [None for i in range(15)]
|
||||
@@ -45,4 +49,4 @@ insert_at_hash("bad", "evil", container)
|
||||
insert_at_hash("dab", "good", container)
|
||||
|
||||
|
||||
get_at_hash("dab", container)
|
||||
get_at_hash("bad", container)
|
||||
|
||||
Reference in New Issue
Block a user