-
Notifications
You must be signed in to change notification settings - Fork 0
/
a1z26.py
65 lines (49 loc) · 1.39 KB
/
a1z26.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
from unidecode import unidecode
def text_to_a1z26(text: str) -> str:
"""
Converts plaintext into A1Z26 code.
Example: text_to_a1z26('hello, world!')
Output: 8-5-12-12-15, 23-15-18-12-4!
"""
text = text.strip()
if text:
new_text = ''
for i in unidecode(text).upper().strip():
if i.isalpha():
new_text += str(ord(i) - 64) + '-'
else:
new_text = new_text[:-1] + i + '-'
new_text = new_text[:-1] if new_text[-1] == '-' else new_text
return new_text.replace(' -', ' ').strip()
return ''
def a1z26_to_text(text: str) -> str:
"""
Converts A1Z26 code into plaintext.
Example: a1z26_to_text('8-5-12-12-15, 23-15-18-12-4!')
Output: HELLO, WORLD!
"""
text = text.strip()
if text:
transformed = []
for word in text.split():
for char in word.split('-'):
cache = []
if char.isnumeric() and 0 < int(char) < 27:
transformed.append(chr(int(char)+64))
else:
for c in char:
if c.isnumeric() and len(cache) > 0 and cache[-1].isnumeric():
cache[-1] += c
elif c.isnumeric():
cache += [c]
else:
cache += c
for i in cache:
if i.isnumeric() and 0 < int(i) < 27:
transformed.append(chr(int(i) + 64))
else:
transformed.append(i)
transformed.append(' ')
return ''.join(transformed).strip()
return ''