1def plugboard(text, plugged_letters):
2 def plug(letter):
3 for a, b in plugged_letters:
4 if letter == a:
5 return b
6 if letter == b:
7 return a
8 return letter
9 return ''.join([plug(letter) for letter in text])
10
11
12def rotor(text, rotated):
13 return ''.join([rotated.get(letter, letter) for letter in text])
14
15
16def enigma_encrypt(plugboard_position, rotor_position):
17 def encryptor(func):
18 def encrypt(text):
19 return func(rotor(plugboard(text, plugboard_position), rotor_position))
20 return encrypt
21 return encryptor
22
23
24def enigma_decrypt(plugboard_position, rotor_position):
25 inverse_position = {v: k for k, v in rotor_position.items()}
26
27 def decryptor(func):
28 def decrypt(text):
29 return func(plugboard(rotor(text, inverse_position), plugboard_position))
30 return decrypt
31 return decryptor
32
33
34"""
35and a cursed one-liner for nightmares 💀
36plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda txt, plg: ''.join([dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l, l) for l in txt]), lambda txt, rtr: ''.join([rtr.get(l, l) for l in txt]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position))
37"""
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
f | 1 | def plugboard(text, plugged_letters): | f | 1 | def plugboard(text, plugged_letters): |
2 | def plug(letter): | 2 | def plug(letter): | ||
3 | for a, b in plugged_letters: | 3 | for a, b in plugged_letters: | ||
4 | if letter == a: | 4 | if letter == a: | ||
5 | return b | 5 | return b | ||
6 | if letter == b: | 6 | if letter == b: | ||
7 | return a | 7 | return a | ||
8 | return letter | 8 | return letter | ||
9 | return ''.join([plug(letter) for letter in text]) | 9 | return ''.join([plug(letter) for letter in text]) | ||
10 | 10 | ||||
11 | 11 | ||||
12 | def rotor(text, rotated): | 12 | def rotor(text, rotated): | ||
13 | return ''.join([rotated.get(letter, letter) for letter in text]) | 13 | return ''.join([rotated.get(letter, letter) for letter in text]) | ||
14 | 14 | ||||
15 | 15 | ||||
16 | def enigma_encrypt(plugboard_position, rotor_position): | 16 | def enigma_encrypt(plugboard_position, rotor_position): | ||
17 | def encryptor(func): | 17 | def encryptor(func): | ||
18 | def encrypt(text): | 18 | def encrypt(text): | ||
19 | return func(rotor(plugboard(text, plugboard_position), rotor_position)) | 19 | return func(rotor(plugboard(text, plugboard_position), rotor_position)) | ||
20 | return encrypt | 20 | return encrypt | ||
21 | return encryptor | 21 | return encryptor | ||
22 | 22 | ||||
23 | 23 | ||||
24 | def enigma_decrypt(plugboard_position, rotor_position): | 24 | def enigma_decrypt(plugboard_position, rotor_position): | ||
25 | inverse_position = {v: k for k, v in rotor_position.items()} | 25 | inverse_position = {v: k for k, v in rotor_position.items()} | ||
26 | 26 | ||||
27 | def decryptor(func): | 27 | def decryptor(func): | ||
28 | def decrypt(text): | 28 | def decrypt(text): | ||
29 | return func(plugboard(rotor(text, inverse_position), plugboard_position)) | 29 | return func(plugboard(rotor(text, inverse_position), plugboard_position)) | ||
30 | return decrypt | 30 | return decrypt | ||
31 | return decryptor | 31 | return decryptor | ||
32 | 32 | ||||
33 | 33 | ||||
34 | """ | 34 | """ | ||
35 | and a cursed one-liner for nightmares 💀 | 35 | and a cursed one-liner for nightmares 💀 | ||
t | 36 | plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda txt, plg: ''.join([dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l, l) for l in txt]), lambda txt, rtr: ''.join([rtr[l] if l in rtr else l for l in txt]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position)) | t | 36 | plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda txt, plg: ''.join([dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l, l) for l in txt]), lambda txt, rtr: ''.join([rtr.get(l, l) for l in txt]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position)) |
37 | """ | 37 | """ |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, plugged_letters): | f | 1 | def plugboard(text, plugged_letters): |
2 | def plug(letter): | 2 | def plug(letter): | ||
3 | for a, b in plugged_letters: | 3 | for a, b in plugged_letters: | ||
4 | if letter == a: | 4 | if letter == a: | ||
5 | return b | 5 | return b | ||
6 | if letter == b: | 6 | if letter == b: | ||
7 | return a | 7 | return a | ||
8 | return letter | 8 | return letter | ||
9 | return ''.join([plug(letter) for letter in text]) | 9 | return ''.join([plug(letter) for letter in text]) | ||
10 | 10 | ||||
11 | 11 | ||||
12 | def rotor(text, rotated): | 12 | def rotor(text, rotated): | ||
n | 13 | return ''.join([rotated[letter] if letter in rotated else letter for letter in text]) | n | 13 | return ''.join([rotated.get(letter, letter) for letter in text]) |
14 | 14 | ||||
15 | 15 | ||||
16 | def enigma_encrypt(plugboard_position, rotor_position): | 16 | def enigma_encrypt(plugboard_position, rotor_position): | ||
17 | def encryptor(func): | 17 | def encryptor(func): | ||
18 | def encrypt(text): | 18 | def encrypt(text): | ||
19 | return func(rotor(plugboard(text, plugboard_position), rotor_position)) | 19 | return func(rotor(plugboard(text, plugboard_position), rotor_position)) | ||
20 | return encrypt | 20 | return encrypt | ||
21 | return encryptor | 21 | return encryptor | ||
22 | 22 | ||||
23 | 23 | ||||
24 | def enigma_decrypt(plugboard_position, rotor_position): | 24 | def enigma_decrypt(plugboard_position, rotor_position): | ||
25 | inverse_position = {v: k for k, v in rotor_position.items()} | 25 | inverse_position = {v: k for k, v in rotor_position.items()} | ||
26 | 26 | ||||
27 | def decryptor(func): | 27 | def decryptor(func): | ||
28 | def decrypt(text): | 28 | def decrypt(text): | ||
29 | return func(plugboard(rotor(text, inverse_position), plugboard_position)) | 29 | return func(plugboard(rotor(text, inverse_position), plugboard_position)) | ||
30 | return decrypt | 30 | return decrypt | ||
31 | return decryptor | 31 | return decryptor | ||
32 | 32 | ||||
33 | 33 | ||||
34 | """ | 34 | """ | ||
35 | and a cursed one-liner for nightmares 💀 | 35 | and a cursed one-liner for nightmares 💀 | ||
t | 36 | to_dict, plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda sts: dict([(x, y) for x, y in sts] + [(y, x) for x, y in sts]), lambda txt, plg: ''.join([(lambda l: l if l not in to_dict(plg) else to_dict(plg).get(l))(l) for l in txt]), lambda txt, rtr: ''.join([rtr[l] if l in rtr else l for l in txt]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position)) | t | 36 | plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda txt, plg: ''.join([dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l, l) for l in txt]), lambda txt, rtr: ''.join([rtr[l] if l in rtr else l for l in txt]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position)) |
37 | """ | 37 | """ |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, plugged_letters): | f | 1 | def plugboard(text, plugged_letters): |
2 | def plug(letter): | 2 | def plug(letter): | ||
3 | for a, b in plugged_letters: | 3 | for a, b in plugged_letters: | ||
4 | if letter == a: | 4 | if letter == a: | ||
5 | return b | 5 | return b | ||
6 | if letter == b: | 6 | if letter == b: | ||
7 | return a | 7 | return a | ||
8 | return letter | 8 | return letter | ||
9 | return ''.join([plug(letter) for letter in text]) | 9 | return ''.join([plug(letter) for letter in text]) | ||
10 | 10 | ||||
11 | 11 | ||||
12 | def rotor(text, rotated): | 12 | def rotor(text, rotated): | ||
n | 13 | return ' '.join([''.join([rotated[letter] for letter in word]) for word in text.split()]) | n | 13 | return ''.join([rotated[letter] if letter in rotated else letter for letter in text]) |
14 | 14 | ||||
15 | 15 | ||||
16 | def enigma_encrypt(plugboard_position, rotor_position): | 16 | def enigma_encrypt(plugboard_position, rotor_position): | ||
17 | def encryptor(func): | 17 | def encryptor(func): | ||
18 | def encrypt(text): | 18 | def encrypt(text): | ||
19 | return func(rotor(plugboard(text, plugboard_position), rotor_position)) | 19 | return func(rotor(plugboard(text, plugboard_position), rotor_position)) | ||
20 | return encrypt | 20 | return encrypt | ||
21 | return encryptor | 21 | return encryptor | ||
22 | 22 | ||||
23 | 23 | ||||
24 | def enigma_decrypt(plugboard_position, rotor_position): | 24 | def enigma_decrypt(plugboard_position, rotor_position): | ||
25 | inverse_position = {v: k for k, v in rotor_position.items()} | 25 | inverse_position = {v: k for k, v in rotor_position.items()} | ||
26 | 26 | ||||
27 | def decryptor(func): | 27 | def decryptor(func): | ||
28 | def decrypt(text): | 28 | def decrypt(text): | ||
29 | return func(plugboard(rotor(text, inverse_position), plugboard_position)) | 29 | return func(plugboard(rotor(text, inverse_position), plugboard_position)) | ||
30 | return decrypt | 30 | return decrypt | ||
31 | return decryptor | 31 | return decryptor | ||
32 | 32 | ||||
33 | 33 | ||||
34 | """ | 34 | """ | ||
35 | and a cursed one-liner for nightmares 💀 | 35 | and a cursed one-liner for nightmares 💀 | ||
t | 36 | plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda txt, plg: ''.join([(lambda l: l if dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l) is None else dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l))(l) for l in txt]), lambda txt, rtr: ' '.join([''.join([rtr[l] for l in w]) for w in txt.split()]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position)) | t | 36 | to_dict, plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda sts: dict([(x, y) for x, y in sts] + [(y, x) for x, y in sts]), lambda txt, plg: ''.join([(lambda l: l if l not in to_dict(plg) else to_dict(plg).get(l))(l) for l in txt]), lambda txt, rtr: ''.join([rtr[l] if l in rtr else l for l in txt]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position)) |
37 | """ | 37 | """ |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def plugboard(text, plugged_letters): | f | 1 | def plugboard(text, plugged_letters): |
2 | def plug(letter): | 2 | def plug(letter): | ||
3 | for a, b in plugged_letters: | 3 | for a, b in plugged_letters: | ||
4 | if letter == a: | 4 | if letter == a: | ||
5 | return b | 5 | return b | ||
6 | if letter == b: | 6 | if letter == b: | ||
7 | return a | 7 | return a | ||
8 | return letter | 8 | return letter | ||
9 | return ''.join([plug(letter) for letter in text]) | 9 | return ''.join([plug(letter) for letter in text]) | ||
10 | 10 | ||||
11 | 11 | ||||
12 | def rotor(text, rotated): | 12 | def rotor(text, rotated): | ||
13 | return ' '.join([''.join([rotated[letter] for letter in word]) for word in text.split()]) | 13 | return ' '.join([''.join([rotated[letter] for letter in word]) for word in text.split()]) | ||
14 | 14 | ||||
15 | 15 | ||||
n | 16 | def enigma_encrypt(*, plugboard_position, rotor_position): | n | 16 | def enigma_encrypt(plugboard_position, rotor_position): |
17 | def encryptor(func): | 17 | def encryptor(func): | ||
18 | def encrypt(text): | 18 | def encrypt(text): | ||
19 | return func(rotor(plugboard(text, plugboard_position), rotor_position)) | 19 | return func(rotor(plugboard(text, plugboard_position), rotor_position)) | ||
20 | return encrypt | 20 | return encrypt | ||
21 | return encryptor | 21 | return encryptor | ||
22 | 22 | ||||
23 | 23 | ||||
t | 24 | def enigma_decrypt(*, plugboard_position, rotor_position): | t | 24 | def enigma_decrypt(plugboard_position, rotor_position): |
25 | inverse_position = {v: k for k, v in rotor_position.items()} | 25 | inverse_position = {v: k for k, v in rotor_position.items()} | ||
26 | 26 | ||||
27 | def decryptor(func): | 27 | def decryptor(func): | ||
28 | def decrypt(text): | 28 | def decrypt(text): | ||
29 | return func(plugboard(rotor(text, inverse_position), plugboard_position)) | 29 | return func(plugboard(rotor(text, inverse_position), plugboard_position)) | ||
30 | return decrypt | 30 | return decrypt | ||
31 | return decryptor | 31 | return decryptor | ||
32 | 32 | ||||
33 | 33 | ||||
34 | """ | 34 | """ | ||
35 | and a cursed one-liner for nightmares 💀 | 35 | and a cursed one-liner for nightmares 💀 | ||
36 | plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda txt, plg: ''.join([(lambda l: l if dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l) is None else dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l))(l) for l in txt]), lambda txt, rtr: ' '.join([''.join([rtr[l] for l in w]) for w in txt.split()]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position)) | 36 | plugboard, rotor, enigma_encrypt, enigma_decrypt = lambda txt, plg: ''.join([(lambda l: l if dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l) is None else dict([(x, y) for x, y in plg] + [(y, x) for x, y in plg]).get(l))(l) for l in txt]), lambda txt, rtr: ' '.join([''.join([rtr[l] for l in w]) for w in txt.split()]), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(rotor(plugboard(txt, plugboard_position), rotor_position)), lambda plugboard_position, rotor_position: lambda f: lambda txt: f(plugboard(rotor(txt, {v: k for k, v in rotor_position.items()}), plugboard_position)) | ||
37 | """ | 37 | """ |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|