密码术中使用了摩尔斯电码翻译器。它由塞缪尔·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

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

  1. python字典是什么的集合_Python中的字典和集合

    文章目录字典1.介绍 2.创建字典 3.字典内元素的访问 4.字典元素的添加.修改.删除 5.关于字典的其它操作 6.字典存储底层原理 集合1.介绍 2.相关操作 3.集合元素的添加 4.移除元素 字 ...

  2. python字典的特点是什么_Python中dict的特点

    dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样.而list的查找速度随着元素增加而逐渐下降. 不过dict的查找速度快不是没有代价的,dict的缺点是占用内 ...

  3. python 字典组成的列表 差集_python 中 如何 获取两个 字典组成的列表的差集?

    由于两个list各包含了多个dict 假设: list_before = [dict1{},dict2{}] list_now = [dict3{},dict4{}] 那么存在可能 dict1['Us ...

  4. python字典的常用方法有哪些_python中字典常用方法

    # -*- coding: utf-8 -*- """ Created on Fri Jul 24 09:37:44 2020 答疑: 李立宗 lilizong@Gmai ...

  5. Python基础_第3章_Python中的循环结构

    Python基础_第3章_Python中的循环结构 文章目录 Python基础_第3章_Python中的循环结构 Python中的循环结构 一.回顾分支练习题 1.判断是否为一个合法三角形 2.求世界 ...

  6. Python基础_第5章_Python中的数据序列

    Python基础_第5章_Python中的数据序列 文章目录 Python基础_第5章_Python中的数据序列 Python中的数据序列 一.字典--Python中的==查询==神器 1.为什么需要 ...

  7. python 字典的值是列表_python实现求和python如何通过列表中字典的值对列表进行排序...

    一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} 1. sorted_x = sorted(x.iteritems(), key=operator.item ...

  8. python 字典由值找键_python字典怎么根据值返回键

    迭代的过程中如果没有发生对字典的修改,那么.keys() and .values 这两个函数返回的 dict-view对象总是保持对应关系.下面是python字典如何根据值返回键的相关介绍. > ...

  9. python 获取用户的一个输入值_Python中,用于获取用户输入的命令为:

    [多选题]以下关于机器学习说法正确的是? [判断题]Python内置函数sum____用来返回数值型序列中所有元素之和. [单选题]关于自定义函数的下列说法不正确的是: [判断题]Python内置函数 ...

最新文章

  1. python程序不出结果_超详细的Python入门教程,1小时就可以学会
  2. aliyun服务器安装nc工具
  3. [转载]用 grub2 启动 clover.iso 来启动 OS X
  4. RHEL7出现tkinter.TclError: no display name and no $DISPLAY environment variable
  5. C++中使用流读取数据 ifstream
  6. 接上文 ,解决 虚拟机VM,U盘(磁盘)装有系统,将其中系统安装到另外一块磁盘的具体操作
  7. idea项目结构树状展示_「软件项目管理入门」(26)如何做功能结构设计?
  8. Windows Server 2008 R2 Server Core文件操作命令
  9. 部分拆解笔记本电脑(联想y580)
  10. excel VBA 编程,数据处理,并画图,详细代码,加解释
  11. DATEUTIL计算时间进度
  12. 【软件测试的重要性】
  13. 滴滴6月或发布造车计划;头部App上线一键关闭 “个性化推荐 ”​;下载捆绑,“高速下载”竟为元凶 | EA周报...
  14. 计算机中职作文,中职作文题目
  15. 一台计算机怎样介绍自己,一台电脑的自我介绍作文
  16. REVIT建模步骤中:绘制形状不能拾取两条参照平面的交点解决方法
  17. 结构型设计模式(七种)
  18. Intel汇编-JMP无条件调转
  19. 我的世界服务器修改生成怪物速度的文件,我的世界服务器怎么不生成怪物_禁止服务器产生怪物代码大全_游戏城...
  20. 西方使用计算机的态度,对于东方科学采取粗暴态度的人,对于西方文明言过其实的人,大概...阅读答案...

热门文章

  1. Python之数据载入、存储及文件格式
  2. 千锋逆战班学员教你从零基础了解HTML5的知识
  3. Android 4.0 Launcher源码详细分析 傻蛋
  4. MySQL连接查询练习
  5. Ural 1998 The old Padawan(二分)
  6. Exp10 Final 类CTF(Webug3.0漏洞靶场—渗透基础)
  7. 05-kubernetes Pod控制器应用进阶
  8. 沭阳学爬虫01HTTP基本原理
  9. CS 188 Project4(RL) Introduction:Ghostbusters
  10. 使用Echarts实现圆环图