31 lines
865 B
Python
31 lines
865 B
Python
def encode(plain_text: str) -> str:
|
|
encoded_text: str = ""
|
|
weight: int = 0
|
|
for char in plain_text.lower():
|
|
if char.isalpha():
|
|
encoded_text += chr(97 + 25 - (ord(char) - 97))
|
|
weight += 1
|
|
elif char.isdigit():
|
|
encoded_text += char
|
|
weight += 1
|
|
else:
|
|
continue
|
|
if (weight % 5) == 0:
|
|
encoded_text += " "
|
|
# remove space at the end when it exists
|
|
if encoded_text[-1] == " ":
|
|
return encoded_text[0:-1]
|
|
return encoded_text
|
|
|
|
|
|
def decode(ciphered_text: str) -> str:
|
|
decoded_text: str = ""
|
|
for char in ciphered_text.lower():
|
|
if char.isalpha():
|
|
decoded_text += chr(122 - 25 + (122 - ord(char)))
|
|
elif char.isdigit():
|
|
decoded_text += char
|
|
else:
|
|
continue
|
|
return decoded_text
|