派森 #P123. 摩斯密码解密

描述

摩斯密码(morse code),又称摩斯电码、摩尔斯电码(莫尔斯电码),是一种时通时断的信号代码,通过不同的信号排列顺序来表达不同的英文字母、数字和标点符号;通信时,将英文字母等内容翻译成摩斯电码(摩尔斯电码)进行传输,收到摩斯密码(莫尔斯电码)后,对电码进行反翻译,得到通信的实际内容,达到加密通信内容的目的。 摩斯密码表_摩斯密码对照表:

字母 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 ━ ━ ━ ━ .

标点符号

. .━ .━ .━ : ━ ━ ━ ... , ━ ━ ..━ ━ ; ━ .━ .━ . ? ..━ ━ .. = ━ ...━ ' .━ ━ ━ ━ . / ━ ..━ . ! ━ .━ .━ ━ ━ ━ ....━ _ ..━ ━ .━ " .━ ..━ . ( ━ .━ ━ . ) ━ .━ ━ .━ $ ...━ ..━ & .━ ... @ .━ ━ .━ .

已知某摩斯密码的加密规则为:将输入的英文句子转换成摩尔斯电码并输出,其中字母、数字和标点符号按编码输出(和之前OJ中的加密规则一样),若编码表里没有的字符,原样输出,且每个摩斯码之间用一个空格分隔。 morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."] digit = ['-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..', '----.'] punctuation = {'.': '.-.-.-', ':': '---...', ',': '--..--', ';': '-.-.-.', '?': '..- -..', '=': '-...-', "'": '.----.', '/': '-..-.', '!': '-.-.--', '-': '-....-', '_': '..--.-', '"': '.-..-.', '(': '-.--.', ')': '-.--.-', '$': '...-..-', '&': '·-···', '@': '.--.-.', ' ': ''}

要求编写程序将输入接收的莫斯密码解密成真实字符。

样例

输入

输入数据 1

.... . .-.. .-.. --- --..-- .-- . .-.. -.-. --- -- .  - ---  .--. -.-- - .... --- -.

输出数据 1

hello,welcome to pyth

代码:

# 法1
str1 = input().split(' ')
morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
digit = ['-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..', '----.']
punctuation = {'.': '.-.-.-', ':': '---...', ',': '--..--', ';': '-.-.-.', '?': '..- -..', '=': '-...-', "'": '.----.', '/': '-..-.', '!': '-.-.--', '-': '-....-', '_': '..--.-', '"': '.-..-.', '(': '-.--.', ')': '-.--.-', '$': '...-..-', '&': '·-···', '@': '.--.-.', ' ': ''}
real_words = ""
for ch in str1:
    if ch in morse:
        real_words += chr(ord('a')+morse.index(ch))
    elif ch in digit:
        real_words += chr(ord('0') + digit.index(ch))
    elif ch in punctuation.values():
        real_words += list(punctuation.keys())[list(punctuation.values()).index(ch)]  # 通过value值寻找key值
    else:
        real_words += ch
print(real_words)
# 法2
morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
digit = ['-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..', '----.']
punctuation = {'.': '.-.-.-', ':': '---...', ',': '--..--', ';': '-.-.-.', '?': '..- -..', '=': '-...-', "'": '.----.', '/': '-..-.', '!': '-.-.--', '-': '-....-', '_': '..--.-', '"': '.-..-.', '(': '-.--.', ')': '-.--.-', '$': '...-..-', '&': '·-···', '@': '.--.-.', ' ': ''}
punctuationList = list(punctuation.values())
s = input()
for i in s.split(' '):
    if i in morse:
        print(chr(morse.index(i) + ord('a')), end='')
    elif i in digit:
        print(digit.index(i), end='')
    elif i in punctuationList:
        for j in punctuation:
            if punctuation[j] == i:
                print(j, end='')
    else:
        print(i, end='')

代码解析:

解析法1

  1. str1 = input().split(' '):通过input()函数获取用户输入的一行字符串,并使用split(' ')方法将其拆分成一个字符串列表。默认情况下,split()方法使用空格作为分隔符来拆分字符串。
  2. morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]:定义了一个Morse码对应的列表,其中每个字母或数字对应一个Morse码。
  3. digit = ['-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..', '----.']:定义了一个数字对应的列表,其中每个数字对应一个数字Morse码。
  4. punctuation = {...}:定义了一个包含常见标点符号及其对应Morse码的字典。
  5. real_words = "":初始化一个空字符串,用于存储转换后的真实字符串。
  6. for ch in str1::对字符串列表str1进行迭代,将迭代的值依次赋给变量ch。
  7. if ch in morse::检查变量ch是否在Morse码列表morse中。
  8. real_words += chr(ord('a')+morse.index(ch)):如果ch在Morse码列表中,通过morse.index(ch)找到其索引,然后使用chr(ord('a')+索引)将其转换为对应的字母,并添加到real_words字符串中。
  9. elif ch in digit::检查变量ch是否在数字Morse码列表digit中。
  10. real_words += chr(ord('0') + digit.index(ch)):如果ch在数字Morse码列表中,通过digit.index(ch)找到其索引,然后使用chr(ord('0')+索引)将其转换为对应的数字,并添加到real_words字符串中。
  11. elif ch in punctuation.values()::检查变量ch是否在标点符号对应的Morse码字典的值中。
  12. real_words += list(punctuation.keys())[list(punctuation.values()).index(ch)]:如果ch在标点符号Morse码字典的值中,使用list(punctuation.keys())[list(punctuation.values()).index(ch)]通过值找到对应的键,并将该键添加到real_words字符串中。
  13. else::如果ch不属于上述任何一种情况,表示ch是未转换的字符,直接将其添加到real_words字符串中。
  14. print(real_words):打印最终得到的真实字符串。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值