[codewars][Java] 翻译摩斯码

本Kata要求实现一个简单的摩斯电码解码器。摩斯电码尽管已被现代通信方式取代,但仍应用于某些场景。电码将每个字符表示为点和划的序列,如 'A' 编码为 '.-', 'B' 编码为 '-...', '1' 编码为 '.----'。电码不区分大小写,通常使用大写字母。字符间用单个空格分隔,单词间用3个空格分隔。你的任务是编写一个函数,输入摩斯电码,返回可读字符串,如 '.- ...- .-- - .. .-' 解码为 'SOS'。" 118371265,10641549,Java面试实战:时间戳转换与核心技术梳理,"['Java', '面试经验', '数据库', 'JVM内存', '网络协议', '算法']
摘要由CSDN通过智能技术生成

In this kata you have to write a simple Morse code decoder. While the Morse code is now mostly superceded by voice and digital data communication channels, it still has its use in some applications around the world.

The Morse code encodes every character as a sequence of "dots" and "dashes". For example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded as ·−−−−. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. For example, the message HEY JUDE in Morse code is ···· · −·−−   ·−−− ··− −·· ·.

NOTE: Extra spaces before or after the code have no meaning and shou

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----'} def encrypt(message): cipher = '' for letter in message: if letter != ' ': cipher += MORSE_CODE_DICT[letter] + ' ' else: cipher += ' ' return cipher # 摩斯解码 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 decrypt(message): message += ' ' decipher = '' citext = '' for letter in message: if (letter != ' '): i = 0 citext += letter else: i += 1 if i == 2 : decipher += ' ' else: decipher += MORSE_CODE_DICT[citext] citext = '' return decipher ``` 使用示例: ```python # 加密示例 message = "HELLO WORLD" result = encrypt(message.upper()) print(result) # 解密示例 message = ".... . .-.. .-.. --- .-- --- .-. .-.. -.." result = decrypt(message) print(result) ``` 相关问题: 1. 摩斯是什么? 2. 摩斯有哪些应用场景? 3. 除了字母和数字外,摩斯还可以翻译哪些符号?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值