python字典表示摩尔斯电码_Python中的摩尔斯电码翻译器

本文介绍了如何使用Python创建摩尔斯电码翻译器。通过一个字典映射,将英文字母和数字转化为摩尔斯电码,反之亦然。在加密过程中,保持每个摩尔斯码间一个空格,单词间两个空格。解密时,根据空格来还原原文。给出了加密和解密的详细步骤以及示例代码。
摘要由CSDN通过智能技术生成

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

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

字典如下'A':'.-', 'B':'-...',

'C':'-.-.', 'D':'-..', 'E':'.',

'F':'..-.', 'G':'--.', 'H':'....',

'I':'..', 'J':'.---', 'K':'-.-',

'L':'.-..', 'M':'--', 'N':'-.',

'O':'---', 'P':'.--.', 'Q':'--.-',

'R':'.-.', 'S':'...', 'T':'-',

'U':'..-', 'V':'...-', 'W':'.--',

'X':'-..-', 'Y':'-.--', 'Z':'--..',

'1':'.----', '2':'..---', '3':'...--',

'4':'....-', '5':'.....', '6':'-....',

'7':'--...', '8':'---..', '9':'----.',

'0':'-----', ', ':'--..--', '.':'.-.-.-',

'?':'..--..', '/':'-..-.', '-':'-....-',

'(':'-.--.', ')':'-.--.-'}

示例Message is PYTHON-PROGRAM

Output is .--. -.-- - .... --- -.  -....- .--. .-. --- --. .-. .- --

算法

加密Step1: Given a string, atfirst we extract each letter from the word and match with the Morse Code dictionary, then we consider the code corresponding the letter.

Step2: Next step is to store the code into a variable. And we have to follow that one space should be maintained between every Morse code.

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':'....',

'I':'..', 'J':'.---', 'K':'-.-',

'L':'.-..', 'M':'--', 'N':'-.',

'O':'---', 'P':'.--.', 'Q':'--.-',

'R':'.-.', 'S':'...', 'T':'-',

'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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
摩尔电码是一种通过短暂的电信号或灯光表达字母和数字的编码方式。在Python可以用简单的方式实现摩尔电码的转换。 首先,我们可以定义一个摩尔电码字典,将每个字母和数字对应的摩尔电码表示存储在字典。然后,用户输入需要转换的字符串,我们可以将输入的字符串转换成大写并遍历每一个字符,根据字典映射将每个字符转换成摩尔电码的形式。 接着,我们可以用点(.)表示短暂的信号,用横线(-)表示长时间的信号,用空格表示字母之间的间隔,用两个空格表示单词之间的间隔。然后,将转换好的摩尔电码打印输出或者以灯光的形式展现出来。 下面是一个简单的Python代码示例: ```python morse_code_dict = { 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.' } def to_morse_code(message): morse_code = "" for char in message.upper(): if char != " ": if char in morse_code_dict: morse_code += morse_code_dict[char] + " " else: morse_code += " " return morse_code input_message = input("请输入需要转换的消息: ") morse_code = to_morse_code(input_message) print(f"摩尔电码: {morse_code}") ``` 通过这段简单的Python代码,我们可以输入需要转换的消息,然后得到对应的摩尔电码结果。这样就实现了一个简单的Python摩尔电码的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值