-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.py
50 lines (39 loc) · 1.3 KB
/
crypto.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
def isCryptSolution(crypt, solution):
mapping = {}
for s in solution:
mapping.setdefault(s[0], s[1])
def decrypt(crypt):
if len(crypt) == 1:
return int(mapping[crypt]), True
dec = ''
is_valid = True
for i, c in enumerate(crypt):
d = mapping[c]
dec += d
if i == 0 and d == '0':
is_valid = False
break
return int(dec), is_valid
n0, is_valid = decrypt(crypt[0])
if not is_valid:
return False
n1, is_valid = decrypt(crypt[1])
if not is_valid:
return False
n2, is_valid = decrypt(crypt[2])
if not is_valid:
return False
return n0 + n1 == n2
def main():
crypt = ["SEND", "MORE", "MONEY"]
solution = [['O', '0'],
['M', '1'],
['Y', '2'],
['E', '5'],
['N', '6'],
['D', '7'],
['R', '8'],
['S', '9']]
print isCryptSolution(crypt, solution)
if __name__ == "__main__":
main()