easy frequency analysis python

Solutions on MaxInterview for easy frequency analysis python by the best coders in the world

showing results for - "easy frequency analysis python"
Giorgio
02 May 2019
1import collections
2
3ALPHABET = "abcdefghijklmnopqrstuvwxyz"
4ETAOIN = 'etaoinshrdlcumwfgypbvkjxqz'
5
6def generate_caesar_key(offset):
7    dictionary = {}
8    index = 0
9    for letter in ALPHABET:
10        if index+offset >= 26:
11            index -= 26
12        dictionary.update({letter:ALPHABET[index+offset]})
13        index += 1
14    return dictionary
15
16def switch_encode(string, key):
17    encoded = ""
18    for letter in string.lower():
19        if letter in key.keys():
20            encoded += key[letter]
21        else:
22            encoded += letter
23    return encoded
24
25def switch_decode(string, key):
26    key = dict(zip(key.values(),key.keys()))
27    decoded = ""
28    for letter in string.lower():
29        if letter in key.keys():
30            decoded += key[letter]
31        else:
32            decoded += letter
33    return decoded
34
35def switch_crack(string):
36    key = {}
37    frequent_letters = collections.Counter(string).most_common()
38    index = 0
39    for letter in frequent_letters:
40        if letter[0] in ALPHABET:
41            key[ETAOIN[index]] = letter[0]
42            index += 1
43    return key