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

Pythonのモールス信号変換器

暗号技術にはモールスコード翻訳器が使用されます。それはサミュエル・FB・モ尔斯(Samuel FB Morse)に名付けられました。この技術により、メッセージを点、カンマ、「」のシリーズに変換します。-”,“ /”。

この技術は非常に単純です。それぞれの英文字母は「.」、「、」、「」のシリーズを表します。 /”,“-。メッセージからシンボルに暗号化して、シンボルから英語に復号化するだけです。

辞書は以下の通りです

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

メッセージはPYTHON-PROGRAM
出力は。--. -.-- - .... --- -.  -....- .--. .-. --- --. .-. .- --

アルゴリズム

暗号化

Step1文字列が与えられた場合、まず、単語からそれぞれの文字を抽出し、モールスコード辞書と一致させ、その文字に対応するコードを考慮します。
Step2次のステップは、コードを変数に保存することです。そして、それぞれのモールスコードの間に一つのスペースを保つ必要があります。
Step3: Two spaces should be maintained in between every word.

解密

Step1: First we add a space at the end of the string.
Step2: Now we traverse each letter of the message until space is not encountered.
Step3: When we get space then check with Morse Code Dictionary and store in a variable.
Step4: When get 2 consecutive spaces we will add another space to our variable containing the decoded string.
Step5: When get last space of the message that means this is the last letter of Morse Code Generator.

范例程式码

# -*- coding: utf-8 -*-
"""
Created on Tue Oct  2 11:21:31 2018
@author: Satyajit
"""
# Dictionary representing the morse code chart
MORSE_CODE_DICT = { 'A':'.-', 'B':'-'C':'...
   . 'D':'-.-..', 'E':'.-'F':'..
   . 'G':'-. 'H':'....',--, 'J':'.
   'I':'..---', 'K':'-.-,
   'L':'.-..', 'M':'--', 'N':'-.
   'O':'---', 'P':'.--. 'Q':'--.-,
   'R':'.-. '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()

出力結果

.--. -.-- - .... --- -.  -....- .--. .-. --- --. .-. .- --
PYTHON-PROGRAM
おすすめ