morseDecodeHelper = [
' ',
'ET',
'INAM',
'SDRGUKWO',
'HBLZFCP VX Q YJ ',
'56 7 8 94 3 2 10'
]
def morseDecode(code):
result = []
morseList = code.split(' ')
for morseChar in morseList:
length = len(morseChar)
code = 0
for i in range(length):
if morseChar[i] == '-':
code += 2 ** i
result.append(morseDecodeHelper[length][code])
return ''.join(result)
测试:
ABCDEFG = morseDecode('.- -... -.-. -.. . ..-. --.')
HIJKLMN = morseDecode('.... .. .--- -.- .-.. -- -.')
OPQRST = morseDecode('--- .--. --.- .-. ... -')
UVWXYZ = morseDecode('..- ...- .-- -..- -.-- --..')
n12345 = morseDecode('.---- ..--- ...-- ....- .....')
n67890 = morseDecode('-.... --... ---.. ----. -----')
print(ABCDEFG)
print(HIJKLMN)
print(OPQRST)
print(UVWXYZ)
print(n12345)
print(n67890)