-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathcaesar_cipher.py
More file actions
39 lines (30 loc) · 1015 Bytes
/
caesar_cipher.py
File metadata and controls
39 lines (30 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Caesar Cipher
Caesar's cipher shifts each letter by a fixed number of positions in the
alphabet. Letters wrap around when they pass the end of the alphabet.
Reference: https://en.wikipedia.org/wiki/Caesar_cipher
Complexity:
Time: O(n) where n is the length of the input string
Space: O(n)
"""
from __future__ import annotations
def caesar_cipher(text: str, shift: int) -> str:
"""Encrypt a string using the Caesar cipher with the given shift.
Args:
text: The plaintext string to encrypt.
shift: The number of positions to shift each letter.
Returns:
The encrypted string with shifted letters.
Examples:
>>> caesar_cipher("Hello_World!", 4)
'Lipps_Asvph!'
"""
result = ""
for char in text:
code = ord(char)
if 64 < code < 91:
code = ((code - 65 + shift) % 26) + 65
if 96 < code < 123:
code = ((code - 97 + shift) % 26) + 97
result = result + chr(code)
return result