diff --git a/applications/crack_caesar/crack_caesar.py b/applications/crack_caesar/crack_caesar.py index 1418f0ef3..d6119cbe3 100644 --- a/applications/crack_caesar/crack_caesar.py +++ b/applications/crack_caesar/crack_caesar.py @@ -3,3 +3,39 @@ # Your code here +from collections import OrderedDict +# Your code here +def readText(): + letterDict = {} + cipherArray = ['E', 'T', 'A', 'O', 'H', 'N', 'R', 'I', 'S', 'D', 'L', 'W', 'U', 'G', 'F', 'B', 'M', 'Y', 'C', 'P', 'K', 'V', 'Q', 'J', 'X', 'Z'] + decodedDict = {} + + f = open("applications/crack_caesar/ciphertext.txt", "r") + contents = f.read() + + for x in contents: + if x in letterDict: + letterDict[x] += 1 + else: + if x.isalpha(): + letterDict[x] = 1 + + sorted_letterDict = sorted(letterDict.items(), key=lambda x: (x[1], x[0]), reverse=True) + + index = 0 + for x in sorted_letterDict: + for letter in x[-2]: + decodedDict[letter] = cipherArray[index] + index += 1 + print(decodedDict) + + decodedText = "" + for x in contents: + if x in decodedDict: + decodedText += decodedDict[x] + else: + decodedText += x + return print(decodedText) + +readText() + diff --git a/applications/expensive_seq/expensive_seq.py b/applications/expensive_seq/expensive_seq.py index 5c82b8453..57f1763ae 100644 --- a/applications/expensive_seq/expensive_seq.py +++ b/applications/expensive_seq/expensive_seq.py @@ -1,10 +1,19 @@ # Your code here +dict = {} def expensive_seq(x, y, z): - # Your code here + if (x, y, z) in dict: + return dict[(x, y, z)] + result = 0 + if x <=0: + result = y + z + else: + result = expensive_seq(x-1,y+1,z) + expensive_seq(x-2,y+2,z*2) + expensive_seq(x-3,y+3,z*3) + dict[(x, y, z)] = result + return result if __name__ == "__main__": for i in range(10): diff --git a/applications/no_dups/no_dups.py b/applications/no_dups/no_dups.py index caa162c8c..99d641ccc 100644 --- a/applications/no_dups/no_dups.py +++ b/applications/no_dups/no_dups.py @@ -1,6 +1,15 @@ def no_dups(s): - # Your code here + words_already_seen = set() + result = '' + words = s.split() + for word in words: + if word not in words_already_seen: + result += word + ' ' + words_already_seen.add(word) + if result.endswith(' '): + result = result[:-1] + return result if __name__ == "__main__": diff --git a/applications/word_count/word_count.py b/applications/word_count/word_count.py index a20546425..f7cb707e7 100644 --- a/applications/word_count/word_count.py +++ b/applications/word_count/word_count.py @@ -1,6 +1,14 @@ def word_count(s): - # Your code here + dict = {} + special_chars = ['"', ':', ';', ',', '.', '-', '+', '=', '/', '\\', '|', '[', ']', '{', '}', '(', ')', '*', '^', '&'] + s2 = ''.join(c.lower() for c in s if not c in special_chars) + + + for word in s2.split(): + dict[word] = dict[word] + 1 if word in dict else 1 + + return dict if __name__ == "__main__": diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0205f0ba9..1b59df679 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -11,6 +11,8 @@ def __init__(self, key, value): # Hash table can't have fewer than this many slots MIN_CAPACITY = 8 +MAX_LOAD_FACTOR = 0.7 +MIN_LOAD_FACTOR = 0.2 class HashTable: """ @@ -20,9 +22,10 @@ class HashTable: Implement this. """ - def __init__(self, capacity): - # Your code here - + def __init__(self, capacity=MIN_CAPACITY): + self.capacity = capacity + self.array = [None] * capacity + self.number_of_items = 0 def get_num_slots(self): """ @@ -34,7 +37,7 @@ def get_num_slots(self): Implement this. """ - # Your code here + return self.capacity def get_load_factor(self): @@ -43,7 +46,7 @@ def get_load_factor(self): Implement this. """ - # Your code here + return self.number_of_items / self.capacity def fnv1(self, key): @@ -53,7 +56,12 @@ def fnv1(self, key): Implement this, and/or DJB2. """ - # Your code here + hash = 14638081039346656478 # Offset + + for s in key: + hash = hash * 1099511628211 # FVN prime + hash = hash ^ ord(s) + return hash % len(self.array) def djb2(self, key): @@ -62,7 +70,11 @@ def djb2(self, key): Implement this, and/or FNV-1. """ - # Your code here + hash = 5381 + + for x in key: + hash = ((hash << 5) + hash) + ord(x) + return hash & 0xffffffff % self.capacity def hash_index(self, key): @@ -81,7 +93,29 @@ def put(self, key, value): Implement this. """ - # Your code here + index = self.hash_index(key) + + # if self.array[index] is not None: + # print(f"Collision Warning") + # self.array[index] = value + + entry = self.array[index] + + if entry is None: + self.array[index] = HashTableEntry(key, value) + self.number_of_items += 1 + self.resizeIfNeeded() + return + + while entry.next != None and entry.key !=key: + entry = entry.next + + if entry.key == key: + entry.value = value + else: + entry.next = HashTableEntry(key, value) + self.number_of_items += 1 + self.resizeIfNeeded() def delete(self, key): @@ -92,19 +126,58 @@ def delete(self, key): Implement this. """ - # Your code here + index = self.hash_index(key) + + # if index is None: + # print(f"Warning: No value exists within this table for key: '{key}'") + # self.array[index] = None + + entry = self.array[index] + prev_entry = None + + if entry is not None: + while entry.next != None and entry.key != key: + prev_entry = entry + entry = entry.next + + if entry.key == key: + if prev_entry is None: + self.array[index] = entry.next + else: + prev_entry.next = entry.next + self.number_of_items -= 1 + self.resizeIfNeeded() + return + + print(f"Warning: Attempted to delete value from HashTable but no value exists for key '{key}'") def get(self, key): - """ - Retrieve the value stored with the given key. + - Returns None if the key is not found. + index = self.hash_index(key) - Implement this. - """ - # Your code here + # if index is None: + # return None + + # return self.array[index] + + entry = self.array[index] + + if entry is None: + return None + + while entry.next != None and entry.key != key: + entry = entry.next + return entry.value if entry.key == key else None + + + def resizeIfNeeded(self): + if self.get_load_factor () > MAX_LOAD_FACTOR: + self.resize(self.capacity * 2) + elif self.get_load_factor() < MIN_LOAD_FACTOR and int(self.capacity / 2) >= MIN_CAPACITY: + self.resize(int(self.capacity / 2)) def resize(self, new_capacity): """ @@ -113,8 +186,29 @@ def resize(self, new_capacity): Implement this. """ - # Your code here + old_array = self.array + self.array = [None] * new_capacity + self.capacity = new_capacity + + for old_entry in old_array: + while old_entry is not None: + key = old_entry.key + value = old_entry.value + index = self.hash_index(key) + entry = self.array[index] + + # inserts old key/value into resized hash table + + if entry is None: + self.array[index] = HashTableEntry(key, value) + else: + while entry.next != None: + entry = entry.next + entry.next = HashTableEntry(key, value) + + old_entry = old_entry.next + if __name__ == "__main__":