Домашни > Енигма > Решения > Решението на Теодор Вълчев

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

10 точки общо

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

 1""" This is my second homework :)
 2     ,\
 3    # (_
 4      _[]##
 5  ###(())_
 6       ){}####
 7     _((
 8####/  )\
 9     ,;;`;,
10    (_______)
11      {===}
12      /===\
13     /=aat=\
14    The bonsai is PEP8 escape compatible
15    The bonsai is here because my ssd failed n times,
16        and the laptop is giving blue screen every 5 minutes(git files are also not physically saving,
17        just as the homework files in the git repo ...) :)
18        The homework function were having validation and exceptions, but yep in the past ....
19"""
20
21
22def plugboard(text, plugboard_map):
23    """
24    Plugboard for enigma machine
25
26    :param str text: The input text
27    :param list(two_element_sets()) plugboard_map: List of two element sets (Letter swap mapping)
28    :return: Encoded text
29    :rtype str
30    """
31    def plugboard_transform(letter):
32        for set_pair in plugboard_map:
33            if letter in set_pair:
34                return next(iter(set_pair.difference(set(letter))))
35        return letter
36
37    return ''.join([plugboard_transform(letter) for letter in text])
38
39
40def rotor(text, dictionary):
41    """
42    Rotor for enigma machine
43
44    :param str text: The input text
45    :param {one_letter_str,one_letter_str} dictionary: Rotor dictionary
46    :return: Encoded text
47    :rtype str
48    """
49    mapped_text = []
50    for letter in text:
51        if letter.isalpha():
52            mapped_text.append(dictionary.get(letter))
53        else:
54            mapped_text.append(letter)
55    return ''.join(mapped_text)
56
57
58def enigma_encrypt(plugboard_position, rotor_position):
59    """
60    Encryption function for enigma machine
61
62    :param list(two_element_set(str))) plugboard_position: Plugboard configuration
63    :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration
64    :return: Decorator acting over function(str) giving the function enigma-encoded str
65    :rtype function-decorator
66    """
67    def decorator(func):
68        def encoder(text):
69            return func(rotor(plugboard(text, plugboard_position), rotor_position))
70        return encoder
71    return decorator
72
73
74def enigma_decrypt(plugboard_position, rotor_position):
75    """
76    Decryption function for enigma machine
77
78    :param list(two_element_set(str))) plugboard_position: Plugboard configuration
79    :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration
80    :return: Decorator acting over function(str) giving the function enigma-encoded str
81    :rtype function-decorator
82    """
83    def decorator(func):
84        def decoder(text):
85            # Do you see the snake? :P
86            return func(plugboard(rotor(text, dict(zip(rotor_position.values(),
87                                                       rotor_position.keys()))), plugboard_position))
88        return decoder
89    return decorator

.........
----------------------------------------------------------------------
Ran 9 tests in 0.001s

OK

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

f1""" This is my second homework :)f1""" This is my second homework :)
2     ,\2     ,\
3    # (_3    # (_
4      _[]##4      _[]##
5  ###(())_5  ###(())_
6       ){}####6       ){}####
7     _((7     _((
8####/  )\8####/  )\
9     ,;;`;,9     ,;;`;,
10    (_______)10    (_______)
11      {===}11      {===}
12      /===\12      /===\
13     /=aat=\13     /=aat=\
14    The bonsai is PEP8 escape compatible14    The bonsai is PEP8 escape compatible
15    The bonsai is here because my ssd failed n times,15    The bonsai is here because my ssd failed n times,
16        and the laptop is giving blue screen every 5 minutes(git files are also not physically saving,16        and the laptop is giving blue screen every 5 minutes(git files are also not physically saving,
17        just as the homework files in the git repo ...) :)17        just as the homework files in the git repo ...) :)
18        The homework function were having validation and exceptions, but yep in the past ....18        The homework function were having validation and exceptions, but yep in the past ....
19"""19"""
2020
2121
22def plugboard(text, plugboard_map):22def plugboard(text, plugboard_map):
23    """23    """
24    Plugboard for enigma machine24    Plugboard for enigma machine
2525
26    :param str text: The input text26    :param str text: The input text
27    :param list(two_element_sets()) plugboard_map: List of two element sets (Letter swap mapping)27    :param list(two_element_sets()) plugboard_map: List of two element sets (Letter swap mapping)
28    :return: Encoded text28    :return: Encoded text
29    :rtype str29    :rtype str
30    """30    """
31    def plugboard_transform(letter):31    def plugboard_transform(letter):
32        for set_pair in plugboard_map:32        for set_pair in plugboard_map:
33            if letter in set_pair:33            if letter in set_pair:
34                return next(iter(set_pair.difference(set(letter))))34                return next(iter(set_pair.difference(set(letter))))
35        return letter35        return letter
3636
n37    text = text.lower()n37    return ''.join([plugboard_transform(letter) for letter in text])
38    mapped_text = []
39    [mapped_text.append(plugboard_transform(letter)) for letter in text]
40    return ''.join(mapped_text)
4138
4239
43def rotor(text, dictionary):40def rotor(text, dictionary):
44    """41    """
45    Rotor for enigma machine42    Rotor for enigma machine
4643
47    :param str text: The input text44    :param str text: The input text
48    :param {one_letter_str,one_letter_str} dictionary: Rotor dictionary45    :param {one_letter_str,one_letter_str} dictionary: Rotor dictionary
49    :return: Encoded text46    :return: Encoded text
50    :rtype str47    :rtype str
51    """48    """
n52 n
53    text = text.lower()
54    mapped_text = []49    mapped_text = []
55    for letter in text:50    for letter in text:
56        if letter.isalpha():51        if letter.isalpha():
n57            mapped_text.append(dictionary[letter])n52            mapped_text.append(dictionary.get(letter))
58        else:53        else:
59            mapped_text.append(letter)54            mapped_text.append(letter)
60    return ''.join(mapped_text)55    return ''.join(mapped_text)
6156
6257
63def enigma_encrypt(plugboard_position, rotor_position):58def enigma_encrypt(plugboard_position, rotor_position):
64    """59    """
65    Encryption function for enigma machine60    Encryption function for enigma machine
6661
67    :param list(two_element_set(str))) plugboard_position: Plugboard configuration62    :param list(two_element_set(str))) plugboard_position: Plugboard configuration
68    :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration63    :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration
69    :return: Decorator acting over function(str) giving the function enigma-encoded str64    :return: Decorator acting over function(str) giving the function enigma-encoded str
70    :rtype function-decorator65    :rtype function-decorator
71    """66    """
t72 t
73    def decorator(func):67    def decorator(func):
74        def encoder(text):68        def encoder(text):
75            return func(rotor(plugboard(text, plugboard_position), rotor_position))69            return func(rotor(plugboard(text, plugboard_position), rotor_position))
76        return encoder70        return encoder
77    return decorator71    return decorator
7872
7973
80def enigma_decrypt(plugboard_position, rotor_position):74def enigma_decrypt(plugboard_position, rotor_position):
81    """75    """
82    Decryption function for enigma machine76    Decryption function for enigma machine
8377
84    :param list(two_element_set(str))) plugboard_position: Plugboard configuration78    :param list(two_element_set(str))) plugboard_position: Plugboard configuration
85    :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration79    :param {one_letter_str,one_letter_str} rotor_position : Rotor dictionary configuration
86    :return: Decorator acting over function(str) giving the function enigma-encoded str80    :return: Decorator acting over function(str) giving the function enigma-encoded str
87    :rtype function-decorator81    :rtype function-decorator
88    """82    """
89    def decorator(func):83    def decorator(func):
90        def decoder(text):84        def decoder(text):
91            # Do you see the snake? :P85            # Do you see the snake? :P
92            return func(plugboard(rotor(text, dict(zip(rotor_position.values(),86            return func(plugboard(rotor(text, dict(zip(rotor_position.values(),
93                                                       rotor_position.keys()))), plugboard_position))87                                                       rotor_position.keys()))), plugboard_position))
94        return decoder88        return decoder
95    return decorator89    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op