You are on page 1of 4

def CaeserCipher(s, k):

newWord=''
for ch in s:
if ord(ch)+k<=122:
new_ch = chr(ord(ch)+k)
else:
new_ch = chr(ord(ch)+k-26)
newWord = newWord+new_ch
print (newWord)
def main():
file = open("plain.txt", "r")
word = [line.strip() for line in file]
key = eval(input("Please enter the number of positions in the alphabet you wish to
apply: "))
newWord=''
for ch in word:
if ord(ch)+key<=122:
new_ch = chr(ord(ch)+key)
else:
new_ch = chr(ord(ch)+key-26)
newWord = newWord+new_ch
print newWord >> encrypted.txt
def parseSentences(bigString):
return bigString.replace("!", ".").replace("?", ".").split(".")
def replaceNonLetters(s):
nonLetters = [chr(x) for x in range(0, 128) if not chr(x).isalpha()]
for char in nonLetters:
s = s.replace(char, " ")
return s
def parseWords(sentenceList):

return [replaceNonLetters(x).split() for x in sentenceList]


def computeFrequenciesFast(D):
Dictionary = sentenceList
frequencies = {}
index = 0
while index < len(sentenceList):
loc = getIndex(Dictionary, sentenceList[index])
if loc >= 0:
frequencies[loc] = frequencies[loc] + 1
else:
Dictionary.append(sentenceList[index])
frequencies.append(1)
index = index + 1
return [Dictionary, frequencies]
def mergeDictionaries(bigDict, smallDict):
f = open("war.txt", "r")
f = open("hyde.txt", "r")
f = open("illiad.txt", "r")
sentenceList = parseSentences(bigString)
nestedWordList = parseWords(sentenceList)
nestedWordList = [x[1:] for x in nestedWordList]
wordList = [y for x in nestedWordList for y in x]
characterNames = [x for x in wordList if x[0].isupper() and len(x) > 3]
[Dictionary, frequencies] = computeFrequenciesFast(characterNames)
mergedDictionary = zip(frequencies, Dictionary)
mergedDictionary.sort(reverse = True)
print mergedDictionary[10:]

file = open("encrypt.txt", "r")

x = [line.strip() for line in file]


NUM_LETTERS = 26
def DeCoder(S, K):
y = ""
for i in S:
if(i.isupper()):
x = ord(i)
x += K
if x > ord('Z'):
x -= NUM_LETTERS
elif x < ord('A'):
x += NUM_LETTERS
y += chr(x)
else:
y += " "
return y
def GoodnessFinder(S):
y=0
for i in S:
if i.isupper():
x = ord(i)
x -= ord('A')
y += letterGoodness[x]
else:
y += 1
return y
def GoodnessComparer(S):
goodnesstocompare = GoodnessFinder(S)
goodness = 0
v = ''
best_v = S
for i in range(0, 26):
v = SpyCoder(S, i)
goodness = GoodnessFinder(v)
if goodness > goodnesstocompare:

best_v = v
goodnesstocompare = goodness
return best_v

print(GoodnessComparer(x))

You might also like