test.py 800 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import logging
  2. __doc__ = """ Test hook functions. """
  3. logger = logging.getLogger(__name__)
  4. def rotate(ctx, n):
  5. """
  6. Simple character rotation.
  7. Implements the Caesar's Cypher algorithm by shifting a single
  8. [A-Za-z] character by `n` places, and wrapping around
  9. the edges.
  10. Characters not in range are not shifted.
  11. """
  12. uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  13. lc = uc.lower()
  14. logger.debug(f"cursor: {ctx.cur}")
  15. ch = ctx.src[ctx.cur]
  16. if ch in uc:
  17. idx = uc.index(ch)
  18. dest_ch = uc[(idx + n) % len(uc)]
  19. elif ch in lc:
  20. idx = lc.index(ch)
  21. dest_ch = lc[(idx + n) % len(lc)]
  22. else:
  23. dest_ch = ch
  24. logger.debug(f"ROT {n}: {ch} -> {dest_ch}")
  25. ctx.dest_ls.append(dest_ch)
  26. ctx.cur += 1
  27. return "continue"