278 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
If the list of potential decryption letters for a cipherletter in a cipherletter mapping is blank, this
means that this cipherletter could potentially decrypt to any letter. In this case, the intersected
cipherletter mapping will just be a copy of the other mapping’s list of potential decryption letters.
That is, if mapA’s list of potential decryption letters is blank, then set the intersected mapping’s
list to be a copy of mapB’s list. Or if mapB’s list is blank, then set the intersected mapping’s list
to be a copy of mapA’s list.
(If both mappings’ lists were blank, then line 65 will simply copy mapB’s blank list to the
intersected mapping. This is the behavior we want: if both lists are blank then the intersected
mapping will have a blank list.)
simpleSubHacker.py
68. else:
69. # If a letter in mapA[letter] exists in mapB[letter], add
70. # that letter to intersectedMapping[letter].
71. for mappedLetter in mapA[letter]:
72. if mappedLetter in mapB[letter]:
73. intersectedMapping[letter].append(mappedLetter)
The else block handles the case where neither mapA nor mapB are blank. In this case, line 71
loops through the uppercase letter strings in the list at mapA[letter]. Line 72 checks if this
uppercase letter in mapA[letter] also exists in the list of uppercase letter strings in
mapB[letter]. If it does, then line 73 will add this common letter to the list of potential
decryption letters at intersectedMapping[letter].
simpleSubHacker.py
75. return intersectedMapping
Once the for loop that started on line 60 has finished, the cipherletter mapping in
intersectedMapping will only have the potential decryption letters that exist in the lists of
potential decryption letters of both mapA and mapB. This completely intersected cipherletter
mapping is returned on line 75.
Removing Solved Letters from the Letter Mapping
simpleSubHacker.py
78. def removeSolvedLettersFromMapping(letterMapping):
79. # Cipher letters in the mapping that map to only one letter are
80. # "solved" and can be removed from the other letters.
81. # For example, if 'A' maps to potential letters ['M', 'N'], and 'B'
82. # maps to ['N'], then we know that 'B' must map to 'N', so we can