密码术中使用了摩尔斯电码翻译器。它由塞缪尔·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. 用leangoo做阶段式游戏新产品研发
  2. 成功解决TypeError: Encoders require their input to be uniformly strings or numbers. Got [‘float‘, ‘int‘,
  3. html页面手机端console,GitHub - MobileHTML5/vConsole: 一个针对手机网页的前端 console 调试面板。...
  4. Spark _05Standalone模式两种提交任务方式
  5. 拦截地址栏参数_selenium操作chrome时的配置参数
  6. mysql数据超10亿条,大型主键:超过10亿行MySQL + InnoDB?
  7. [TFS] 使用技巧
  8. sql取日期的年月_机油检测美版银美孚5W30,生产日期20年1月
  9. WPF入门(三):简单绑定 - 绑定到页面元素
  10. 我们能用RNN写策略吗?
  11. .NetCore 利用反射处理RESTful的更新自动赋值
  12. uva 10308 Roads in the North
  13. 应用化工技术学计算机不,化工技术类包括哪些专业
  14. 保存网页为图片——滚动截取IE(WebBrowse)
  15. 计算机论文的研究思路与方法,计算机毕业论文开题报告教学网站的设计与实现...
  16. AI、量子计算引爆硬科技创新,雷鸣、王海峰、施尧耘等北大120周年论道信科最前沿...
  17. 阿里内核月报2014年12月
  18. 使用百度地图api 报错 A parser-blocking, cross site
  19. 呆呆和你谈谈入职CVTE一个月的感受
  20. Selenium自动化下载文件Firefox配置教程

热门文章

  1. mysql 一闪就退_MySQL 一闪退出解决
  2. Windows编程语言VBA学习(二)——VBA基础
  3. 未能加载文件或程序集“System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依
  4. 必读论文|百篇最值得一读的“认知图谱”经典论
  5. 计算机程序式版面,版式设计基本程序!与版式基本形式
  6. 【 1小时打通你的英语任督二脉—雪梨】—荔枝直播总结分享
  7. 低头玩手机 不如抬头说英语
  8. Objective-C 内存管理之dealloc方法中变量释放处理
  9. c语言内存和文件处理有关知识
  10. MATLAB微分和导数