Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions applications/crack_caesar/crack_caesar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
11 changes: 8 additions & 3 deletions applications/expensive_seq/expensive_seq.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
44 changes: 44 additions & 0 deletions applications/histo/histo.py
Original file line number Diff line number Diff line change
@@ -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}")
10 changes: 10 additions & 0 deletions applications/lookup_table/lookup_table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Your code here
import math
import random

table = {}

def slowfun_too_slow(x, y):
v = math.pow(x, y)
Expand All @@ -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!
Expand Down
12 changes: 12 additions & 0 deletions applications/no_dups/no_dups.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down
67 changes: 58 additions & 9 deletions hashtable/hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -35,6 +37,7 @@ def get_num_slots(self):
Implement this.
"""
# Your code here
return len(self.data)


def get_load_factor(self):
Expand All @@ -44,7 +47,7 @@ def get_load_factor(self):
Implement this.
"""
# Your code here

return self.count / self.capacity

def fnv1(self, key):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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)
Expand Down