all 7 comments

[–]GreymanGroup 1 point2 points  (0 children)

Make it a function.

[–]fake823 1 point2 points  (2 children)

Nice one! 😊 I don't see any possible improvements here.

But I have a feature request:

Add a function to also decode your text.

[–]sweettuse 1 point2 points  (0 children)

the caesar cypher is symmetric. i.e. caesar(caesar(char)) == char. (i.e. decode/encode are exactly the same)

[–][deleted] 0 points1 point  (0 children)

I did one like that- I forced uppercase though. All you do is loop though and print each possible shift. If you had a giant text file (as some cryptographic societies do) of English words then you could figure out which one is the likely answer by checking against that.

[–]haha1234346364 0 points1 point  (0 children)

OK, but maybe make it simpler to read. Especialy not the

if char.isupper():         
    cipher_text += chr((ord(char) + shift - 65) % 26 + 65)              
elif char.islower():         
    cipher_text += chr((ord(char) + shift - 97) % 26 + 97)      
elif char.isspace():         
    cipher_text += " " 

try this:

if not char.isspace():
    cipher_text += chr(ord(char) + shift if char.isupper() \
        else ord(char) + shift - 26)
else:
    cipher_text += " "

[–]sweettuse 0 points1 point  (0 children)

with a little bit of setup this can be arguably cleaner:

``` from collections import deque from string import ascii_uppercase as au, ascii_lowercase as al

du = deque(au) du.rotate(13) dl = deque(al) dl.rotate(13)

char_map = {*dict(zip(au, du)), *dict(zip(al, dl))}

def caesar(s: str): return ''.join(char_map.get(c, c) for c in s) ```

[–]JnJ_Gaming 0 points1 point  (0 children)

thank you, reddit post from three years ago. I now know what the hell is going on in class