From 85af23db0e1c61c2dc69e942e8010ddbf04d82f6 Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Wed, 28 Oct 2020 21:34:54 -0600 Subject: [PATCH 01/12] Got stuff working. --- hashtable/hashtable.py | 80 +++++++++++++---------- hashtable/test_hashtable_no_collisions.py | 2 + 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0205f0ba9..57a905a99 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -2,6 +2,7 @@ class HashTableEntry: """ Linked List hash table key/value pair """ + def __init__(self, key, value): self.key = key self.value = value @@ -9,7 +10,7 @@ def __init__(self, key, value): # Hash table can't have fewer than this many slots -MIN_CAPACITY = 8 +# MIN_CAPACITY = 8 class HashTable: @@ -20,9 +21,9 @@ class HashTable: Implement this. """ - def __init__(self, capacity): - # Your code here - + def __init__(self, capacity=8): + self.buckets = [None] * capacity + self.capacity = capacity def get_num_slots(self): """ @@ -34,8 +35,7 @@ def get_num_slots(self): Implement this. """ - # Your code here - + return self.capacity def get_load_factor(self): """ @@ -45,16 +45,23 @@ def get_load_factor(self): """ # Your code here - def fnv1(self, key): """ FNV-1 Hash, 64-bit Implement this, and/or DJB2. """ + pass + # FNV_prime = 1099511628211 + # offset_basis = 14695981039346656037 - # Your code here - + # #FNV-1a Hash Function + # hash = offset_basis + seed + # what is seed here? + # for char in key: + # hash = hash * FNV_prime + # hash = hash ^ ord(char) + # return hash def djb2(self, key): """ @@ -62,15 +69,17 @@ def djb2(self, key): Implement this, and/or FNV-1. """ - # Your code here - + hash = 5381 + for c in key: + hash = (hash * 33) + ord(c) + return hash & 0xFFFFFFFF def hash_index(self, key): """ Take an arbitrary key and return a valid integer index between within the storage capacity of the hash table. """ - #return self.fnv1(key) % self.capacity + # return self.fnv1(key) % self.capacity return self.djb2(key) % self.capacity def put(self, key, value): @@ -81,8 +90,10 @@ def put(self, key, value): Implement this. """ - # Your code here - + index = self.hash_index(key) + if self.buckets[index] != None: + print("Something already exists at this index.") + self.buckets[index] = value def delete(self, key): """ @@ -92,8 +103,8 @@ def delete(self, key): Implement this. """ - # Your code here - + index = self.hash_index(key) + self.buckets[index] = None def get(self, key): """ @@ -103,18 +114,17 @@ def get(self, key): Implement this. """ - # Your code here - - - def resize(self, new_capacity): - """ - Changes the capacity of the hash table and - rehashes all key/value pairs. + index = self.hash_index(key) + return self.buckets[index] - Implement this. - """ - # Your code here + # def resize(self, new_capacity): + # """ + # Changes the capacity of the hash table and + # rehashes all key/value pairs. + # Implement this. + # """ + # # Your code here if __name__ == "__main__": @@ -139,15 +149,15 @@ def resize(self, new_capacity): for i in range(1, 13): print(ht.get(f"line_{i}")) - # Test resizing - old_capacity = ht.get_num_slots() - ht.resize(ht.capacity * 2) - new_capacity = ht.get_num_slots() + # # Test resizing + # old_capacity = ht.get_num_slots() + # ht.resize(ht.capacity * 2) + # new_capacity = ht.get_num_slots() - print(f"\nResized from {old_capacity} to {new_capacity}.\n") + # print(f"\nResized from {old_capacity} to {new_capacity}.\n") - # Test if data intact after resizing - for i in range(1, 13): - print(ht.get(f"line_{i}")) + # # Test if data intact after resizing + # for i in range(1, 13): + # print(ht.get(f"line_{i}")) - print("") + # print("") diff --git a/hashtable/test_hashtable_no_collisions.py b/hashtable/test_hashtable_no_collisions.py index a9b755b3d..034922718 100644 --- a/hashtable/test_hashtable_no_collisions.py +++ b/hashtable/test_hashtable_no_collisions.py @@ -8,6 +8,7 @@ import unittest from hashtable import HashTable + class TestHashTable(unittest.TestCase): def test_hash_table_insertion_and_retrieval(self): @@ -67,5 +68,6 @@ def test_hash_table_removes_correctly(self): return_value = ht.get("key-2") self.assertTrue(return_value is None) + if __name__ == '__main__': unittest.main() From f6810071dc8065014d6f75ad63aafc2eba82f93c Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Thu, 29 Oct 2020 07:11:30 -0600 Subject: [PATCH 02/12] adding a simple hash function. --- hashtable/hashtable.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 57a905a99..824e339d0 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -74,6 +74,20 @@ def djb2(self, key): hash = (hash * 33) + ord(c) return hash & 0xFFFFFFFF + def simp_hash_fn(self, key, value): + # key that gets hashed + byte = key.encode() + print('simp hash func', byte) + total = 0 + + for char in byte: + print(char) + total += char + total &= 0xffffffff + print("total 32 bit", total) + print(total % self.capacity) + return total % self.capacity # 8 + def hash_index(self, key): """ Take an arbitrary key and return a valid integer index @@ -144,6 +158,7 @@ def get(self, key): ht.put("line_12", "And stood awhile in thought.") print("") + ht.simp_hash_fn("yodd dyo ma", "black88") # Test storing beyond capacity for i in range(1, 13): From 4211032abea20b07c238a15512b7f3fa6682e69d Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Thu, 29 Oct 2020 22:36:14 -0600 Subject: [PATCH 03/12] Think I have everything but the resize --- hashtable/hashtable.py | 78 +++++++++++++++++++++++++++------------- hashtable/linked_list.py | 61 +++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 24 deletions(-) create mode 100644 hashtable/linked_list.py diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 824e339d0..41b6a5cc0 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -1,16 +1,4 @@ -class HashTableEntry: - """ - Linked List hash table key/value pair - """ - - def __init__(self, key, value): - self.key = key - self.value = value - self.next = None - - -# Hash table can't have fewer than this many slots -# MIN_CAPACITY = 8 +from linked_list import LinkedList, HashTableEntry class HashTable: @@ -105,9 +93,19 @@ def put(self, key, value): Implement this. """ index = self.hash_index(key) - if self.buckets[index] != None: - print("Something already exists at this index.") - self.buckets[index] = value + if self.buckets[index] == None: + self.buckets[index] = LinkedList() + self.buckets[index].add_to_head(key, value) + elif self.buckets[index].find(key): + self.buckets[index].delete(key) + self.buckets[index].add_to_head(key, value) + + else: + self.buckets[index].add_to_head(key, value) + # index = self.hash_index(key) + # if self.buckets[index] != None: + # print("Something already exists at this index.") + # self.buckets[index] = value def delete(self, key): """ @@ -118,7 +116,10 @@ def delete(self, key): Implement this. """ index = self.hash_index(key) - self.buckets[index] = None + if self.buckets[index] == None: + return None + else: + return self.buckets[index].delete(key) def get(self, key): """ @@ -129,16 +130,22 @@ def get(self, key): Implement this. """ index = self.hash_index(key) - return self.buckets[index] + if self.buckets[index]: + if self.buckets[index].find(key): + answer = self.buckets[index].find(key) + return answer + else: + return None # def resize(self, new_capacity): - # """ - # Changes the capacity of the hash table and - # rehashes all key/value pairs. - - # Implement this. - # """ # # Your code here + # # make a new array that is double the current size. + # # go through each linked list in the array + # # go through each item and rehash it + # # insert the items into their new locations + + # # def shrink + # # # same as resize just cut in half. if __name__ == "__main__": @@ -153,6 +160,7 @@ def get(self, key): ht.put("line_7", "Beware the Jubjub bird, and shun") ht.put("line_8", 'The frumious Bandersnatch!"') ht.put("line_9", "He took his vorpal sword in hand;") + ht.put("line_9", "test;") ht.put("line_10", "Long time the manxome foe he sought--") ht.put("line_11", "So rested he by the Tumtum tree") ht.put("line_12", "And stood awhile in thought.") @@ -176,3 +184,25 @@ def get(self, key): # print(ht.get(f"line_{i}")) # print("") + + # def putLL(self, key, value): + # # find start of the linked list using index + # index = self.hash_index(key) + # # insert into this linked list a new HashtableEntry + # # insert into the head. + # # if key already exists, + # # replace the value with the new value + # # else add new value to head. + # self.buckets[index] = value + + # def get(self, key): + # # get index + # # get the linked list at the computed index + # # search through for key + # # if it exists, return the key + # # else, return None + + # def delete(key): + # # search through linked list for key + # # delete that node + # # return value of deleted node diff --git a/hashtable/linked_list.py b/hashtable/linked_list.py new file mode 100644 index 000000000..a2e204c4c --- /dev/null +++ b/hashtable/linked_list.py @@ -0,0 +1,61 @@ + +class HashTableEntry: + """ + Linked List hash table key/value pair + """ + + def __init__(self, key, value): + self.key = key + self.value = value + self.next = None + + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def add_to_head(self, key, value): + new_node = HashTableEntry(key, value) + if self.head == None: + self.head = new_node + self.tail = new_node + self.next = None + else: + oldhead = self.head + self.head = new_node + self.head.next = oldhead + + def find(self, key): + current = self.head + while current != None: + if current.key == key: + return current.value + else: + current = current.next + return None + + def delete(self, key): + current = self.head + + if current.key == key: + self.head = current.next + return current.value + + prev = current + current = current.next + + while current != None: + if current.key == key: + prev.next = current.next + return current.value + else: + prev = current + current = current.next + return None + + +dude = LinkedList() +dude.add_to_head("hello", "mate") +dude.delete("hello") +print(dude.find("hello")) From 766041c20741a47811a5ab23af1fd6fa4d07a8ed Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Thu, 29 Oct 2020 22:39:54 -0600 Subject: [PATCH 04/12] have everything working but the resize --- hashtable/hashtable.py | 2 +- hashtable/linked_list.py | 1 + hashtable/test_hashtable.py | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 41b6a5cc0..0ac9ee093 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -6,7 +6,7 @@ class HashTable: A hash table that with `capacity` buckets that accepts string keys - Implement this. + Implement this.dfs """ def __init__(self, capacity=8): diff --git a/hashtable/linked_list.py b/hashtable/linked_list.py index a2e204c4c..8cb846714 100644 --- a/hashtable/linked_list.py +++ b/hashtable/linked_list.py @@ -59,3 +59,4 @@ def delete(self, key): dude.add_to_head("hello", "mate") dude.delete("hello") print(dude.find("hello")) +# this should work diff --git a/hashtable/test_hashtable.py b/hashtable/test_hashtable.py index 12ae9e388..d450ac022 100644 --- a/hashtable/test_hashtable.py +++ b/hashtable/test_hashtable.py @@ -7,7 +7,6 @@ class TestHashTable(unittest.TestCase): def test_hash_table_insertion_and_retrieval(self): ht = HashTable(8) - ht.put("key-0", "val-0") ht.put("key-1", "val-1") ht.put("key-2", "val-2") @@ -159,5 +158,6 @@ def test_hash_table_removes_correctly(self): return_value = ht.get("key-9") self.assertTrue(return_value is None) + if __name__ == '__main__': unittest.main() From ca0cf06a37166257f57e861361beaf29d9a9a5c6 Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Fri, 30 Oct 2020 20:34:20 -0600 Subject: [PATCH 05/12] I got resize to work. But it is not automatic yet. Working on that part. --- hashtable/hashtable.py | 94 +++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0ac9ee093..89427116e 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -12,6 +12,8 @@ class HashTable: def __init__(self, capacity=8): self.buckets = [None] * capacity self.capacity = capacity + self.used = 0 + self.load = self.get_load_factor() def get_num_slots(self): """ @@ -23,7 +25,7 @@ def get_num_slots(self): Implement this. """ - return self.capacity + return len(self.buckets) def get_load_factor(self): """ @@ -31,7 +33,8 @@ def get_load_factor(self): Implement this. """ - # Your code here + load = self.used/self.capacity + return load def fnv1(self, key): """ @@ -69,10 +72,10 @@ def simp_hash_fn(self, key, value): total = 0 for char in byte: - print(char) + # print(char) total += char total &= 0xffffffff - print("total 32 bit", total) + # print("total 32 bit", total) print(total % self.capacity) return total % self.capacity # 8 @@ -96,12 +99,13 @@ def put(self, key, value): if self.buckets[index] == None: self.buckets[index] = LinkedList() self.buckets[index].add_to_head(key, value) + elif self.buckets[index].find(key): self.buckets[index].delete(key) self.buckets[index].add_to_head(key, value) - else: self.buckets[index].add_to_head(key, value) + self.used += 1 # index = self.hash_index(key) # if self.buckets[index] != None: # print("Something already exists at this index.") @@ -119,6 +123,7 @@ def delete(self, key): if self.buckets[index] == None: return None else: + self.used -= 1 return self.buckets[index].delete(key) def get(self, key): @@ -137,15 +142,29 @@ def get(self, key): else: return None - # def resize(self, new_capacity): - # # Your code here - # # make a new array that is double the current size. - # # go through each linked list in the array - # # go through each item and rehash it - # # insert the items into their new locations + def resize(self, new_capacity): + # Your code here + # make a new array that is double the current size. + oldArr = self.buckets + self.buckets = [None] * new_capacity + self.capacity = new_capacity + print(len(self.buckets)) + for i in oldArr: + if i != None: + current = i.head + while current != None: + self.put(current.key, current.value) + current = current.next + else: + continue + + # go through each linked list in the array + # go through each item and rehash it + # insert the items into their new locations - # # def shrink - # # # same as resize just cut in half. + def print_out_hashtable(self): + for i in self.buckets: + print(i) if __name__ == "__main__": @@ -160,49 +179,30 @@ def get(self, key): ht.put("line_7", "Beware the Jubjub bird, and shun") ht.put("line_8", 'The frumious Bandersnatch!"') ht.put("line_9", "He took his vorpal sword in hand;") - ht.put("line_9", "test;") ht.put("line_10", "Long time the manxome foe he sought--") ht.put("line_11", "So rested he by the Tumtum tree") ht.put("line_12", "And stood awhile in thought.") print("") - ht.simp_hash_fn("yodd dyo ma", "black88") + # ht.simp_hash_fn("yodd dyo ma", "black88") # Test storing beyond capacity - for i in range(1, 13): - print(ht.get(f"line_{i}")) + # for i in range(1, 13): + # print(ht.get(f"line_{i}")) + + # print(ht.resize(ht.capacity * 2)) # # Test resizing - # old_capacity = ht.get_num_slots() - # ht.resize(ht.capacity * 2) - # new_capacity = ht.get_num_slots() + old_capacity = ht.get_num_slots() + ht.resize(ht.capacity*2) + new_capacity = ht.get_num_slots() - # print(f"\nResized from {old_capacity} to {new_capacity}.\n") + print(f"\nResized from {old_capacity} to {new_capacity}.\n") - # # Test if data intact after resizing - # for i in range(1, 13): - # print(ht.get(f"line_{i}")) + # Test if data intact after resizing + for i in range(1, 13): + print(ht.get(f"line_{i}")) - # print("") - - # def putLL(self, key, value): - # # find start of the linked list using index - # index = self.hash_index(key) - # # insert into this linked list a new HashtableEntry - # # insert into the head. - # # if key already exists, - # # replace the value with the new value - # # else add new value to head. - # self.buckets[index] = value - - # def get(self, key): - # # get index - # # get the linked list at the computed index - # # search through for key - # # if it exists, return the key - # # else, return None - - # def delete(key): - # # search through linked list for key - # # delete that node - # # return value of deleted node + print("") + print(ht.get_load_factor()) + ht.print_out_hashtable() From e6f87884c6cccc3e7a3580e55262ecc601bb3114 Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Sun, 1 Nov 2020 22:18:41 -0700 Subject: [PATCH 06/12] writing some notes for class --- hashtable/hashtable.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 89427116e..bf0b2681e 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -41,17 +41,22 @@ def fnv1(self, key): FNV-1 Hash, 64-bit Implement this, and/or DJB2. + - start hash at some large number(fnv offset_basis) + - the hash variable maintains our total. + - xor (looking at number in binary form) + looks at every pair of bits and treats 1 as true and 0 as false. """ pass # FNV_prime = 1099511628211 # offset_basis = 14695981039346656037 # #FNV-1a Hash Function - # hash = offset_basis + seed + # hash = offset_basis + #bytes_t0_hash = key.encode() # what is seed here? - # for char in key: + # for bytes in bytes_to_hash: # hash = hash * FNV_prime - # hash = hash ^ ord(char) + # hash = hash ^ byte # return hash def djb2(self, key): From cf4549191cf61751a3fe2ec9a37cc73925a542df Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Sun, 1 Nov 2020 22:24:23 -0700 Subject: [PATCH 07/12] making a few changes based on feedback from lecture. --- hashtable/hashtable.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index bf0b2681e..a7bfa6fd1 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -66,8 +66,9 @@ def djb2(self, key): Implement this, and/or FNV-1. """ hash = 5381 - for c in key: - hash = (hash * 33) + ord(c) + bytes_to_hash = key.encode() + for byte in bytes_to_hash: + hash = (hash * 33) + byte return hash & 0xFFFFFFFF def simp_hash_fn(self, key, value): From fadce39d85df190c4262c9f271cd90ae19c3b460 Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Thu, 5 Nov 2020 22:12:47 -0700 Subject: [PATCH 08/12] I added two, but still working. --- applications/crack_caesar/crack_caesar.py | 24 ++++++++++++++++- applications/expensive_seq/expensive_seq.py | 19 +++++++++----- applications/lookup_table/lookup_table.py | 29 ++++++++++++--------- applications/word_count/word_count.py | 19 ++++++++++++-- hashtable/linked_list.py | 4 +-- 5 files changed, 70 insertions(+), 25 deletions(-) diff --git a/applications/crack_caesar/crack_caesar.py b/applications/crack_caesar/crack_caesar.py index 1418f0ef3..3aa9ddab5 100644 --- a/applications/crack_caesar/crack_caesar.py +++ b/applications/crack_caesar/crack_caesar.py @@ -1,5 +1,27 @@ # Use frequency analysis to find the key to ciphertext.txt, and then # decode it. - +with open("ciphertext.txt") as f: + words = f.read() # Your code here +encode_table = { + 'A': 'H', 'B': 'Z', 'C': 'Y', 'D': 'W', 'E': 'O', + 'F': 'R', 'G': 'J', 'H': 'D', 'I': 'P', 'J': 'T', + 'K': 'I', 'L': 'G', 'M': 'L', 'N': 'C', 'O': 'E', + 'P': 'X', 'Q': 'K', 'R': 'U', 'S': 'N', 'T': 'F', + 'U': 'A', 'V': 'M', 'W': 'B', 'X': 'Q', 'Y': 'V', + 'Z': 'S', +} + + +def encode(old_string): + new_letter = "" + for i in old_string: + if i in encode_table: + new_letter = new_letter + encode_table[i] + else: + return i + return new_letter + + +print(encode(words)) diff --git a/applications/expensive_seq/expensive_seq.py b/applications/expensive_seq/expensive_seq.py index 5c82b8453..06b3e1123 100644 --- a/applications/expensive_seq/expensive_seq.py +++ b/applications/expensive_seq/expensive_seq.py @@ -1,14 +1,19 @@ # Your code here +import codecs +print(codecs.encode("VaClguba,nqvpgxrlpnaornalvzzhgnoyrglcrvapyhqvatnghcyr", "rot13")) -def expensive_seq(x, y, z): - # Your code here +# def expensive_seq(x, y, z): +# # Your code here -if __name__ == "__main__": - for i in range(10): - x = expensive_seq(i*2, i*3, i*4) - print(f"{i*2} {i*3} {i*4} = {x}") +# if __name__ == "__main__": +# for i in range(10): +# 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)) + + +# InPython,adictkeycanbeanyimmutabletypeincludingatuple diff --git a/applications/lookup_table/lookup_table.py b/applications/lookup_table/lookup_table.py index 05b7d37fa..538e74bd5 100644 --- a/applications/lookup_table/lookup_table.py +++ b/applications/lookup_table/lookup_table.py @@ -1,26 +1,29 @@ # Your code here +import random +import math +cache = {} -def slowfun_too_slow(x, y): - v = math.pow(x, y) - v = math.factorial(v) - v //= (x + y) - v %= 982451653 +def slowfun_too_slow(x, y): + if (x, y) in cache: + return (x, y) + else: + v = math.pow(x, y) + v = math.factorial(v) + v //= (x + y) + v %= 982451653 + cache[(x, y)] = v 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. - """ + pass # Your code here - -# Do not modify below this line! - + # Do not modify below this line! 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_too_slow(x, y)}') diff --git a/applications/word_count/word_count.py b/applications/word_count/word_count.py index a20546425..6628ed64d 100644 --- a/applications/word_count/word_count.py +++ b/applications/word_count/word_count.py @@ -1,10 +1,25 @@ def word_count(s): - # Your code here + new_list = s.lower().split() + # forbidden characters + forbidden = '":;,.-+=/\|[]}{()*^&' + storage = {} + for word in new_list: + for letter in word: + if letter in forbidden: + word = word.replace(letter, "") + if word == "": + return {} + if word in storage: + storage[word] += 1 + if word not in storage: + storage[word] = 1 + return storage if __name__ == "__main__": print(word_count("")) print(word_count("Hello")) print(word_count('Hello, my cat. And my cat doesn\'t say "hello" back.')) - print(word_count('This is a test of the emergency broadcast network. This is only a test.')) \ No newline at end of file + print(word_count( + 'This is a test of the emergency broadcast network. This is only a test.')) diff --git a/hashtable/linked_list.py b/hashtable/linked_list.py index 8cb846714..81016c13a 100644 --- a/hashtable/linked_list.py +++ b/hashtable/linked_list.py @@ -13,13 +13,13 @@ def __init__(self, key, value): class LinkedList: def __init__(self): self.head = None - self.tail = None + # self.tail = None def add_to_head(self, key, value): new_node = HashTableEntry(key, value) if self.head == None: self.head = new_node - self.tail = new_node + # self.tail = new_node self.next = None else: oldhead = self.head From fb908e7e45f489205e43e9526b95ae4dbb03819f Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Thu, 5 Nov 2020 22:20:05 -0700 Subject: [PATCH 09/12] Added a note --- applications/markov/markov.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/markov/markov.py b/applications/markov/markov.py index 1d138db10..ac4425e5a 100644 --- a/applications/markov/markov.py +++ b/applications/markov/markov.py @@ -10,4 +10,4 @@ # TODO: construct 5 random sentences # Your code here - +# need to keep working on this. From 563986063a11a6e48ce022b9ce60e228e2e2961f Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Sat, 7 Nov 2020 19:30:09 -0700 Subject: [PATCH 10/12] I got nodups, markov, and expensive seq done. --- applications/expensive_seq/expensive_seq.py | 25 ++++++++---- applications/markov/markov.py | 44 ++++++++++++++++++++- applications/no_dups/no_dups.py | 12 +++++- 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/applications/expensive_seq/expensive_seq.py b/applications/expensive_seq/expensive_seq.py index 06b3e1123..bfdfd3ca8 100644 --- a/applications/expensive_seq/expensive_seq.py +++ b/applications/expensive_seq/expensive_seq.py @@ -3,17 +3,28 @@ print(codecs.encode("VaClguba,nqvpgxrlpnaornalvzzhgnoyrglcrvapyhqvatnghcyr", "rot13")) +cache = {} -# def expensive_seq(x, y, z): -# # Your code here +def expensive_seq(x, y, z): + # Your code here + if (x, y, z) in cache: + return cache[(x, y, z)] + if x <= 0: + cache[(x, y, z)] = y + z + if x > 0: + cache[(x, y, z)] = expensive_seq(x-1, y+1, z) + \ + expensive_seq(x-2, y+2, z*2) + expensive_seq(x-3, y+3, z*3) -# if __name__ == "__main__": -# for i in range(10): -# x = expensive_seq(i*2, i*3, i*4) -# print(f"{i*2} {i*3} {i*4} = {x}") + return cache[(x, y, z)] -# print(expensive_seq(150, 400, 800)) + +if __name__ == "__main__": + for i in range(10): + x = expensive_seq(i*2, i*3, i*4) + print(f"{i*2} {i*3} {i*4} = {x}") + + print(expensive_seq(150, 400, 800)) # InPython,adictkeycanbeanyimmutabletypeincludingatuple diff --git a/applications/markov/markov.py b/applications/markov/markov.py index ac4425e5a..d93dc0b86 100644 --- a/applications/markov/markov.py +++ b/applications/markov/markov.py @@ -4,10 +4,52 @@ with open("input.txt") as f: words = f.read() +# words = "Cats and dogs and birds and fish dogs birds." + # TODO: analyze which words can follow other words -# Your code here +dictionary = {} +word_list = words.split() + +for index, word in enumerate(word_list): + if word in dictionary: + if index < len(word_list)-1: + dictionary[word].append(word_list[index+1]) + else: + continue + else: + if index < len(word_list)-1: + dictionary[word] = [word_list[index+1]] +# print(dictionary) + # TODO: construct 5 random sentences +start_list = [] +end_list = [] +for word in word_list: + if word[len(word)-2] in '.?!' or word[-1] in '.?!': + end_list.append(word) + elif (word[0] == '"' and word[1].isupper()) or word[0].isupper(): + start_list.append(word) + + +def createSentence(): + start = random.choice(start_list) + print(start, end=" ") + for s in dictionary[start]: + print(s, end=" ") + new_word = random.choice(dictionary[s]) + while new_word not in end_list: + print(new_word, end=" ") + new_word = random.choice(dictionary[new_word]) + print(new_word, end=" ") + print("\n") + + +for i in range(5): + print(f"SENTENCE {i+1} \n") + createSentence() # Your code here # need to keep working on this. + +# print(dictionary["King"]) diff --git a/applications/no_dups/no_dups.py b/applications/no_dups/no_dups.py index caa162c8c..2ea993fac 100644 --- a/applications/no_dups/no_dups.py +++ b/applications/no_dups/no_dups.py @@ -1,6 +1,14 @@ def no_dups(s): + data = {} # Your code here - + s_array = s.split() + print(s_array) + for word in s_array: + if word in data: + continue + else: + data[word] = 1 + return " ".join(data.keys()) if __name__ == "__main__": @@ -8,4 +16,4 @@ def no_dups(s): print(no_dups("hello")) print(no_dups("hello hello")) print(no_dups("cats dogs fish cats dogs")) - print(no_dups("spam spam spam eggs spam sausage spam spam and spam")) \ No newline at end of file + print(no_dups("spam spam spam eggs spam sausage spam spam and spam")) From 93affe7b765877524d539569a8b0d1146987633d Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Sat, 7 Nov 2020 20:58:02 -0700 Subject: [PATCH 11/12] Managed to get histo problem. --- applications/histo/histo.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/applications/histo/histo.py b/applications/histo/histo.py index 6014a8e13..20b975b00 100644 --- a/applications/histo/histo.py +++ b/applications/histo/histo.py @@ -1,2 +1,34 @@ # Your code here +import codecs +print(codecs.encode( + "Items: .vgrzf() zrgubq ba n qvpgvbanel zvtug or hfrshy. vg'f cbffvoyr sbe .fbeg() gb fbeg ba zhygvcyr xrlf ng bapr. artngvirf zvtug uryc jurer erirefr jba'g. lbh pna cevag n inevnoyr svryq jvqgu va na s-fgevat jvgu arfgrq oenprf, yvxr fb", "rot13")) +# start here +with open("robin.txt") as f: + words = f.read() + +# words = "Round the rugged rock the freak freak freak insanity! Don't forget to ruN to the stoRE, you freak!" + + +def word_count(s): + new_list = s.lower().split() + # forbidden characters + forbidden = '":;,.-+=/\|[]}{()?!*^&' + storage = {} + for word in new_list: + word = word.lower() + for letter in word: + if letter in forbidden: + word = word.replace(letter, "") + if word == "": + return {} + if word in storage: + storage[word] = storage[word] + "#" + if word not in storage: + storage[word] = "#" + + for key, value in sorted(storage.items(), key=lambda x: -len(x[1])): + print(f'{key: <16}{value}') + + +word_count(words) From 19a0895cdbb6290eda66b9b7d2c0f8d83db50df5 Mon Sep 17 00:00:00 2001 From: bsherwood9 <36565536+bsherwood9@users.noreply.github.com> Date: Sun, 8 Nov 2020 14:07:10 -0700 Subject: [PATCH 12/12] tried getting the crack caesar. Intelligle still. --- applications/crack_caesar/crack_caesar.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/applications/crack_caesar/crack_caesar.py b/applications/crack_caesar/crack_caesar.py index 3aa9ddab5..2e67dca96 100644 --- a/applications/crack_caesar/crack_caesar.py +++ b/applications/crack_caesar/crack_caesar.py @@ -13,15 +13,20 @@ 'Z': 'S', } +decode_table = {} -def encode(old_string): +for key, value in encode_table.items(): + decode_table[value] = key + + +def decode(old_string): new_letter = "" for i in old_string: - if i in encode_table: - new_letter = new_letter + encode_table[i] + if i in decode_table: + new_letter = new_letter + decode_table[i] else: - return i + new_letter = new_letter + i return new_letter -print(encode(words)) +print(decode(words))