Let’s get to know our hero: Agent #134 - Mr. Slayer.

He was sent by his CSV agency to Ancient Rome in order to resolve some important national issues. However, something incredible has happened - the enemies have taken Julius Caesar as a prisoner!!!

Caesar, not a simple man as you know, managed to send cryptic message with coordinates of his location hoping that somebody would break the code. Here our agent of the secret service comes to the stage.

But he needs your help!

**Mission:**

You have to implement the function “Encode” of CaesarCrypto class that codes or decodes text based on Caesar’s algorithm.the function receives 2 parameters: an original text of any length of type “string” and a number of type “int” that represents shifts;only letters in both cases must be encrypted;alphabet contains only letters in this range: a-zA-Z;by encryption the letter can change the case;shift could be either positive or negative (for left shift);If the input text is empty, null or includes only whitespaces, return an empty string.

Time's ticking away. The life of Caesar is on the chopping block! Go for it!

Difficulty: introductory

Starter code:

def caesar_crypto_encode(text, shift):
	

Solutions:
from string import ascii_letters as az


def caesar_crypto_encode(text, shift):
    if not text:
        return ''
    sh = shift % 52
    return str.translate(text, str.maketrans(az, az[sh:] + az[:sh])).strip()

from string import ascii_lowercase, ascii_uppercase
S = ascii_lowercase+ascii_uppercase
D = {c:i for i,c in enumerate(S)}

def caesar_crypto_encode(text, shift):
    if text and text != ' '*len(text):
        return ''.join(S[(D[c]+shift)%52] if c in D else c for c in text)
    return ""

def caesar_crypto_encode(text, shift):
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    offset = (shift % 52 + 52) % 52
    return ''.join([(alphabet[(alphabet.find(ch) + offset) % 52] if ch in alphabet else ch) for ch in text.strip()]) if text else ''

from string import ascii_lowercase as al, ascii_uppercase as au
a = au + al

def caesar_crypto_encode(text, shift):
    text = (text or '').strip()
    shift %= 52
    tbl = str.maketrans(a, a[shift:]+a[:shift])
    return text.translate(tbl)

alpha = 'abcdefghijklmnopqrstuvwxyz'
alpha += alpha.upper()

def caesar_crypto_encode(text, shift):
    if not text or not text.strip():
        return ''
    
    return ''.join(
                alpha[(alpha.index(c) + shift) % 52]
                    if c.isalpha() else c for c in text )

def caesar_crypto_encode(text, shift):
    if text in ("", None):
        return ""
    else:
        from string import ascii_letters as abc
        text = text.strip()
        shift = (shift - 1) % len(abc) + 1
        return text.translate(str.maketrans(abc, abc[shift:] + abc[:shift]))

abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
def caesar_crypto_encode(text, shift):
    if not text or text in ' '*10: return ''
    s=''
    for i in text:
        if i.isalpha():
            s += abc[(abc.index(i)+shift)%52]
        else:
            s += i
    return s

from string import ascii_letters

def caesar_crypto_encode(text, shift):
    return '' if text is None or text.isspace() \
        else text.translate(str.maketrans(ascii_letters, ascii_letters[shift % 52:] + ascii_letters[:shift % 52]))

def caesar_crypto_encode(text, shift):
    if not text or not text.strip():return''
    a='abcdefghijklmnopqrstuvwxyz'
    a+=a.upper()
    shift%=len(a)
    b=a[shift:]+a[:shift]
    return text.translate(str.maketrans(a,b))

def caesar_crypto_encode(s, n):
    a = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    r = ''
    for i in s or '':
        if i in a:
            r += a[(a.index(i) + n) % 52]
        else:
            r += i
    return r.strip()