Skip to content

Instantly share code, notes, and snippets.

@diogomartino
Last active May 21, 2018 17:50
Show Gist options
  • Save diogomartino/909c2877529ae00d7a4411c7d8b1e748 to your computer and use it in GitHub Desktop.
Save diogomartino/909c2877529ae00d7a4411c7d8b1e748 to your computer and use it in GitHub Desktop.
Caesar Cipher Python (Cifra de César)
lista = [" ", "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","w","z"]
def encode(text, offset):
encodedText = ""
for i in range(len(text)):
letterIndex = -1
for a in range(len(lista)):
if lista[a] == text[i]:
letterIndex = a
if letterIndex + offset > len(lista) + 1:
resto = int(letterIndex + offset - (len(lista) + 1))
encodedText += lista[resto]
else :
encodedText += lista[letterIndex + offset]
return encodedText
def decode(text, offset):
decodedText = ""
for i in range(len(text)):
letterIndex = 0
for a in range(len(lista)):
if lista[a] == text[i]:
letterIndex = a
decodedText += lista[letterIndex - offset]
return decodedText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment