-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarshift.py
More file actions
57 lines (45 loc) · 1.92 KB
/
caesarshift.py
File metadata and controls
57 lines (45 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Author: Tobias Schneider (https://github.com/iMilchshake)
# Usage:
# decrypt(cipher: str, words: iter, top: int):
# - Decrypts the 'cipher' by finding 'words' for each possible shift-value (1-25)
# - Ignores Numbers, punctuations and whitespaces
# - returns the decrypted cipher and top shift values
#
# shift(cipher: str, s: int, show_punc: bool):
# - Shifts each character in 'cipher' by 's'
# - Removes punctuation if show_punc=False
# - Ignores Numbers, punctuations and whitespaces
#
# get_shiftscores(cipher: str, words: iter, top: int):
# - Calculates scores for each possible shift-value (1-25) by finding 'words'
#
# replace_umlaute(text: str):
# - Replaces all German umlauts
from operator import itemgetter
import string
def shift(cipher: str, s: int, show_punc: bool):
o = ""
if not show_punc: # remove punctuation
cipher = cipher.translate(str.maketrans('', '', string.punctuation))
for c in cipher.lower():
if ord('a') <= ord(c) <= ord('z'): # c is a-z
o += chr(((ord(c) + s - ord('a')) % 26) + ord('a')) # shift by s
else: # c is not a-z
o += c
return o
def get_shiftscores(cipher: str, words: iter, top: int):
scores = list()
for s in range(1, 26):
t = shift(cipher, s, False)
score = 0
for word in words:
score += t.count(word)
scores.append((s, score))
scores.sort(key=itemgetter(1), reverse=True) # Sort Scores
return scores[:top] # return the best ones
def replace_umlaute(text: str):
special_char_map = {ord('ä'): 'ae', ord('ü'): 'ue', ord('ö'): 'oe', ord('ß'): 'ss'}
return text.translate(special_char_map)
def decrypt(cipher: str, words: iter, top: int):
top_scores = get_shiftscores(cipher, words, top)
return shift(cipher, int(top_scores[0][0]), True), top_scores