Skip to content
29 changes: 28 additions & 1 deletion applications/crack_caesar/crack_caesar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Use frequency analysis to find the key to ciphertext.txt, and then
# decode it.

with open("ciphertext.txt") as f:
words = f.read()
# Your code here

encode_table = {
'A': 'H', 'B': 'Z', 'C': 'Y', 'D': 'W', 'E': 'O',
'F': 'R', 'G': 'J', 'H': 'D', 'I': 'P', 'J': 'T',
'K': 'I', 'L': 'G', 'M': 'L', 'N': 'C', 'O': 'E',
'P': 'X', 'Q': 'K', 'R': 'U', 'S': 'N', 'T': 'F',
'U': 'A', 'V': 'M', 'W': 'B', 'X': 'Q', 'Y': 'V',
'Z': 'S',
}

decode_table = {}

for key, value in encode_table.items():
decode_table[value] = key


def decode(old_string):
new_letter = ""
for i in old_string:
if i in decode_table:
new_letter = new_letter + decode_table[i]
else:
new_letter = new_letter + i
return new_letter


print(decode(words))
16 changes: 16 additions & 0 deletions applications/expensive_seq/expensive_seq.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Your code here
import codecs

print(codecs.encode("VaClguba,nqvpgxrlpnaornalvzzhgnoyrglcrvapyhqvatnghcyr", "rot13"))

cache = {}


def expensive_seq(x, y, z):
# Your code here
if (x, y, z) in cache:
return cache[(x, y, z)]
if x <= 0:
cache[(x, y, z)] = y + z
if x > 0:
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__":
Expand All @@ -12,3 +25,6 @@ def expensive_seq(x, y, z):
print(f"{i*2} {i*3} {i*4} = {x}")

print(expensive_seq(150, 400, 800))


# InPython,adictkeycanbeanyimmutabletypeincludingatuple
32 changes: 32 additions & 0 deletions applications/histo/histo.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
# Your code here
import codecs
print(codecs.encode(
"Items: .vgrzf() zrgubq ba n qvpgvbanel zvtug or hfrshy. vg'f cbffvoyr sbe .fbeg() gb fbeg ba zhygvcyr xrlf ng bapr. artngvirf zvtug uryc jurer erirefr jba'g. lbh pna cevag n inevnoyr svryq jvqgu va na s-fgevat jvgu arfgrq oenprf, yvxr fb", "rot13"))

# start here
with open("robin.txt") as f:
words = f.read()

# words = "Round the rugged rock the freak freak freak insanity! Don't forget to ruN to the stoRE, you freak!"


def word_count(s):
new_list = s.lower().split()
# forbidden characters
forbidden = '":;,.-+=/\|[]}{()?!*^&'
storage = {}
for word in new_list:
word = word.lower()
for letter in word:
if letter in forbidden:
word = word.replace(letter, "")
if word == "":
return {}
if word in storage:
storage[word] = storage[word] + "#"
if word not in storage:
storage[word] = "#"

for key, value in sorted(storage.items(), key=lambda x: -len(x[1])):
print(f'{key: <16}{value}')


word_count(words)
29 changes: 16 additions & 13 deletions applications/lookup_table/lookup_table.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
# Your code here
import random
import math

cache = {}

def slowfun_too_slow(x, y):
v = math.pow(x, y)
v = math.factorial(v)
v //= (x + y)
v %= 982451653

def slowfun_too_slow(x, y):
if (x, y) in cache:
return (x, y)
else:
v = math.pow(x, y)
v = math.factorial(v)
v //= (x + y)
v %= 982451653
cache[(x, y)] = v
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.
"""
pass
# Your code here



# Do not modify below this line!

# Do not modify below this line!
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_too_slow(x, y)}')
44 changes: 43 additions & 1 deletion applications/markov/markov.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,52 @@
with open("input.txt") as f:
words = f.read()

# words = "Cats and dogs and birds and fish dogs birds."

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

for index, word in enumerate(word_list):
if word in dictionary:
if index < len(word_list)-1:
dictionary[word].append(word_list[index+1])
else:
continue
else:
if index < len(word_list)-1:
dictionary[word] = [word_list[index+1]]


# print(dictionary)

# TODO: construct 5 random sentences
start_list = []
end_list = []
for word in word_list:
if word[len(word)-2] in '.?!' or word[-1] in '.?!':
end_list.append(word)
elif (word[0] == '"' and word[1].isupper()) or word[0].isupper():
start_list.append(word)


def createSentence():
start = random.choice(start_list)
print(start, end=" ")
for s in dictionary[start]:
print(s, end=" ")
new_word = random.choice(dictionary[s])
while new_word not in end_list:
print(new_word, end=" ")
new_word = random.choice(dictionary[new_word])
print(new_word, end=" ")
print("\n")


for i in range(5):
print(f"SENTENCE {i+1} \n")
createSentence()
# Your code here
# need to keep working on this.

# print(dictionary["King"])
12 changes: 10 additions & 2 deletions applications/no_dups/no_dups.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
def no_dups(s):
data = {}
# Your code here

s_array = s.split()
print(s_array)
for word in s_array:
if word in data:
continue
else:
data[word] = 1
return " ".join(data.keys())


if __name__ == "__main__":
print(no_dups(""))
print(no_dups("hello"))
print(no_dups("hello hello"))
print(no_dups("cats dogs fish cats dogs"))
print(no_dups("spam spam spam eggs spam sausage spam spam and spam"))
print(no_dups("spam spam spam eggs spam sausage spam spam and spam"))
19 changes: 17 additions & 2 deletions applications/word_count/word_count.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
def word_count(s):
# Your code here
new_list = s.lower().split()
# forbidden characters
forbidden = '":;,.-+=/\|[]}{()*^&'
storage = {}
for word in new_list:
for letter in word:
if letter in forbidden:
word = word.replace(letter, "")
if word == "":
return {}
if word in storage:
storage[word] += 1
if word not in storage:
storage[word] = 1

return storage


if __name__ == "__main__":
print(word_count(""))
print(word_count("Hello"))
print(word_count('Hello, my cat. And my cat doesn\'t say "hello" back.'))
print(word_count('This is a test of the emergency broadcast network. This is only a test.'))
print(word_count(
'This is a test of the emergency broadcast network. This is only a test.'))
Loading