28
28
}
29
29
30
30
31
- def generate_table (key : str ) -> [( str , str ) ]:
31
+ def generate_table (key : str ) -> list [ tuple [ str , str ] ]:
32
32
"""
33
33
>>> generate_table('marvin') # doctest: +NORMALIZE_WHITESPACE
34
34
[('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'),
@@ -60,30 +60,21 @@ def decrypt(key: str, words: str) -> str:
60
60
return encrypt (key , words )
61
61
62
62
63
- def get_position (table : [( str , str ) ], char : str ) -> ( int , int ) or ( None , None ) :
63
+ def get_position (table : tuple [ str , str ], char : str ) -> tuple [ int , int ] :
64
64
"""
65
- >>> table = [
66
- ... ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'),
67
- ... ('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'),
68
- ... ('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')]
69
- >>> get_position(table, 'A')
70
- (None, None)
65
+ >>> get_position(generate_table('marvin')[0], 'M')
66
+ (0, 12)
71
67
"""
72
- if char in table [0 ]:
73
- row = 0
74
- else :
75
- row = 1 if char in table [1 ] else - 1
76
- return (None , None ) if row == - 1 else (row , table [row ].index (char ))
68
+ # `char` is either in the 0th row or the 1st row
69
+ row = 0 if char in table [0 ] else 1
70
+ col = table [row ].index (char )
71
+ return row , col
77
72
78
73
79
- def get_opponent (table : [( str , str ) ], char : str ) -> str :
74
+ def get_opponent (table : tuple [ str , str ], char : str ) -> str :
80
75
"""
81
- >>> table = [
82
- ... ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'),
83
- ... ('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'),
84
- ... ('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')]
85
- >>> get_opponent(table, 'A')
86
- 'A'
76
+ >>> get_opponent(generate_table('marvin')[0], 'M')
77
+ 'T'
87
78
"""
88
79
row , col = get_position (table , char .upper ())
89
80
if row == 1 :
@@ -97,14 +88,16 @@ def get_opponent(table: [(str, str)], char: str) -> str:
97
88
98
89
doctest .testmod () # Fist ensure that all our tests are passing...
99
90
"""
100
- ENTER KEY: marvin
101
- ENTER TEXT TO ENCRYPT: jessica
102
- ENCRYPTED: QRACRWU
103
- DECRYPTED WITH KEY: JESSICA
91
+ Demo:
92
+
93
+ Enter key: marvin
94
+ Enter text to encrypt: jessica
95
+ Encrypted: QRACRWU
96
+ Decrypted with key: JESSICA
104
97
"""
105
- key = input ("ENTER KEY : " ).strip ()
106
- text = input ("ENTER TEXT TO ENCRYPT : " ).strip ()
98
+ key = input ("Enter key : " ).strip ()
99
+ text = input ("Enter text to encrypt : " ).strip ()
107
100
cipher_text = encrypt (key , text )
108
101
109
- print (f"ENCRYPTED : { cipher_text } " )
110
- print (f"DECRYPTED WITH KEY : { decrypt (key , cipher_text )} " )
102
+ print (f"Encrypted : { cipher_text } " )
103
+ print (f"Decrypted with key : { decrypt (key , cipher_text )} " )
0 commit comments