题目:
Part of Series 1/3
This kata is part of a series on the Morse code. After you solve this kata, you may move to the next one.
系列的一部分,1/3 这个形是莫尔斯电码系列的一部分。当你解决了这个问题后,你可能会转到下一个。
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 ···· · ?·?? ·??? ··? ?·· ·
.
莫尔斯电码将每个字符编码成“点”和“划”的序列。例如,字母A被编码为,字母qa编码,数字1编码为。摩尔斯电码是不区分大小写的,传统上是大写字母。当这个消息是用摩斯电码写的,一个空格用来分隔字符代码,3个空格用来分隔单词。例如,在摩尔斯电码中所写的信息是。
NOTE: Extra spaces before or after the code have no meaning and should be ignored.
注意:在代码之前或之后的额外空间没有意义,应该被忽略。
In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···???···
. These special codes are treated as single special characters, and usually are transmitted as separate words.
除了字母、数字和一些标点符号之外,还有一些特殊的服务代码,其中最臭名昭著的是国际遇险信号求救信号(由泰坦尼克号首次发行)。这些特殊的编码被当作单个的特殊字符,并且通常作为单独的单词进行传输。
Your task is to implement a function decodeMorse(morseCode)
, that would take the morse code as input and return a decoded human-readable string.
您的任务是实现一个函数解码(morseCode),它将把莫尔斯代码作为输入并返回一个解码的人类可读的字符串。
For example:
decodeMorse(‘.... . -.-- .--- ..- -.. .‘) //should return "HEY JUDE"
The Morse code table is preloaded for you as a dictionary, feel free to use it. In CoffeeScript, C++, JavaScript, PHP, Python, Ruby and TypeScript, the table can be accessed like this: MORSE_CODE[‘.--‘]
, in Java it is MorseCode.get(‘.--‘)
, in C# it is MorseCode.Get(‘.--‘)
, in Haskell the codes are in a Map String String
and can be accessed like this: morseCodes ! ".--"
, in Elixir it is morse_codes
variable.
摩尔斯电码表是作为一本字典预先加载的,可以随意使用它。在棺材、C++、JavaScript、PHP、Python、Ruby和打印文件中,可以像这样访问表:morsecode。“在Java中,它是morsecode……get(‘.‘),在cit中是morsecode……get(‘.‘),在Haskell中,代码在一个Map字符串字符串中,可以这样访问:morseCodes!”。——“在灵丹妙药里,它是莫西码变量。
All the test strings would contain valid Morse code, so you may skip checking for errors and exceptions. In C#, tests will fail if the solution code throws an exception, please keep that in mind. This is mostly because otherwise the engine would simply ignore the tests, resulting in a "valid" solution.
所有的测试字符串都包含有效的莫尔斯代码,因此您可以跳过检查错误和异常。在cif中,如果解决方案代码抛出一个异常,测试将失败,请记住这一点。这主要是因为否则引擎会忽略测试,从而产生一个“有效”的解决方案。
Good luck!
After you complete this kata, you may try yourself at Decode the Morse code, advanced.
当你完成了这个方型,你可以试着解码莫尔斯电码,先进。
Sample Tests:
Test.describe("Example from description", function(){ Test.assertEquals(decodeMorse(‘.... . -.-- .--- ..- -.. .‘), ‘HEY JUDE‘) }); Test.describe("Your own tests", function(){ // Add more tests here });
答案: