33# The dictionary of Morse codes and the corresponding letters.
44
55codes = {
6- ".-" : "a" , " -..." : "b" , " -.-." : "c" , " -.." : "d" ,
7- "." : "e" , " ..-." : "f" , " --." : "g" , " ...." : "h" ,
8- ".." : "i" , " .---" : "j" , " -.-" : "k" , " .-.." : "l" ,
9- "--" : "m" , "-." : "n" , " ---" : "o" , " .--." : "p" ,
10- " --.-" : "q" , " .-." : "r" , " ..." : "s" , "-" : "t" ,
11- " ..-" : "u" , " ...-" : "v" , " .--" : "w" , " -..-" : "x" ,
12- " -.--" : "y" , " --.." : "z"
6+ '.-' : 'a' , ' -...' : 'b' , ' -.-.' : 'c' , ' -..' : 'd' ,
7+ '.' : 'e' , ' ..-.' : 'f' , ' --.' : 'g' , ' ....' : 'h' ,
8+ '..' : 'i' , ' .---' : 'j' , ' -.-' : 'k' , ' .-..' : 'l' ,
9+ '--' : 'm' , '-.' : 'n' , ' ---' : 'o' , ' .--.' : 'p' ,
10+ ' --.-' : 'q' , ' .-.' : 'r' , ' ...' : 's' , '-' : 't' ,
11+ ' ..-' : 'u' , ' ...-' : 'v' , ' .--' : 'w' , ' -..-' : 'x' ,
12+ ' -.--' : 'y' , ' --..' : 'z'
1313 }
1414
15- # Morse code is not what is known as " prefix code" so that
15+ # Morse code is not what is known as ' prefix code' so that
1616# no encoding of some character would be a prefix of the
1717# encoding of some other character. This makes the decoding
1818# ambiguous unless the Morse operator takes a noticeable
1919# short pause between the individual letters.
2020
2121# Construct a reverse dictionary from an existing dictionary
22- # with this handy dictionary comprehension.
22+ # with this handy dictionary comprehension. If multiple keys
23+ # of the original map to the same value, only the last pair
24+ # of value: key is stored in the reverse dictionary.
2325
2426codes_r = {codes [v ]: v for v in codes }
2527
2931# individual letters. Unknown characters are simply skipped
3032# in this encoding.
3133
32- def encode_morse (text , sep = "" ):
33- return sep .join ((codes_r .get (c , "" ) for c in text .lower ()))
34+ def encode_morse (text , sep = '' ):
35+ return sep .join ((codes_r .get (c , '' ) for c in text .lower ()))
3436
3537
3638# A generator function that yields all possible ways to
@@ -40,8 +42,8 @@ def encode_morse(text, sep=""):
4042# the rest of the message.
4143
4244def decode_morse (message ):
43- if message == "" :
44- yield ""
45+ if message == '' :
46+ yield ''
4547 else :
4648 for i in range (1 , min (len (message ) + 1 , 5 )):
4749 prefix = message [:i ]
@@ -51,22 +53,22 @@ def decode_morse(message):
5153 yield head + follow
5254
5355
54- if __name__ == " __main__" :
55- with open ('words_sorted.txt' , encoding = " utf-8" ) as f :
56+ if __name__ == ' __main__' :
57+ with open ('words_sorted.txt' , encoding = ' utf-8' ) as f :
5658 wordlist = [word .strip () for word in f if len (word ) < 8 ]
57- print (f" Read a list of { len (wordlist )} words." )
59+ print (f' Read a list of { len (wordlist )} words.' )
5860
5961 # Convert to set for a quick lookup of individual words.
6062 words = set (wordlist )
6163
6264 for text in sample (wordlist , 20 ):
6365 enc = encode_morse (text )
64- print (f" The word { text !r} encodes in Morse to { enc !r} ." )
65- print (f" The Morse code message { enc !r} decodes to words:" )
66+ print (f' The word { text !r} encodes in Morse to { enc !r} .' )
67+ print (f' The Morse code message { enc !r} decodes to words:' )
6668 dec = [word for word in decode_morse (enc ) if word in words ]
6769 for word in dec :
6870 print (f"{ word !r} split as { encode_morse (word , ' ' )} " )
69- print ("" )
71+ print ('' )
7072
71- # Exercise for the reader: find the word whose encoding in Morse
72- # can be decoded to largest possible number of different words .
73+ # Exercise for the reader: find the largest group of words that
74+ # all encode to the exact same series of Morse dots and dashes .
0 commit comments