thermoprint-homework/jobs/division_cipher.py

62 lines
2.0 KiB
Python

import random
import unicodedata
from .base import Job
from jobs.czech_words import CZECH_WORDS
class DivisionCipherJob(Job):
def __init__(self):
self.secret = "TAJENKA"
def get_name(self):
return "TAJENKA DELENIM"
def configure(self):
print("\n--- Configure Division Cipher ---")
phrase = input("Enter secret phrase (default: Random): ").strip().upper()
raw_secret = phrase if phrase else random.choice(CZECH_WORDS)
# Remove accents to ensure mapping to A-Z works
nfkd_form = unicodedata.normalize('NFKD', raw_secret)
only_ascii = "".join([c for c in nfkd_form if not unicodedata.combining(c)])
# Keep only A-Z
self.secret = "".join([c for c in only_ascii.upper() if 'A' <= c <= 'Z'])
if not self.secret:
self.secret = "TAJENKA"
def print_body(self, p):
secret = self.secret
p.text("Vylusti tajenku!\n")
p.text("Vysledek deleni je poradi pismena\n")
p.text("v abecede (A=1, B=2, C=3...).\n\n")
problems = []
for char in secret:
# A=1, B=2...
target = ord(char) - ord('A') + 1
# Generate Dividend / Divisor = target
# Ensure divisor is small enough for mental math
divisor = random.randint(2, 9)
dividend = target * divisor
problems.append((dividend, divisor))
# Print problems
for i, (dividend, divisor) in enumerate(problems):
p.text(f"{i+1}) {dividend} : {divisor} = ___\n")
p.text("\n")
# Print slots for solution
p.text("Tajenka: " + " ".join(["___"] * len(secret)) + "\n\n")
# Print helper key
p.text("Napoveda:\n")
p.text("1=A 2=B 3=C 4=D 5=E 6=F 7=G 8=H\n")
p.text("9=I 10=J 11=K 12=L 13=M 14=N 15=O\n")
p.text("16=P 17=Q 18=R 19=S 20=T 21=U 22=V\n")
p.text("23=W 24=X 25=Y 26=Z\n")