Last active
December 22, 2015 16:49
-
-
Save pmarkun/6501954 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abc = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"] | |
| def move(texto, steps): | |
| if steps > 0: | |
| steps = steps - 26 | |
| novo = '' | |
| for letra in texto.upper(): | |
| if letra in abc: | |
| pos = abc.index(letra) + steps | |
| if pos > len(abc): | |
| pos = pos - len(abc) | |
| novo += abc[pos] | |
| else: | |
| novo += letra | |
| return novo | |
| def clean(texto): | |
| ok = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','w','x','y','z',' '] | |
| novo = '' | |
| for letra in texto: | |
| if letra in ok: | |
| novo += letra | |
| return novo | |
| def freq(texto, steps=None): | |
| if steps: | |
| texto = move(texto, steps) | |
| frequencia = {} | |
| for letra in clean(texto.lower()): | |
| if frequencia.has_key(letra): | |
| frequencia[letra]['total'] += 1 | |
| else: | |
| frequencia[letra] = { 'total' : 1 } | |
| for f in frequencia: | |
| frequencia[f]['freq'] = frequencia[f]['total'] / (len(texto)*1.0) | |
| return frequencia | |
| def sort(a): | |
| return sorted(a, key=lambda x: a[x]['freq'], reverse=True) | |
| def calc_steps(c,d): | |
| return abc.index(c.upper()) - abc.index(d.upper()) | |
| def decypher(code, decode): | |
| if code[-4:] == '.txt': | |
| code = open(code, 'r').read() | |
| if decode[-4:] == '.txt': | |
| decode = open(decode, 'r').read() | |
| c = sort(freq(code)) | |
| d = sort(freq(decode)) | |
| print code | |
| for x in range(1,6): | |
| steps = calc_steps(c[x], d[x]) | |
| print steps | |
| print move(code, steps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment