-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplacer.py
52 lines (36 loc) · 1.17 KB
/
replacer.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
from typing import Tuple
from utils import get_indexes, readFileFromAppFolder, reverse
layout1 = readFileFromAppFolder('layout1.txt')
layout2 = readFileFromAppFolder('layout2.txt')
def detectLayout(text: str) -> Tuple[str, str]:
'''
Returns (currentLayout, otherLayout)
'''
currentLayout = None
otherLayout = None
reversedText = reverse(text)
for c in reversedText:
if c in layout1 and c in layout2:
continue
elif c in layout1:
currentLayout = layout1
otherLayout = layout2
break
elif c in layout2:
currentLayout = layout2
otherLayout = layout1
break
return (currentLayout, otherLayout)
def replacer(text: str) -> str:
currentLayout, otherLayout = detectLayout(text)
if currentLayout is None:
return text
output = ''
for c in text:
indexes = get_indexes(c, currentLayout)
if len(indexes) == 0:
output += c
continue
index = indexes[0] # TODO handle multiple indexes
output += otherLayout[index]
return output