#!/usr/bin/env python3"""Python program to translate to and from Morse code.https://en.wikipedia.org/wiki/Morse_code"""# fmt: offMORSE_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": "-----", "&": ".-...", "@": ".--.-.",":": "---...", ",": "--..--", ".": ".-.-.-", "'": ".----.", '"': ".-..-.","?": "..--..", "/": "-..-.", "=": "-...-", "+": ".-.-.", "-": "-....-","(": "-.--.", ")": "-.--.-", "!": "-.-.--", " ": "/"} # Exclamation mark is not in ITU-R recommendation# fmt: onREVERSE_DICT = {value: key for key, value in MORSE_CODE_DICT.items()}def encrypt(message: str) -> str:""">>> encrypt("Sos!")'... --- ... -.-.--'>>> encrypt("SOS!") == encrypt("sos!")True"""return " ".join(MORSE_CODE_DICT[char] for char in message.upper())def decrypt(message: str) -> str:""">>> decrypt('... --- ... -.-.--')'SOS!'"""return "".join(REVERSE_DICT[char] for char in message.split())def main() -> None:""">>> s = "".join(MORSE_CODE_DICT)>>> decrypt(encrypt(s)) == sTrue"""message = "Morse code here!"print(message)message = encrypt(message)print(message)message = decrypt(message)print(message)if __name__ == "__main__":main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。