# HG changeset patch # User Brian Neal # Date 1392259885 21600 # Node ID 37eab2f88dcc2e64fe3cf864160a899b52961d98 # Parent 800a8467f07a514f3e8a1abc8d5d83068194edc1 Code cleanup while trying to get help with 1 letter being off. Notably the decrypt function now accepts '-' chars as garbles. diff -r 800a8467f07a -r 37eab2f88dcc purple/machine.py --- a/purple/machine.py Tue Dec 10 21:49:36 2013 -0600 +++ b/purple/machine.py Wed Feb 12 20:51:25 2014 -0600 @@ -30,6 +30,7 @@ """ VALID_KEYS = set(string.ascii_uppercase) + VALID_DECRYPT_KEYS = set(string.ascii_uppercase + '-') STRAIGHT_PLUGBOARD = 'AEIOUYBCDFGHJKLMNPQRSTVWXZ' def __init__(self, switches_pos=None, fast_switch=1, middle_switch=2, @@ -177,21 +178,25 @@ """Decrypts the given ciphertext message and returns the plaintext output. - ciphertext must contain only the letters A-Z or else a Purple97Error - exception is raised. + ciphertext must contain only the letters A-Z or '-' or else a + Purple97Error exception is raised. A '-' is used to indicate a garble. + When a '-' is encountered, a '-' is added to the plaintext output and + the machine is stepped. """ plaintext = [] for i, c in enumerate(ciphertext): - if c not in self.VALID_KEYS: + if c not in self.VALID_DECRYPT_KEYS: raise Purple97Error("invalid input '{}' to decrypt".format(c)) + # Process a garble: + if c == '-': + plaintext.append('-') + self.step() + continue + n = self.plugboard[c] - if i == 490 or i == 491: - print(self.sixes.pos, self.fast_switch.pos, - self.middle_switch.pos, self.slow_switch.pos) - if n < 6: # This input goes to the sixes switch x = self.sixes.decrypt(n) @@ -207,10 +212,6 @@ # Now step the switches. self.step() - if i == 490 or i == 491: - print(self.sixes.pos, self.fast_switch.pos, - self.middle_switch.pos, self.slow_switch.pos) - return ''.join(plaintext) def encrypt(self, plaintext): diff -r 800a8467f07a -r 37eab2f88dcc purple/main.py --- a/purple/main.py Tue Dec 10 21:49:36 2013 -0600 +++ b/purple/main.py Wed Feb 12 20:51:25 2014 -0600 @@ -103,7 +103,7 @@ parser.exit(1) if args.encrypt and args.decrypt: parser.error("Please supply either -e or -d, not both") - if args.text and args.file: + if args.text and args.input: parser.error("Please supply either -t or -i, not both") if args.decrypt and args.filter: parser.error("The -f option only works with -e (encrypt)") @@ -126,9 +126,9 @@ parser.error(str(ex)) - if args.file: + if args.input: try: - fp = sys.stdin if args.file == '-' else open(args.file, 'r') + fp = sys.stdin if args.input == '-' else open(args.input, 'r') except IOError as ex: raise SystemExit(str(ex)) diff -r 800a8467f07a -r 37eab2f88dcc purple/tests/test_machine.py --- a/purple/tests/test_machine.py Tue Dec 10 21:49:36 2013 -0600 +++ b/purple/tests/test_machine.py Wed Feb 12 20:51:25 2014 -0600 @@ -93,6 +93,12 @@ ONCL-----HETRIPAITI--------THGERM-----DYTALYC---OV """ +# For debugging with other simulators. Just import this module and print these +# out or whatever...: +_lines = part1.split() +PT1_CT = ''.join(_lines[0::2]) +PT1_PT = ''.join(_lines[1::2]) + class Purple97TestCase(unittest.TestCase): @@ -179,12 +185,8 @@ def test_decrypt_part_1_message(self): - lines = part1.split() - ciphertext = ''.join(lines[0::2]) - plaintext = ''.join(lines[1::2]) - - # Use 'X' in place of the garbles - input_text = ciphertext.replace('-', 'X') + ciphertext = PT1_CT + plaintext = PT1_PT self.assertEqual(len(ciphertext), len(plaintext)) @@ -193,13 +195,12 @@ switches='9-1,24,6-23', alphabet='NOKTYUXEQLHBRMPDICJASVWGZF') - actual = purple.decrypt(input_text) + actual = purple.decrypt(ciphertext) mismatches = [] for n, (a, b) in enumerate(zip(plaintext, actual)): - if a != b and a != '-': - import pdb; pdb.set_trace() - mismatches.append(n) + if a != b: + mismatches.append((n, a, b, plaintext[n:n+10], actual[n:n+10])) msg = None if mismatches: @@ -210,9 +211,8 @@ def test_encrypt_part_1_message(self): - lines = part1.split() - ciphertext = ''.join(lines[0::2]) - plaintext = ''.join(lines[1::2]) + ciphertext = PT1_CT + plaintext = PT1_PT # Use 'X' in place of the garbles input_text = plaintext.replace('-', 'X') @@ -229,7 +229,7 @@ mismatches = [] for n, (a, b) in enumerate(zip(ciphertext, actual)): if a != b and a != '-': - mismatches.append(n) + mismatches.append((n, a, b)) msg = None if mismatches: