GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Python/ciphers/atbash.py at master · jusing-es/Python Expand file tree/ Copy pathatbash.pyMore file actions 66 lines (55 loc) · 1.57 KB / Copy pathatbash.pyFile metadata and controls66 lines (55 loc) · 1.57 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 """ https://en.wikipedia.org/wiki/Atbash """ import string def atbash_slow(sequence: str) -> str: """ >>> atbash_slow("ABCDEFG") 'ZYXWVUT' >>> atbash_slow("aW;;123BX") 'zD;;123YC' """ output = "" for i in sequence: extract = ord(i) if 65 <= extract <= 90: output += chr(155 - extract) elif 97 <= extract <= 122: output += chr(219 - extract) else: output += i return output def atbash(sequence: str) -> str: """ >>> atbash("ABCDEFG") 'ZYXWVUT' >>> atbash("aW;;123BX") 'zD;;123YC' """ letters = string.ascii_letters letters_reversed = string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1] return "".join( letters_reversed[letters.index(c)] if c in letters else c for c in sequence ) def benchmark() -> None: """Let's benchmark them side-by-side...""" from timeit import timeit print("Running performance benchmarks...") print( "> atbash_slow()", timeit( "atbash_slow(printable)", setup="from string import printable ; from __main__ import atbash_slow", ), "seconds", ) print( "> atbash()", timeit( "atbash(printable)", setup="from string import printable ; from __main__ import atbash", ), "seconds", ) if __name__ == "__main__": for sequence in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"): print(f"{sequence} encrypted in atbash: {atbash(sequence)}") benchmark() |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |