forked from KenMercusLai/checkio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cipher map.py
41 lines (34 loc) · 1.09 KB
/
Cipher map.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
import itertools
def get_password(cipher_grille, ciphered_password):
flat_grille = [True if j == 'X' else False
for i in cipher_grille
for j in i]
flat_password = [j for i in ciphered_password for j in i]
return ''.join(itertools.compress(flat_password, flat_grille))
def recall_password(cipher_grille, ciphered_password):
password = ''
for i in range(4):
password += get_password(cipher_grille, ciphered_password)
cipher_grille = map(lambda x: ''.join(x), zip(*cipher_grille[::-1]))
return password
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for
# auto-testing
assert recall_password(
('X...',
'..X.',
'X..X',
'....'),
('itdf',
'gdce',
'aton',
'qrdi')) == 'icantforgetiddqd', 'First example'
assert recall_password(
('....',
'X..X',
'.X..',
'...X'),
('xhwc',
'rsqx',
'xqzz',
'fyzr')) == 'rxqrwsfzxqxzhczy', 'Second example'