def _score_text(self, text): """Score text by counting known English words.""" words = re.findall(r'[A-Za-z]+', text.lower()) score = 0 for w in words: if w in self.word_set: score += len(w) # longer words give more confidence return score 5. Interactive solver (command line) ---------------------------------------------------------------------- def main(): print("=" * 50) print("CODE CRACKER WORD SOLVER") print("Solves simple substitution ciphers (A-Z -> any letter)") print("Enter your coded message (use letters only, spaces fine):") print("(Example: 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt')") print("Type 'quit' to exit.") print("=" * 50)
def _consistent(self, cipher_word, plain_word, mapping): """Check if cipher_word can map to plain_word given existing mapping.""" for c, p in zip(cipher_word, plain_word): if c in mapping and mapping[c] != p: return False return True code cracker word solver
def _apply_map(self, text, mapping): """Apply mapping to entire text (preserve case/punctuation).""" result = [] for ch in text: if ch.isalpha(): lower_ch = ch.lower() if lower_ch in mapping: new_ch = mapping[lower_ch] if ch.isupper(): new_ch = new_ch.upper() result.append(new_ch) else: result.append('?') else: result.append(ch) return ''.join(result) def _score_text(self, text): """Score text by counting known
while True: print("\n> ", end="") cipher = input().strip() if cipher.lower() in ('quit', 'exit', 'q'): break if not cipher: continue decoded, mapping = cracker.solve(cipher) print("\nDecoded message:") print(decoded) if mapping: print("\nCipher mapping (cipher -> plain):") for c in sorted(mapping.keys()): print(f" c.upper() -> mapping[c].upper()") else: print("\nNo mapping found — try a larger word list.") print("-" * 50) 6. Example usage (if run as script) ---------------------------------------------------------------------- if name == " main ": # Test with a famous cipher example (ROT13 actually) test_message = "Gur dhvpx oebja sbk whzcf bire gur ynml qbt" cracker = CodeCracker() result, _ = cracker.solve(test_message) print("\nTest:\n" + test_message) print("-> " + result) p in zip(cipher_word
Gur dhvpx oebja sbk whzcf bire gur ynml qbt
cracker = CodeCracker()