File tree 1 file changed +15
-12
lines changed
1 file changed +15
-12
lines changed Original file line number Diff line number Diff line change 1
- def dencrypt (s : str , n : int = 13 ) -> str :
1
+ def dencrypt (s : str ) -> str :
2
2
"""
3
+ Performs ROT13 encryption/decryption on the input string `s`.
4
+
3
5
https://en.wikipedia.org/wiki/ROT13
4
6
5
7
>>> msg = "My secret bank account number is 173-52946 so don't tell anyone!!"
@@ -9,29 +11,30 @@ def dencrypt(s: str, n: int = 13) -> str:
9
11
>>> dencrypt(s) == msg
10
12
True
11
13
"""
12
- out = ""
14
+ # Validate input
15
+ assert isinstance (s , str ), "Input must be a string"
16
+
17
+ # Using list to accumulate characters for efficiency
18
+ out = []
13
19
for c in s :
14
20
if "A" <= c <= "Z" :
15
- out += chr (ord ("A" ) + (ord (c ) - ord ("A" ) + n ) % 26 )
21
+ out . append ( chr (ord ("A" ) + (ord (c ) - ord ("A" ) + 13 ) % 26 ) )
16
22
elif "a" <= c <= "z" :
17
- out += chr (ord ("a" ) + (ord (c ) - ord ("a" ) + n ) % 26 )
23
+ out . append ( chr (ord ("a" ) + (ord (c ) - ord ("a" ) + 13 ) % 26 ) )
18
24
else :
19
- out += c
20
- return out
21
-
25
+ out .append (c )
26
+ return "" .join (out )
22
27
23
28
def main () -> None :
24
29
s0 = input ("Enter message: " )
25
30
26
- s1 = dencrypt (s0 , 13 )
31
+ s1 = dencrypt (s0 )
27
32
print ("Encryption:" , s1 )
28
33
29
- s2 = dencrypt (s1 , 13 )
30
- print ("Decryption: " , s2 )
31
-
34
+ s2 = dencrypt (s1 )
35
+ print ("Decryption:" , s2 )
32
36
33
37
if __name__ == "__main__" :
34
38
import doctest
35
-
36
39
doctest .testmod ()
37
40
main ()
You can’t perform that action at this time.
0 commit comments