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