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
19 changes: 17 additions & 2 deletions applications/expensive_seq/expensive_seq.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
# Your code here


# exps(x, y, z) =
# if x <= 0: y + z
# if x > 0: exps(x-1,y+1,z) + exps(x-2,y+2,z*2) + exps(x-3,y+3,z*3)

nums = {}
def expensive_seq(x, y, z):
# Your code here
if (x, y, z) in nums.keys():
return nums[(x, y, z)]

if x <= 0:
return y + z

if x > 0:
value = expensive_seq(x-1, y+1, z) + expensive_seq(x-2,
y+2, z*2) + expensive_seq(x-3, y+3, z*3)

nums[(x, y, z)] = value
return value



Expand Down
3 changes: 2 additions & 1 deletion applications/histo/histo.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Your code here

with open("applications/histo/robin.txt") as f:
words = f.read()
21 changes: 21 additions & 0 deletions applications/lookup_table/lookup_table.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Your code here
import math
import random




def slowfun_too_slow(x, y):
Expand All @@ -9,12 +13,24 @@ def slowfun_too_slow(x, y):

return v

nums = {}

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
if (x, y) in nums.keys():
return nums[(x, y)]

v = math.pow(x, y)
v = math.factorial(v)
v //= (x + y)
v %= 982451653
nums[(x, y)] = v

return v



Expand All @@ -24,3 +40,8 @@ def slowfun(x, y):
x = random.randrange(2, 14)
y = random.randrange(3, 6)
print(f'{i}: {x},{y}: {slowfun(x, y)}')

# for i in range(50000):
# x = random.randrange(2, 14)
# y = random.randrange(3, 6)
# print(f'{i}: {x},{y}: {slowfun_too_slow(x, y)}')
63 changes: 61 additions & 2 deletions applications/markov/markov.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
import random

# Read in all the words in one go
with open("input.txt") as f:
with open("applications/markov/input.txt") as f:
words = f.read()

# TODO: analyze which words can follow other words
# Your code here
word_list = words.split()

markov = {}

last_word = None
for index in range(1, len(word_list)):
last_word = word_list[index - 1]
word = word_list[index]
if last_word not in markov.keys():
markov[last_word] = [word]
else:
markov[last_word].append(word)

# print(markov)
# TODO: construct 5 random sentences
# Your code here
select_word = word_list[random.randint(0, len(word_list)-1)]
sentence = select_word + " "
for word in range(20):
words = markov[select_word]
select_word = words[random.randint(0, len(words)-1)]
sentence += select_word + " "

print("")
print(sentence)

select_word = word_list[random.randint(0, len(word_list)-1)]
sentence = select_word + " "
for word in range(20):
words = markov[select_word]
select_word = words[random.randint(0, len(words)-1)]
sentence += select_word + " "

print("")
print(sentence)

select_word = word_list[random.randint(0, len(word_list)-1)]
sentence = select_word + " "
for word in range(20):
words = markov[select_word]
select_word = words[random.randint(0, len(words)-1)]
sentence += select_word + " "

print("")
print(sentence)

select_word = word_list[random.randint(0, len(word_list)-1)]
sentence = select_word + " "
for word in range(20):
words = markov[select_word]
select_word = words[random.randint(0, len(words)-1)]
sentence += select_word + " "

print("")
print(sentence)

select_word = word_list[random.randint(0, len(word_list)-1)]
sentence = select_word + " "
for word in range(20):
words = markov[select_word]
select_word = words[random.randint(0, len(words)-1)]
sentence += select_word + " "

print("")
print(sentence)
11 changes: 11 additions & 0 deletions applications/no_dups/no_dups.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
def no_dups(s):
# Your code here
duplicates = {}
sentence = ""

words = s.split()

for word in words:
if word not in duplicates.keys():
duplicates[word] = word
sentence += word + " "
sentence = sentence[:-1]
return sentence



Expand Down
37 changes: 36 additions & 1 deletion applications/word_count/word_count.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
def word_count(s):
# Your code here
# Your code here

words = s.replace(",", "")
words = words.replace(".", "")
words = words.replace("\"", "")
words = words.replace(":", "")
words = words.replace(";", "")
words = words.replace("-", "")
words = words.replace("+", "")
words = words.replace("=", "")
words = words.replace("/", "")
words = words.replace("\\", "")
words = words.replace("|", "")
words = words.replace("[", "")
words = words.replace("]", "")
words = words.replace("{", "")
words = words.replace("}", "")
words = words.replace("(", "")
words = words.replace(")", "")
words = words.replace("*", "")
words = words.replace("^", "")
words = words.replace("&", "")
words = words.lower()
words = words.split()


count = {}
for word in words:
# if not word.isalpha():
# continue
if word in count.keys():
count[word] += 1
else:
count[word] = 1


return count


if __name__ == "__main__":
Expand Down
Loading