Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion applications/expensive_seq/expensive_seq.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
# Your code here

cache = {}

def expensive_seq(x, y, z):
# Your code here
if (x, y, z) in cache:
return cache[(x, y, z)]

if x <= 0:
value = y + z
else:
value = expensive_seq(x - 1, y + 1, z) + expensive_seq(x - 2, y + 2, z * 2) + expensive_seq(x - 3, y + 3, z * 3)

cache[(x, y, z)] = value

return value


print(cache)



Expand Down
30 changes: 30 additions & 0 deletions hashtable/hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class HashTable:

def __init__(self, capacity):
# Your code here
self.capacity = MIN_CAPACITY
self.table = [None] * capacity


def get_num_slots(self):
Expand All @@ -35,6 +37,7 @@ def get_num_slots(self):
Implement this.
"""
# Your code here
return self.capacity


def get_load_factor(self):
Expand All @@ -54,6 +57,15 @@ def fnv1(self, key):
"""

# Your code here
fnv_prime = 2**40 + 2**8 + int('0xb3', 16)
hash = 14695981039346656037
key_bytes = key.encode()

for b in key_bytes:
hash = hash * fnv_prime
hash = hash ^ b

return hash


def djb2(self, key):
Expand All @@ -63,6 +75,12 @@ def djb2(self, key):
Implement this, and/or FNV-1.
"""
# Your code here
hash = 5381

for char in key:
hash = 33 * hash ^ ord(char)

return hash


def hash_index(self, key):
Expand All @@ -82,6 +100,9 @@ def put(self, key, value):
Implement this.
"""
# Your code here
i = self.hash_index(key)
self.table[i] = value



def delete(self, key):
Expand All @@ -93,6 +114,12 @@ def delete(self, key):
Implement this.
"""
# Your code here
i = self.hash_index(key)

if self.table[i]:
self.table[i] = None
else:
print(f'{key} not found')


def get(self, key):
Expand All @@ -104,6 +131,9 @@ def get(self, key):
Implement this.
"""
# Your code here
i = self.hash_index(key)

return self.table[i] if self.table[i] else None


def resize(self, new_capacity):
Expand Down