The comments already suggest that this would be a perfect place to make them a function. In the same function you have the two blocks # propagate contact right and # propagate contact left. This way you avoid having to go through the list more often than necessary (the edge case Z will iterate through the alphabet twice, once to see if it is there and once to actually get the index). Just use try.except in Machine.encipher_character: # compute the contact position of the first rotor and machine's input One place where you can use this is the check whether the character to encode is in the character set. It is better to ask forgiveness than permission. Join can take a generator expression directly, so you can get rid of one set of parenthesis in Machine.encipher: return "".join(self.encipher_character(x) for x in text.upper()) You can use string.ascii_uppercase for the uppercase alphabet. This saves having to do a costly list addition offset times.ĭifferent commonly used ASCII character classes are included in the string module. Your Rotor.rotate can be simplified to def rotate(self, offset=1): Overall comments on the style and documentation would be much appreciated, too.įYI: development for this project has been moved to: I'm wondering how the encipher algorithm might be implemented more cleanly (in particular, how the code might be better distributed across the Rotor and Reflector classes). Turn_frequency = len(Machine.ALPHABET)*index # rotate the first rotor and anything else that needs it # reflect and compute the starting contact position with the right rotorĬontact_letter = Machine.ALPHABET # compute the contact position of the first rotor and machine's inputĬontact_index = (x)Ĭontact_letter = rotor.alphabet Return the character at the terminating contact position as the input Rotate the first rotor, and check if any other rotor should be rotatedĥ. Once we get to the reflector, get the reflection and repeat the aboveĤ. Letter to pass through to the next rotor in the machine's sequenceģ. For each of the rotors, determine the character in contact with x.ĭetermine the enciphering for that character, and use it as the next If x is not in the known character set, don't encipher itĢ. Runs a character through the machine's cipher algorithmġ. Return "".join((self.encipher_character(x) for x in text.upper()))Īrgs: text (string) cipher text to decode Most of the enciphering is done through the helper function encipher_character. This class exposes the encipher and decipher methods. Returns the reflection of the input character Raise ValueError("Mapping for is invalid".format(x, y)) Self.mappings = dict(zip(Machine.ALPHABET, mappings)) Reflector("CDAB")Īrgs: mappings (string) bijective map representing the reflection Models a 'reflector' in the Enigma machine. A Reflector can reflect a character and is used to put the input back through machine's rotors. Gets the plain text mapping of a cipher text character Gets the cipher text mapping of a plain text character Self.alphabet = self.alphabet + self.alphabet Rotates the rotor the given number of charactersĪrgs: offset (int) how many turns to make Helper to re-initialize the rotor to its initial configuration Self.reverse_mappings = dict(zip(mappings, self.alphabet)) Self.forward_mappings = dict(zip(self.alphabet, mappings)) Offset (int) the starting position of the rotor Mappings (string) encipherings for the machine's alphabet. Rotated once from ABCD (the clear text character 'B' is facing the user) Rotor("BCDA", 1) means that A->B, B->C, C->D, D->A and the rotor has been A Rotor knows how to rotate itself, and provides methods for navigating connections with the adjacent circuits through the encipher and decipher methods. > reflector = Reflector("EJMZALYXVBWFCRQUONTSPIKHGD") > r3 = Rotor("DJYPKQNOZLMGIHFETRVCBXSWAU", 3) > r2 = Rotor("WNYPVJXTOAMQIZKSRFUHGCEDBL", 2) Here's some test code that illustrates how the machine's construction and use: > r1 = Rotor("VEADTQRWUFZNLHYPXOGKJIMCSB", 1) This code simulates the Enigma machine, minus the plugboard.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |