File tree 1 file changed +55
-4
lines changed
1 file changed +55
-4
lines changed Original file line number Diff line number Diff line change 1
- def atbash ():
1
+ """ https://en.wikipedia.org/wiki/Atbash """
2
+ import string
3
+
4
+
5
+ def atbash_slow (sequence : str ) -> str :
6
+ """
7
+ >>> atbash_slow("ABCDEFG")
8
+ 'ZYXWVUT'
9
+
10
+ >>> atbash_slow("aW;;123BX")
11
+ 'zD;;123YC'
12
+ """
2
13
output = ""
3
- for i in input ( "Enter the sentence to be encrypted " ). strip () :
14
+ for i in sequence :
4
15
extract = ord (i )
5
16
if 65 <= extract <= 90 :
6
17
output += chr (155 - extract )
7
18
elif 97 <= extract <= 122 :
8
19
output += chr (219 - extract )
9
20
else :
10
21
output += i
11
- print (output )
22
+ return output
23
+
24
+
25
+ def atbash (sequence : str ) -> str :
26
+ """
27
+ >>> atbash("ABCDEFG")
28
+ 'ZYXWVUT'
29
+
30
+ >>> atbash("aW;;123BX")
31
+ 'zD;;123YC'
32
+ """
33
+ letters = string .ascii_letters
34
+ letters_reversed = string .ascii_lowercase [::- 1 ] + string .ascii_uppercase [::- 1 ]
35
+ return "" .join (
36
+ letters_reversed [letters .index (c )] if c in letters else c for c in sequence
37
+ )
38
+
39
+
40
+ def benchmark () -> None :
41
+ """Let's benchmark them side-by-side..."""
42
+ from timeit import timeit
43
+
44
+ print ("Running performance benchmarks..." )
45
+ print (
46
+ "> atbash_slow()" ,
47
+ timeit (
48
+ "atbash_slow(printable)" ,
49
+ setup = "from string import printable ; from __main__ import atbash_slow" ,
50
+ ),
51
+ "seconds" ,
52
+ )
53
+ print (
54
+ "> atbash()" ,
55
+ timeit (
56
+ "atbash(printable)" ,
57
+ setup = "from string import printable ; from __main__ import atbash" ,
58
+ ),
59
+ "seconds" ,
60
+ )
12
61
13
62
14
63
if __name__ == "__main__" :
15
- atbash ()
64
+ for sequence in ("ABCDEFGH" , "123GGjj" , "testStringtest" , "with space" ):
65
+ print (f"{ sequence } encrypted in atbash: { atbash (sequence )} " )
66
+ benchmark ()
You can’t perform that action at this time.
0 commit comments