From 5891d97b1257489d911cfd7a6a2bd1e49748a253 Mon Sep 17 00:00:00 2001 From: Andrew Haberman Date: Wed, 7 Oct 2020 13:53:33 -0400 Subject: [PATCH 1/5] hashtable.py --- hashtable/hashtable.py | 83 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0205f0ba9..f2b4a43d4 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -11,6 +11,9 @@ def __init__(self, key, value): # Hash table can't have fewer than this many slots MIN_CAPACITY = 8 +class LinkedList: + def __init__(self): + self.head = None class HashTable: """ @@ -22,6 +25,9 @@ class HashTable: def __init__(self, capacity): # Your code here + self.capacity = MIN_CAPACITY + self.count = 0 + self.storage = [LinkedList()] * self.capacity def get_num_slots(self): @@ -35,7 +41,7 @@ def get_num_slots(self): Implement this. """ # Your code here - + return self.capacity def get_load_factor(self): """ @@ -44,7 +50,7 @@ def get_load_factor(self): Implement this. """ # Your code here - + return self.count / len(self.capacity) def fnv1(self, key): """ @@ -54,6 +60,7 @@ def fnv1(self, key): """ # Your code here + def djb2(self, key): @@ -63,7 +70,10 @@ def djb2(self, key): Implement this, and/or FNV-1. """ # Your code here - + hash = 5381 + for item in key: + hash = (hash * 33) + ord(item) + return hash def hash_index(self, key): """ @@ -82,6 +92,25 @@ def put(self, key, value): Implement this. """ # Your code here + index = self.hash_index(key) + + # if LL is empty + if self.storage[index].head == None: + self.storage[index].head = HashTableEntry(key, value) + self.count += 1 + return + + else: + current = self.storage[index].head + + while current.next: + + if current.key == key: + current.value = value + current = current.next + + current.next = HashTableEntry(key, value) + self.count += 1 def delete(self, key): @@ -93,6 +122,21 @@ def delete(self, key): Implement this. """ # Your code here + index = self.hash_index(key) + current = self.storage[index].head + + if current.key == key: + self.storage[index].head = self.storage[index].head.next + self.count -= 1 + return + + while current.next: + prev = current + current = current.next + if current.key == key: + prev.next = current.next + self.count -= 1 + return None def get(self, key): @@ -104,7 +148,20 @@ def get(self, key): Implement this. """ # Your code here + index = self.hash_index(key) + current = self.storage[index].head + + if current == None: + return None + if current.key == key: + return current.value + + while current.next: + current = current.next + if current.key == key: + return current.value + return None def resize(self, new_capacity): """ @@ -114,7 +171,25 @@ def resize(self, new_capacity): Implement this. """ # Your code here + self.capacity = new_capacity + new_list = [LinkedList()] * new_capacity + + for i in self.storage: + current = i.head + while current is not None: + index = self.hash_index(current.key) + + if new_list[index].head == None: + new_list[index].head = HashTableEntry(current.key, current.value) + else: + node = HashTableEntry(current.key, current.value) + + node.next = new_list[index].head + + new_list[index].head = node + current = current.next + self.storage = new_list if __name__ == "__main__": @@ -150,4 +225,4 @@ def resize(self, new_capacity): for i in range(1, 13): print(ht.get(f"line_{i}")) - print("") + print("") \ No newline at end of file From 3c26b076fde67e66abe77faa361a52b4f2d52404 Mon Sep 17 00:00:00 2001 From: Andrew Haberman Date: Wed, 7 Oct 2020 13:55:08 -0400 Subject: [PATCH 2/5] word count --- applications/word_count/word_count.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/applications/word_count/word_count.py b/applications/word_count/word_count.py index a20546425..a7e8a9e0a 100644 --- a/applications/word_count/word_count.py +++ b/applications/word_count/word_count.py @@ -1,5 +1,23 @@ def word_count(s): # Your code here + cache = {} + words_lowercased = s.lower() + ignored_chars = '" : ; , . - + = / \ | [ ] { } ( ) * ^ &'.split(" ") + + for chars in ignored_chars: + words_lowercased = words_lowercased.replace(chars, "") + + for words in words_lowercased.split(): + print(words) + if words == "": + continue + if words not in cache: + cache[words] = 1 + else: + cache[words] += 1 + return cache + + From c539945cb6ceea633cc0fbd29d8f3a30ea9cb8da Mon Sep 17 00:00:00 2001 From: Andrew Haberman Date: Wed, 7 Oct 2020 13:56:25 -0400 Subject: [PATCH 3/5] no dups --- applications/no_dups/no_dups.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/applications/no_dups/no_dups.py b/applications/no_dups/no_dups.py index caa162c8c..9bb9b5074 100644 --- a/applications/no_dups/no_dups.py +++ b/applications/no_dups/no_dups.py @@ -1,6 +1,13 @@ def no_dups(s): # Your code here + cache = {} + formated_string =[] + for word in s.split(): + if word not in cache: + cache[word] = 1 + formated_string.append(word) + return " ".join(formated_string) if __name__ == "__main__": From d5576df5f6c17476e2446e34d249456b30968f95 Mon Sep 17 00:00:00 2001 From: Andrew Haberman Date: Wed, 7 Oct 2020 13:57:45 -0400 Subject: [PATCH 4/5] lookup table --- applications/lookup_table/lookup_table.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/applications/lookup_table/lookup_table.py b/applications/lookup_table/lookup_table.py index 05b7d37fa..cbff6d277 100644 --- a/applications/lookup_table/lookup_table.py +++ b/applications/lookup_table/lookup_table.py @@ -1,5 +1,8 @@ # Your code here +import math +import random +cache = {} def slowfun_too_slow(x, y): v = math.pow(x, y) @@ -9,12 +12,19 @@ def slowfun_too_slow(x, y): return v + + def slowfun(x, y): """ Rewrite slowfun_too_slow() in here so that the program produces the same output, but completes quickly instead of taking ages to run. """ # Your code here + pairs = (x, y) + if pairs not in cache: + cache[pairs] = slowfun_too_slow(x, y) + + return cache[pairs] @@ -23,4 +33,4 @@ def slowfun(x, y): for i in range(50000): x = random.randrange(2, 14) y = random.randrange(3, 6) - print(f'{i}: {x},{y}: {slowfun(x, y)}') + print(f'{i}: {x},{y}: {slowfun(x, y)}') \ No newline at end of file From 080f864224195250364969a4fb57b79eb6e456a8 Mon Sep 17 00:00:00 2001 From: Andrew Haberman Date: Wed, 7 Oct 2020 13:58:46 -0400 Subject: [PATCH 5/5] expensive seq --- applications/expensive_seq/expensive_seq.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/applications/expensive_seq/expensive_seq.py b/applications/expensive_seq/expensive_seq.py index 5c82b8453..e673d0ce6 100644 --- a/applications/expensive_seq/expensive_seq.py +++ b/applications/expensive_seq/expensive_seq.py @@ -1,9 +1,15 @@ # Your code here - +cache = {} def expensive_seq(x, y, z): # Your code here - + pair = (x,y,z) + if pair not in cache: + if x <=0: + cache[pair] = y + z + if x > 0: + cache[pair] = expensive_seq(x-1,y+1,z) + expensive_seq(x-2,y+2,z*2) + expensive_seq(x-3,y+3,z*3) + return cache[pair] if __name__ == "__main__": @@ -11,4 +17,4 @@ def expensive_seq(x, y, z): x = expensive_seq(i*2, i*3, i*4) print(f"{i*2} {i*3} {i*4} = {x}") - print(expensive_seq(150, 400, 800)) + print(expensive_seq(150, 400, 800)) \ No newline at end of file