1def rotor(sentence, char_to_char):
2 new_sentence = [char_to_char.get(char, char) for char in sentence]
3 return ''.join(new_sentence)
4
5
6def plugboard(sentence, char_to_char):
7 char_to_char_dict = {char1: char2 for char1, char2 in char_to_char}
8 char_to_char_dict.update({char2: char1 for char1, char2 in char_to_char})
9 return ''.join(char_to_char_dict.get(word, word) for word in sentence)
10
11
12def enigma_encrypt(plugboard_position, rotor_position):
13 return lambda func: lambda text: func(rotor(plugboard(text, plugboard_position), rotor_position))
14
15
16def enigma_decrypt(plugboard_position, rotor_position):
17 reversed_rotor_position = {v: k for k, v in rotor_position.items()}
18 return lambda func: lambda text: func(plugboard(rotor(text, reversed_rotor_position), plugboard_position))
.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s
OK
n | 1 | def rotor(sentence, word_to_word): | n | 1 | def rotor(sentence, char_to_char): |
2 | new_sentence = [word_to_word.get(word, word) for word in sentence] | 2 | new_sentence = [char_to_char.get(char, char) for char in sentence] | ||
3 | return ''.join(new_sentence) | 3 | return ''.join(new_sentence) | ||
4 | 4 | ||||
5 | 5 | ||||
t | 6 | def plugboard(sentence, word_to_word): | t | 6 | def plugboard(sentence, char_to_char): |
7 | word_to_word_dict = {char1: char2 for char1, char2 in word_to_word} | 7 | char_to_char_dict = {char1: char2 for char1, char2 in char_to_char} | ||
8 | word_to_word_dict.update({char2: char1 for char1, char2 in word_to_word}) | 8 | char_to_char_dict.update({char2: char1 for char1, char2 in char_to_char}) | ||
9 | return ''.join(word_to_word_dict.get(word, word) for word in sentence) | 9 | return ''.join(char_to_char_dict.get(word, word) for word in sentence) | ||
10 | 10 | ||||
11 | 11 | ||||
12 | def enigma_encrypt(plugboard_position, rotor_position): | 12 | def enigma_encrypt(plugboard_position, rotor_position): | ||
13 | return lambda func: lambda text: func(rotor(plugboard(text, plugboard_position), rotor_position)) | 13 | return lambda func: lambda text: func(rotor(plugboard(text, plugboard_position), rotor_position)) | ||
14 | 14 | ||||
15 | 15 | ||||
16 | def enigma_decrypt(plugboard_position, rotor_position): | 16 | def enigma_decrypt(plugboard_position, rotor_position): | ||
17 | reversed_rotor_position = {v: k for k, v in rotor_position.items()} | 17 | reversed_rotor_position = {v: k for k, v in rotor_position.items()} | ||
18 | return lambda func: lambda text: func(plugboard(rotor(text, reversed_rotor_position), plugboard_position)) | 18 | return lambda func: lambda text: func(plugboard(rotor(text, reversed_rotor_position), plugboard_position)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def rotor(sentence, word_to_word): | f | 1 | def rotor(sentence, word_to_word): |
t | 2 | words = list(sentence) | t | ||
3 | new_sentence = [word_to_word.get(word, word) for word in words] | 2 | new_sentence = [word_to_word.get(word, word) for word in sentence] | ||
4 | return ''.join(new_sentence) | 3 | return ''.join(new_sentence) | ||
5 | 4 | ||||
6 | 5 | ||||
7 | def plugboard(sentence, word_to_word): | 6 | def plugboard(sentence, word_to_word): | ||
8 | word_to_word_dict = {char1: char2 for char1, char2 in word_to_word} | 7 | word_to_word_dict = {char1: char2 for char1, char2 in word_to_word} | ||
9 | word_to_word_dict.update({char2: char1 for char1, char2 in word_to_word}) | 8 | word_to_word_dict.update({char2: char1 for char1, char2 in word_to_word}) | ||
10 | return ''.join(word_to_word_dict.get(word, word) for word in sentence) | 9 | return ''.join(word_to_word_dict.get(word, word) for word in sentence) | ||
11 | 10 | ||||
12 | 11 | ||||
13 | def enigma_encrypt(plugboard_position, rotor_position): | 12 | def enigma_encrypt(plugboard_position, rotor_position): | ||
14 | return lambda func: lambda text: func(rotor(plugboard(text, plugboard_position), rotor_position)) | 13 | return lambda func: lambda text: func(rotor(plugboard(text, plugboard_position), rotor_position)) | ||
15 | 14 | ||||
16 | 15 | ||||
17 | def enigma_decrypt(plugboard_position, rotor_position): | 16 | def enigma_decrypt(plugboard_position, rotor_position): | ||
18 | reversed_rotor_position = {v: k for k, v in rotor_position.items()} | 17 | reversed_rotor_position = {v: k for k, v in rotor_position.items()} | ||
19 | return lambda func: lambda text: func(plugboard(rotor(text, reversed_rotor_position), plugboard_position)) | 18 | return lambda func: lambda text: func(plugboard(rotor(text, reversed_rotor_position), plugboard_position)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|