Skip to content
Open

MVP #183

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
109 changes: 108 additions & 1 deletion applications/crack_caesar/crack_caesar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,112 @@
# Use frequency analysis to find the key to ciphertext.txt, and then
# decode it.
import math

# Your code here
master_frequency = {
"E": 11.53,
"T": 9.75,
"A": 8.46,
"O": 8.08,
"H": 7.71,
"N": 6.73,
"R": 6.29,
"I": 5.84,
"S": 5.56,
"D": 4.74,
"L": 3.92,
"W": 3.08,
"U": 2.59,
"G": 2.48,
"F": 2.42,
"B": 2.19,
"M": 2.18,
"Y": 2.02,
"C": 1.58,
"P": 1.08,
"K": 0.84,
"V": 0.59,
"Q": 0.17,
"J": 0.07,
"X": 0.07,
"Z": 0.03,
}

frequency = {
"count": 0,
"E": ["", 0],
"T": ["", 0],
"A": ["", 0],
"O": ["", 0],
"H": ["", 0],
"N": ["", 0],
"R": ["", 0],
"I": ["", 0],
"S": ["", 0],
"D": ["", 0],
"L": ["", 0],
"W": ["", 0],
"U": ["", 0],
"G": ["", 0],
"F": ["", 0],
"B": ["", 0],
"M": ["", 0],
"Y": ["", 0],
"C": ["", 0],
"P": ["", 0],
"K": ["", 0],
"V": ["", 0],
"Q": ["", 0],
"J": ["", 0],
"X": ["", 0],
"Z": ["", 0],
}

# ? Open the cipher and create file for decrypted text
theFile = open("ciphertext.txt", "r")
decrypted = open("decrypted.txt", "w")

# ? Count each letter in the cipher
for ch in theFile.read():
if ch in frequency:
frequency[ch][1] += 1
frequency["count"] += 1

# ? Calculate the frequency of each letter and check if its close to master frequency
for ch in frequency:
if ch != "count":
frequency[ch][1] = round((frequency[ch][1] / frequency["count"]) * 100, 2)

for mch in master_frequency:
if math.isclose(frequency[ch][1], master_frequency[mch]):
frequency[ch][0] = mch

# ? Reset the pointer of cipher file to beginning
theFile.seek(0)

# ? Write the decrypted file
for ch in theFile.read():
if ch in frequency:
decrypted.write(frequency[ch][0])
else:
decrypted.write(ch)

theFile.close()
decrypted.close()


# ? Import text
# ? make it a list

# ? Init a dict of count and a-z keys with vals of 0
# * for cd in range(ord('A'), ord('Z') + 1):
# * frequency[chr(cd)] = 0

# ? loop through text
# ? if letter, increase its count in dict by 1 and val of 'count' by one

# ? Then go through alphabet dict and divide letter count by length count and reassign it

# ? loop again and swap frequency value for corresponding letter

# ? Loop through text and swap each letter

8 changes: 7 additions & 1 deletion applications/expensive_seq/expensive_seq.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Your code here

stupid_numbers = {}

def expensive_seq(x, y, z):
# Your code here
# trip = (x, y, z)
print(x, y, z)
if x <= 0:
return y + z
if x > 0:
return expensive_seq(x-1,y+1,z) + expensive_seq(x-2,y+2,z*2) + expensive_seq(x-3,y+3,z*3)



Expand Down
42 changes: 42 additions & 0 deletions applications/histo/histo.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
# Your code here
histo = {}
longest = 0
ignore = ['"', ':', ';', ',', '.', '-', '+', '=', '/', '\\', '|', '[', ']', '{', '}', '(', ')', '*', '^', '&', '?', '!']

theFile = open("robin.txt", "r")

for line in theFile.readlines():
clean_line = "".join(ch for ch in line if ch not in ignore).lower()
for word in clean_line.split():
if word not in histo:
histo[word] = "#"
if len(word) > longest:
longest = len(word)
else:
histo[word] = histo[word] + "#"

sorted_histo = sorted(histo.items(), key=lambda x: x[0], reverse=False)
# sorted_histo_o = sorted(sorted_.items(), key=lambda x: x[0], reverse=False)


# for word in histo:
# just = longest + 2
# print(f"{word.ljust(just)} {histo[word]}")

print(sorted_histo)
# print(histo)


# def word_count(s):
# frequency = {}
# ignore = ['"', ':', ';', ',', '.', '-', '+', '=', '/', '\\', '|', '[', ']', '{', '}', '(', ')', '*', '^', '&']

# clean_string = "".join(ch for ch in s if ch not in ignore)
# for word in clean_string.lower().split():
# if word not in frequency:
# frequency[word] = 1
# else:
# frequency[word] += 1

# return frequency

theFile.close()

18 changes: 15 additions & 3 deletions applications/lookup_table/lookup_table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Your code here
import math
import random

fun = {}

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

duo = (x, y)

if duo in fun:
return fun[duo]
else:
v = math.pow(x, y)
v = math.factorial(v)
v //= (x + y)
v %= 982451653

fun[duo] = v
return v


# Do not modify below this line!
Expand Down
14 changes: 12 additions & 2 deletions applications/word_count/word_count.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
def word_count(s):
# Your code here

def word_count(s):
frequency = {}
ignore = ['"', ':', ';', ',', '.', '-', '+', '=', '/', '\\', '|', '[', ']', '{', '}', '(', ')', '*', '^', '&']

clean_string = "".join(ch for ch in s if ch not in ignore)
for word in clean_string.lower().split():
if word not in frequency:
frequency[word] = 1
else:
frequency[word] += 1

return frequency


if __name__ == "__main__":
Expand Down
Loading