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
12 changes: 9 additions & 3 deletions applications/expensive_seq/expensive_seq.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# 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__":
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))
12 changes: 11 additions & 1 deletion 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
cache = {}

def slowfun_too_slow(x, y):
v = math.pow(x, y)
Expand All @@ -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]



Expand All @@ -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)}')
7 changes: 7 additions & 0 deletions applications/no_dups/no_dups.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down
18 changes: 18 additions & 0 deletions applications/word_count/word_count.py
Original file line number Diff line number Diff line change
@@ -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





Expand Down
83 changes: 79 additions & 4 deletions hashtable/hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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):
Expand All @@ -35,7 +41,7 @@ def get_num_slots(self):
Implement this.
"""
# Your code here

return self.capacity

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

return self.count / len(self.capacity)

def fnv1(self, key):
"""
Expand All @@ -54,6 +60,7 @@ def fnv1(self, key):
"""

# Your code here



def djb2(self, key):
Expand All @@ -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):
"""
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
"""
Expand All @@ -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__":
Expand Down Expand Up @@ -150,4 +225,4 @@ def resize(self, new_capacity):
for i in range(1, 13):
print(ht.get(f"line_{i}"))

print("")
print("")