-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.py
39 lines (39 loc) · 1021 Bytes
/
test1.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
def T2M(text):
res = ""
for ch in text:
if ch in pattern:
res += pattern[ch]+" "
else:
return "Invalid : "+ text
return res
def M2T(code):
res = ""
code = code.split()
ptt = dict((v,k) for k,v in pattern.items())
for m in code:
if m in ptt:
res += ptt[m]
else:
return "Invalid : " + ' '.join(code)
return res.strip()
name = input().strip()
file = open(name,'r')
method = file.readline().strip()
raw_pattern = file.readline().strip()
pattern = dict()
line = file.readline().strip()
i = raw_pattern.find("]")
while i != -1 :
pattern[raw_pattern[i-1]] = raw_pattern[i+1:raw_pattern.find("[",i + 1)]
i = raw_pattern.find("]",i+1)
if method == "T2M":
while len(line) != 0:
print(T2M(line))
line = file.readline().strip()
elif method == "M2T":
while len(line) != 0:
print(M2T(line))
line = file.readline().strip()
else:
print("Invalid code")
file.close()