fork download
  1. def decipher(ciphertext: str, known_word: str) -> str:
  2. """
  3. Decrypts Caesar cipher by testing all possible shifts.
  4. Returns first decryption containing the exact known_word or 'levalid' if not found.
  5.  
  6. Args:
  7. ciphertext: Encrypted text to decode
  8. known_word: Exact word expected in plaintext (case-sensitive)
  9.  
  10. Returns:
  11. Decrypted text or 'levalid' if no valid decryption found
  12. """
  13.  
  14. def shift_char(c: str, shift: int) -> str:
  15. """Shift character backward in alphabet by given positions"""
  16. if c.islower():
  17. return chr((ord(c) - ord('a') - shift) % 26 + ord('a'))
  18. if c.isupper():
  19. return chr((ord(c) - ord('A') - shift) % 26 + ord('A'))
  20. return c # Non-alphabetic unchanged
  21.  
  22. # Test all possible shifts (0-25)
  23. for shift in range(26):
  24. decrypted = ''.join(shift_char(c, shift) for c in ciphertext)
  25. if known_word in decrypted.split(): # Check as whole word
  26. return decrypted
  27.  
  28. return "levalid"
  29.  
  30.  
  31. if __name__ == "__main__":
  32. try:
  33. ciphertext = input().strip()
  34. known_word = input().strip()
  35. print(decipher(ciphertext, known_word))
  36. except EOFError:
  37. print("levalid")
Success #stdin #stdout 0.07s 14116KB
stdin
Standard input is empty
stdout
levalid