Домашни > Енигма > Решения > Решението на Велислава Крумова

Резултати
10 точки от тестове
1 точки от учител

11 точки общо

9 успешни теста
0 неуспешни теста
Код

 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

Дискусия
История

n1def rotor(sentence, word_to_word):n1def rotor(sentence, char_to_char):
2    new_sentence = [word_to_word.get(wordword) 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)
44
55
t6def plugboard(sentence, word_to_word):t6def 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)
1010
1111
12def enigma_encrypt(plugboard_position, rotor_position):12def 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))
1414
1515
16def enigma_decrypt(plugboard_position, rotor_position):16def 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
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def rotor(sentence, word_to_word):f1def rotor(sentence, word_to_word):
t2    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)
54
65
7def plugboard(sentence, word_to_word):6def 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)
1110
1211
13def enigma_encrypt(plugboard_position, rotor_position):12def 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))
1514
1615
17def enigma_decrypt(plugboard_position, rotor_position):16def 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
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op