2828}
2929
3030
31- def generate_table (key : str ) -> [( str , str ) ]:
31+ def generate_table (key : str ) -> list [ tuple [ str , str ] ]:
3232 """
3333 >>> generate_table('marvin') # doctest: +NORMALIZE_WHITESPACE
3434 [('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'),
@@ -60,30 +60,21 @@ def decrypt(key: str, words: str) -> str:
6060 return encrypt (key , words )
6161
6262
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 ] :
6464 """
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)
7167 """
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
7772
7873
79- def get_opponent (table : [( str , str ) ], char : str ) -> str :
74+ def get_opponent (table : tuple [ str , str ], char : str ) -> str :
8075 """
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'
8778 """
8879 row , col = get_position (table , char .upper ())
8980 if row == 1 :
@@ -97,14 +88,16 @@ def get_opponent(table: [(str, str)], char: str) -> str:
9788
9889 doctest .testmod () # Fist ensure that all our tests are passing...
9990 """
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
10497 """
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 ()
107100 cipher_text = encrypt (key , text )
108101
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