Skip to content

Instantly share code, notes, and snippets.

@ralmn
Created October 2, 2014 18:53
Show Gist options
  • Save ralmn/4831315bb9c6357a3317 to your computer and use it in GitHub Desktop.
Save ralmn/4831315bb9c6357a3317 to your computer and use it in GitHub Desktop.
TP4 AP IUT INFO ORS 2014
import math
import itertools
key="nbvcxwmlkjhgfdsqpoiuy treza"
def indiceLettre(t, l):
if l not in t:
return -1
for i in range(len(t)):
ltest = t[i]
if ltest == l:
return i
return -1
def codeLettre(l):
res = '0'
indice = indiceLettre(cle,l)
indice += (indice // 9) + 1
res += str(indice)
return res
def decodeLettre(l, cle):
codeInt = int(l)
codeInt -= 1
codeInt -= codeInt//10
if codeInt < len(cle):
return cle[codeInt]
else:
return ":("
def encode(text, cle):
res = ''
for l in text:
res += codeLettre(l, cle)
return res
def decode(text, cle):
res = ''
codeActuel= ''
for l in text:
if l == '0' and len(codeActuel) > 0:
res += decodeLettre(codeActuel, cle)
codeActuel = l
else:
codeActuel += l
res += decodeLettre(codeActuel, cle)
return res
def genCle():
res = []
abc = "abcdefghijklmnopqrstuvwxyz "
code = ''
for pas in range(1,len(abc)):
#print("pas :",pas)
for start in range(0, len(abc)):
#print("start :",start)
for i in range(start, pas * len(abc) + start):
j = i * start * pas
code += abc[j%(len(abc)-1)]
if len(code) == len(abc):
res.append(code)
code =''
res.append(code)
code = ''
return res
#print(len(genCle()), " code")
aDecode = "01701101013026014013026022050140101307040140110702205050240140101408070901101802801507011014027070140290705050101607"
for cle in itertools.permutations(key):
k = ''.join(cle)
print(decode(aDecode, k))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment