diff --git a/applications/crack_caesar/crack_caesar.py b/applications/crack_caesar/crack_caesar.py index 1418f0ef3..edcf85a86 100644 --- a/applications/crack_caesar/crack_caesar.py +++ b/applications/crack_caesar/crack_caesar.py @@ -2,4 +2,43 @@ # decode it. # Your code here +with open('ciphertext.txt','r') as f_open: + data = f_open.read() +myKey = { + "H": "A", + "R": "F", + "I": "K", + "X": "P", + "A": "U", + "S": "Z", + "Z": "B", + "J": "G", + "G": "L", + "K": "Q", + "M": "V", + "Y": "C", + "D": "H", + "L": "M", + "U": "R", + "B": "W", + "W": "D", + "P": "I", + "C": "N", + "N": "S", + "Q": "X", + "O": "E", + "T": "J", + "E": "O", + "F": "T", + "V": "Y" +} + +def decoder(textFile): + for i in range(0, len(textFile)): + for each in myKey: + if each in textFile[i]: + textFile = textFile[i].replace(each, myKey[each]) + return textFile + +print(decoder(data)) \ No newline at end of file diff --git a/applications/expensive_seq/expensive_seq.py b/applications/expensive_seq/expensive_seq.py index 5c82b8453..83eff78cb 100644 --- a/applications/expensive_seq/expensive_seq.py +++ b/applications/expensive_seq/expensive_seq.py @@ -1,10 +1,15 @@ # Your code here +cache ={} def expensive_seq(x, y, z): - # Your code here - - + if (x, y, z) not in cache: + if x <= 0: + cache[(x, y, z)] = y + z + else: + 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) + return cache[(x, y, z)] if __name__ == "__main__": for i in range(10): diff --git a/applications/histo/histo.py b/applications/histo/histo.py index 6014a8e13..a939d31e9 100644 --- a/applications/histo/histo.py +++ b/applications/histo/histo.py @@ -1,2 +1,46 @@ # Your code here +with open("robin.txt") as f: + words = f.read() + +d = {} + + # split the string into list of words +for c in words: + if not c.isalpha() and not c.isspace(): + words = words.replace(c, '') + +wordList = words.split() +maxLength = 0 + + # populate historgram + + # loop through the words +for word in wordList: + word = word.lower() + + # keeping track of the longest word length + if len(word) > maxLength: + maxLength = len(word) + + if word not in d: + d[word] = 1 + else: + d[word] += 1 + + + # sort histogram + +sortedHist = sorted(d.items(), key=lambda kv: [-kv[1], kv[0]]) + + +# print histogram with desired formatting + + +for word in sortedHist: + + width = maxLength + 2 + + hashtags = word[1]*'#' + + print(f"{word[0]:{width}} {hashtags}") diff --git a/applications/lookup_table/lookup_table.py b/applications/lookup_table/lookup_table.py index 05b7d37fa..aabb67cb0 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 +table = {} def slowfun_too_slow(x, y): v = math.pow(x, y) @@ -15,7 +18,14 @@ def slowfun(x, y): output, but completes quickly instead of taking ages to run. """ # Your code here + v = math.pow(x, y) + if v not in table: + table[v] = math.factorial(v) + v = table[v] + v //= (x + y) + v %= 982451653 + return v # Do not modify below this line! diff --git a/applications/no_dups/no_dups.py b/applications/no_dups/no_dups.py index caa162c8c..fd6ac3307 100644 --- a/applications/no_dups/no_dups.py +++ b/applications/no_dups/no_dups.py @@ -1,6 +1,18 @@ def no_dups(s): # Your code here + d = {} + wordList = s.split() + for word in wordList: + if word in d: + continue + else: + d[word] = 1 + + arr = [key for key in d] + string = " " + + return string.join(arr) if __name__ == "__main__": diff --git a/hashtable/hashtable.py b/hashtable/hashtable.py index 0205f0ba9..b918400a1 100644 --- a/hashtable/hashtable.py +++ b/hashtable/hashtable.py @@ -20,9 +20,11 @@ class HashTable: Implement this. """ - def __init__(self, capacity): + def __init__(self, capacity=MIN_CAPACITY): # Your code here - + self.capacity = capacity + self.data = [None] * capacity + self.count = 0 def get_num_slots(self): """ @@ -35,6 +37,7 @@ def get_num_slots(self): Implement this. """ # Your code here + return len(self.data) def get_load_factor(self): @@ -44,7 +47,7 @@ def get_load_factor(self): Implement this. """ # Your code here - + return self.count / self.capacity def fnv1(self, key): """ @@ -63,7 +66,14 @@ def djb2(self, key): Implement this, and/or FNV-1. """ # Your code here - + fnv_prime = 1099511628211 + offset_basis = 14695981039346656037 + hash_value = offset_basis + key_utf8 = key.encode() + for byte in key_utf8: + hash_value = hash_value ^ byte + hash_value = hash_value * fnv_prime + return hash_value def hash_index(self, key): """ @@ -82,7 +92,17 @@ def put(self, key, value): Implement this. """ # Your code here - + index = self.hash_index(key) + hst = HashTableEntry(key, value) + node = self.data[index] + if node is not None: + self.data[index] = hst + self.data[index].next = node + else: + self.data[index] = hst + self.count += 1 + if self.get_load_factor() > 0.7: + self.resize(self.capacity * 2) def delete(self, key): """ @@ -93,7 +113,11 @@ def delete(self, key): Implement this. """ # Your code here - + if key is key: + self.put(key, None) + self.count -= 1 + else: + print("Key is not found.") def get(self, key): """ @@ -104,7 +128,14 @@ def get(self, key): Implement this. """ # Your code here - + index = self.hash_index(key) + node = self.data[index] + if node is not None: + while node: + if node.key == key: + return node.value + node = node.next + return node def resize(self, new_capacity): """ @@ -114,8 +145,26 @@ def resize(self, new_capacity): Implement this. """ # Your code here - - + # make a new hashTable passing in the new capacity + new_hashTable = HashTable(new_capacity) + # for each entry in the data of the table + for entry in self.data: + # check if entry and if there is + if entry: + # we update the new hashTable using put, passing in the key/value + new_hashTable.put(entry.key, entry.value) + # check if there is a next entry + if entry.next: + # if there is set the current var to be entry + current = entry + # while there is a next entry + while current.next: + # set current = current.next + current = current.next + # we use put to modify the new hashTable using key/value + new_hashTable.put(current.key, current.value) + self.data = new_hashTable.data + self.capacity = new_hashTable.capacity if __name__ == "__main__": ht = HashTable(8)