Skip to content

Commit

Permalink
Taking out copy.deepcopy() calls to drastically speed up the simple s…
Browse files Browse the repository at this point in the history
…ub hacker.
  • Loading branch information
asweigart committed Mar 18, 2013
1 parent 1b642cc commit 8b76740
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions simpleSubHacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def addLettersToMapping(letterMapping, cipherword, candidate):
# This function adds the letters of the candidate as potential
# decryption letters for the cipherletters in the cipherletter
# mapping.
# The letterMapping dictionary is modified in-place.

letterMapping = copy.deepcopy(letterMapping)
for i in range(len(cipherword)):
if candidate[i] not in letterMapping[cipherword[i]]:
letterMapping[cipherword[i]].append(candidate[i])
return letterMapping



def intersectMappings(mapA, mapB):
Expand Down Expand Up @@ -84,7 +84,7 @@ def removeSolvedLettersFromMapping(letterMapping):
# to ['M']. Note that now that 'A' maps to only one letter, we can
# remove 'M' from the list of letters for every other
# letter. (This is why there is a loop that keeps reducing the map.)
letterMapping = copy.deepcopy(letterMapping)

loopAgain = True
while loopAgain:
# First assume that we will not loop again:
Expand All @@ -107,7 +107,7 @@ def removeSolvedLettersFromMapping(letterMapping):
if len(letterMapping[cipherletter]) == 1:
# A new letter is now solved, so loop again.
loopAgain = True
return letterMapping
# Nothing is returned because letterMapping is modified in-place.


def hackSimpleSub(message):
Expand All @@ -123,13 +123,13 @@ def hackSimpleSub(message):

# Add the letters of each candidate to the mapping.
for candidate in wordPatterns.allPatterns[wordPattern]:
newMap = addLettersToMapping(newMap, cipherword, candidate)
addLettersToMapping(newMap, cipherword, candidate)

# Intersect the new mapping with the existing intersected mapping.
intersectedMap = intersectMappings(intersectedMap, newMap)

# Remove any solved letters from the other lists.
return removeSolvedLettersFromMapping(intersectedMap)
removeSolvedLettersFromMapping(intersectedMap)
return intersectedMap


def decryptWithCipherletterMapping(ciphertext, letterMapping):
Expand Down

0 comments on commit 8b76740

Please sign in to comment.