diff --git a/applications/expensive_seq/expensive_seq.py b/applications/expensive_seq/expensive_seq.py index 5c82b8453..fa3686973 100644 --- a/applications/expensive_seq/expensive_seq.py +++ b/applications/expensive_seq/expensive_seq.py @@ -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) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0205f0ba9..4fe6b8875 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -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): @@ -35,6 +37,7 @@ def get_num_slots(self): Implement this. """ # Your code here + return self.capacity def get_load_factor(self): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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):