CrackingCodes.Ch17 package¶
Submodules¶
CrackingCodes.Ch17.PracticeQuestions module¶
Chapter 17 Practice Questions
Answers Chapter 17 Practice Questions via Python code.
CrackingCodes.Ch17.makeWordPatterns module¶
Make wordPatterns.py file
Creates CrackingCodes.Ch17.wordPatterns
based on the words in our dictionary text file,
dictionary.txt. A word pattern assigns a number to each letter in a word, then generates a pattern representation of
that word based on the number assigned to each letter.
-
CrackingCodes.Ch17.makeWordPatterns.
DICTIONARY_FILE
¶ String containing absolute path to dictionary.txt file.
Type: str
Note
- Download the dictionary file from https://invpy.com/dictionary.txt
- https://www.nostarch.com/crackingcodes (BSD Licensed)
-
CrackingCodes.Ch17.makeWordPatterns.
getWordPattern
(word: str) → str[source]¶ Get word pattern
Returns a string of the pattern form of the given word.
Parameters: word – String containing word to convert into word pattern. Example: >>> import pythontutorials.books.CrackingCodes.Ch17.makeWordPatterns as makeWordPatterns >>> makeWordPatterns.getWordPattern(‘DUSTBUSTER’) ‘0.1.2.3.4.1.2.3.5.6’
Returns: String containing word pattern.
CrackingCodes.Ch17.simpleSubHacker module¶
Simple Substitution Cipher Hacker
Implements a function that can hack a substitution cipher encrypted message.
-
CrackingCodes.Ch17.simpleSubHacker.
nonLettersOrSpacePattern
¶ Regular expression object representing all non-letter characters and space.
Type: re._sre.SRE_Pattern
Note
- https://www.nostarch.com/crackingcodes/ (BSD Licensed)
-
CrackingCodes.Ch17.simpleSubHacker.
addLettersToMapping
(letterMapping: dict, cipherword: str, candidate: str) → None[source]¶ Add letters to cipherletter mapping
The letterMapping parameter takes a dictionary value that stores a cipherletter mapping, which is copied by the function. The cipherword parameter is a string value of the ciphertext word. The candidate parameter is a possible English word that the cipherword could decrypt to.
This function adds the letters in the candidate as potential decryption letters for the cipherletters in the cipherletter mapping.
Parameters: - letterMapping – Dictionary containing a cipherletter mapping.
- cipherword – String containing an encrypted ciphertext word.
- candidate – String containing an English word the cipherword could potentially decrypt to.
Returns: None. Modifies contents of letterMapping by adding letters to the cipherletter mapping.
-
CrackingCodes.Ch17.simpleSubHacker.
decryptWithCipherletterMapping
(ciphertext: str, letterMapping: dict) → str[source]¶ Decrypt substitution cipher message with cipherletter map
Decrypts given substitution cipher encrypted message with given dictionary containing a cipherletter map.
Parameters: - ciphertext – Substitution cipher encrypted message to decrypt.
- letterMapping – Dictionary with cipherletter map that may decrypt the ciphertext.
Returns: String containing decrypted ciphertext message.
Note
- Ambiguous decrypted letters are replaced with an underscore, ‘_’
-
CrackingCodes.Ch17.simpleSubHacker.
getBlankCipherletterMapping
() → dict[source]¶ Get blank cipherletter mapping
Returns a dictionary value that is a blank cipherletter mapping
Returns: Returns dictionary with uppercase latin letters as keys and empty lists as values.
-
CrackingCodes.Ch17.simpleSubHacker.
hackSimpleSub
(message: str) → dict[source]¶ Hack simple substitution cipher
Hacks simple substitution cipher and returns dictionary with cipherletter map that may be able to decrypt given message.
Parameters: message – String containing substitution cipher encrypted message. Returns: Dictionary with cipherletter map that may decrypt given message.
-
CrackingCodes.Ch17.simpleSubHacker.
intersectMappings
(mapA: dict, mapB: dict) → dict[source]¶ Intersects two cipherletter mappings
Checks each letter in LETTERS and adds to intersected map if it exists in both given maps. If either map is empty, the non-empty map is copied to the intersected map.
Parameters: - mapA – Dictionary containing potential decryption letters.
- mapB – Dictionary containing potential decryption letters.
Returns: Dictionary containing intersected map of potential decryption letters.
-
CrackingCodes.Ch17.simpleSubHacker.
removeSolvedLettersFromMapping
(letterMapping: dict) → dict[source]¶ Removes solved letters from cipherletter mapping
Cipherletters in the mapping that map to only one letter are “solved” and can be removed from the other letters.
For example, if ‘A’ maps to potential letters [‘M’, ‘N’] and ‘B’ maps to [‘N’], then we know that ‘B’ must map to ‘N’, so we can remove ‘N’ from the list of what ‘A’ could map to. So ‘A’ then maps 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.)
Parameters: letterMapping – Cipherletter map dictionary to remove solved letters from. Returns: Dictionary containing cipherletter map with solved letters removed.
CrackingCodes.Ch17.wordPatterns module¶
Word patterns file
Dictionary with word patterns as keys and a list of words matching the word pattern as values.
-
CrackingCodes.Ch17.wordPatterns.
allPatterns
¶ Dictionary containing all word patterns in dictionary.txt
Type: dict
Example
>>> {'0.0.1': ['EEL']}
Note
- Docstring gets erased when wordPatterns.py is generated by
CrackingCodes.Ch17.makeWordPatterns