Skip to content

Instantly share code, notes, and snippets.

Last active Sep 12, 2021
What would you like to do?
A simple tool to help you solve your cryptograms.
#!/usr/bin/env python3
A small tool to help you solve your cryptogram for Python 3.
./ <cryptogram_text>
From there you'll get a prompt until you've solved the puzzle at hand
(which is up to you to determine).
At the prompt:
> XY --> (any two letters); Swap letters X and Y in the working solution.
> r --> Retype the original cryptogram text.
> f --> Show letter frequency alongside English letter ranking.
> q --> Quit.
# ____________________________________________________________
# Imports
import sys
from collections import Counter
# ____________________________________________________________
# Functions
def swap(s, x, y):
""" Return string s with letters x and y swapped. """
s = s.replace(y, '_')
s = s.replace(x, y)
return s.replace('_', x)
def show_letter_frequencies(s):
""" Show the top 10 letters in s, sorted most-frequent first. """
letter_counts = Counter(s.replace(' ', ''))
letters = [letter for letter, _ in letter_counts.most_common()[:10]]
print('Cipher letters: ', ' '.join(letters))
print('English letters:', 'e t a o i n s r h d')
# ____________________________________________________________
# Main
if len(sys.argv) < 2:
crypt = ' '.join(sys.argv[1:])
swaps = []
soln = crypt
while True:
inp = input('> ')
except (EOFError, KeyboardInterrupt):
print('Have a great rest of your day! :)')
if inp == 'r':
print(f'Original cryptogram:\n{crypt}')
crypt = input('Replacement cryptogram: ')
soln = crypt
for pair in swaps:
soln = swap(soln, pair[0], pair[1])
elif inp == 'f':
elif inp == 'q':
print('Have a great day! :D')
elif len(inp) == 2:
soln = swap(soln, inp[0], inp[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment