English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Morse code translator in Python

密码术中使用了摩尔斯电码翻译器。它由塞缪尔·FB·摩尔斯(Samuel FB Morse)命名。通过这种技术,我们将消息转换为一系列的点,逗号,“-”,“ /”。

这项技术非常简单。每个英文字母表示一系列“。”,“,”,“ /”,“-。我们只是从消息到符号加密消息,然后从符号到英语解密消息。

字典如下

'A':'.-''G':'-.
''H':'....',-.-.-''J':'.
'I':'..-', ''K':'--'L':'.
..', ''M':'---', ''N':'-.-,
'O':'-''P':'.--''Q':'-.
.---''R':'.--''T':'--.-,
''S':'...-.-,
'U':'..-', ''V':'...-', ''W':'.--,
'X':'-. . . .-', ''Y':'-.--', ''Z':'--..',
'1':'.----', ''2':'..---', ''3':'...--,
'4':'....-', ''5':'.....', ''6:-....,}
'7:--..., .8:---. . . . .9:----.
0 :-----, , :--. . . .--, .:.-.-.-,
?:..--. . . . ./:-. . . .-. . .-:-....-,
(:-.--. . .)-.--.-}

示例

信息是PYTHON-PROGRAM
输出是。--. -.-- - .... --- -.  -....- .--. . .-. --- --. . .-. . .- --

算法

加密

"""1: 给定一个字符串,首先我们提取单词中的每个字母并与摩尔斯电码字典进行匹配,然后我们考虑与该字母对应的电码。
"""2: 下一步是将代码存储到一个变量中。并且我们必须遵循每个摩尔斯电码之间应该保持一个空格。
"""3Step

: When get last space of the message that means this is the last letter of Morse Code Generator.

"""1范例程式码
"""2#
"""3coding: utf
"""4Created on Tue Oct 2 @author: Satyajit
"""5# Dictionary representing the morse code chart

MORSE_CODE_DICT = {'A':'.

', ''B':' -*- ...-8 -*-
.
'C':'  2 11:21:31 2018
''D':'
.
..', ''E':'.
'F':'..-''G':'-.
   ''H':'....',-.-.-''J':'.
   'I':'..-', ''K':'--'L':'.
   ..', ''M':'---', ''N':'-.-,
   'O':'-''P':'.--''Q':'-.
   .---''R':'.--''T':'--.-,
   ''S':'...-.-,
   'U':'..-', ''V':'...-', ''W':'.--,
   'X':'-. . . .-', ''Y':'-.--', ''Z':'--..',
   '1':'.----', ''2':'..---', ''3':'...--,
   '4':'....-', ''5':'.....', ''6:-....,}
   '7:--..., .8:---. . . . .9:----.
   0 :-----, , :--. . . .--, .:.-.-.-,
   ?:..--. . . . ./:-. . . .-. . .-:-....-,
   (:-.--. . .)-.--.-'
}
def encryption(message):
   my_cipher = ''
   for myletter in message:
      if myletter != ' ':
         my_cipher += MORSE_CODE_DICT[myletter] + ' '
      else:
         my_cipher += ' '
      return my_cipher
# This function is used to decrypt
# Morse code to English
def decryption(message):
   message += ' '
   decipher = ''
   mycitext = ''
   for myletter in message:
      # checks for space
      if (myletter != ' '):
         i = 0
         mycitext += myletter
      else:
         i += 1
         if i == 2 :
            decipher += ' '
         else:
            decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
            .values()).index(mycitext)
            mycitext = ''
   return decipher
def main():
   my_message = "PYTHON"-PROGRAM"
   output = encryption(my_message.upper())
   print(output)
   my_message = "."--. -.-- - .... --- -.  -....- .--. . .-. --- --. . .-. . .- -- "
   output = decryption(my_message)
   print(output)
# Executes the main function
if __name__ == '__main__':
	main()

Output Result

.--. -.-- - .... --- -.  -....- .--. . .-. --- --. . .-. . .- --
PYTHON-PROGRAM