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
41 changes: 38 additions & 3 deletions applications/crack_caesar/crack_caesar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
# Use frequency analysis to find the key to ciphertext.txt, and then
# decode it.
import os

# Your code here
input_text = os.path.join(os.path.dirname(__file__), 'cyphertext.txt')

# Read in all the words in one go
with open(input_text) as f:
words = f.read()

lowercase_words = words.lower()
encrypted_dict = {}

# Get frequency of characters in encryption
for char in lowercase_words:
if char.isalpha() != True:
continue
if char in encrypted_dict:
encrypted_dict[char] += 1
else:
encrypted_dict[char] = 1

# Sort characters by frequency
sorted_encrypted_dict = {k: v for k, v in sorted(encrypted_dict.items(), key=lambda item: item[1], reverse=True)}

# Use sorted encryption dictionary to replace characters
encryption_key = ['e', 't', 'a', 'o', 'h', 'n', 'r', 'i', 's', 'd', 'l', 'w', 'u', 'g', 'f', 'b', 'm', 'y', 'c', 'p', 'k', 'v', 'q', 'j', 'x', 'z']
transfer_dict = {}
result = ""

# Create a tranfer Dictionary
for index, key in enumerate(sorted_encrypted_dict):
transfer_dict[key] = encryption_key[index]

# Use Transfer dictionary to replace characters in CypherText.txt
for char in lowercase_words:
if char.isalpha() != True:
result += char
if char in transfer_dict:
result += transfer_dict[char]

print(result)
13 changes: 11 additions & 2 deletions applications/expensive_seq/expensive_seq.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# Your code here

dictionary = {}

def expensive_seq(x, y, z):
# Your code here
if (x, y, z) in dictionary:
return dictionary[(x, y, z)]

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

dictionary[(x, y, z)] = result
return result


if __name__ == "__main__":
Expand Down
28 changes: 27 additions & 1 deletion applications/histo/histo.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# Your code here
import os
import collections

input_text = os.path.join(os.path.dirname(__file__), 'robin.txt')

# Read in all the words in one go
with open(input_text) as f:
words = f.read()

lower_case_words = words.lower()
split_words = lower_case_words.split()
dictionary = {}
largest_word = 0

for word in split_words:
if len(word) > largest_word:
largest_word = len(word)
if word not in dictionary:
dictionary[word] = 1
elif word in dictionary:
dictionary[word] += 1

sorted_dictionary = {k: v for k, v in sorted(dictionary.items(), key=lambda item: item[1], reverse=True)}

for key, value in sorted_dictionary.items():
hash_number = "#" * value
space = largest_word + 2
print(f"{key:{space}} {hash_number}")
26 changes: 17 additions & 9 deletions applications/lookup_table/lookup_table.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
# Your code here
import math
import random

dictionary = {}

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):
# v = math.pow(x, y)
# v = math.factorial(v)
# v //= (x + y)
# v %= 982451653

return 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.
"""
# Your code here

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

dictionary[(x, y)] = v
return v

# Do not modify below this line!

Expand Down
29 changes: 26 additions & 3 deletions applications/markov/markov.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import random
import os.path

input_text = os.path.join(os.path.dirname(__file__), 'input.txt')

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

# print(words)
# TODO: analyze which words can follow other words
# Your code here
split_words = words.split()
word_dictionary = {}

for index, word in enumerate(split_words):
if word not in word_dictionary and index != len(split_words) - 1:
word_dictionary[word] = [split_words[index + 1]]
elif word in word_dictionary:
word_dictionary[word] += [split_words[index + 1]]

# print(word_dictionary)

# TODO: construct 5 random sentences
# Your code here
stop_chars = [".", "?", "!"]

for _ in range(5):
begin_word = random.choice(list(word_dictionary.keys()))
curr_word = begin_word
sentence = ""

while curr_word[-1] not in stop_chars:
sentence += curr_word + " "
word = word_dictionary[curr_word]
curr_word = random.choice(list(word))

print(sentence)
17 changes: 16 additions & 1 deletion applications/no_dups/no_dups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@

def no_dups(s):
# Your code here
array = []
result_string = ""
split_string = s.split()

if s == "":
return result_string

for word in split_string:
if word not in array:
array.append(word)
result_string += word + " "

if result_string.endswith(" "):
return result_string[:-1]

return result_string


if __name__ == "__main__":
Expand Down
18 changes: 17 additions & 1 deletion applications/word_count/word_count.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
def word_count(s):
# Your code here
dictionary = {}
special_characters = ['"', ":", ";", ",", ".", "-", "+", "=", "/", "|", "[", "]", "{", "}", "(", ")", "*", "^", "&", "\\"]

lower_case = s.lower()

for i in special_characters:
lower_case = lower_case.replace(i, "")

split_string = lower_case.split()
if split_string.count == 0:
return dictionary

for word in split_string:
if word in dictionary:
dictionary[word] += 1
else:
dictionary[word] = 1
return dictionary


if __name__ == "__main__":
Expand Down
Loading