系列的一部分,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.git
在這種狀況下,你必須寫一個簡單的莫爾斯碼譯碼器。儘管摩爾斯電碼如今已經被語音和數字數據通訊通道取代,但它仍然在世界各地的一些應用程序中使用。api
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 ···· · −·−− ·−−− ··− −·· ·
.app
莫爾斯電碼將每一個字符編碼成「點」和「劃」的序列。例如,字母A被編碼爲,字母qa編碼,數字1編碼爲。摩爾斯電碼是不區分大小寫的,傳統上是大寫字母。當這個消息是用摩斯電碼寫的,一個空格用來分隔字符代碼,3個空格用來分隔單詞。例如,在摩爾斯電碼中所寫的信息是。函數
NOTE: Extra spaces before or after the code have no meaning and should be ignored.測試
注意:在代碼以前或以後的額外空間沒有意義,應該被忽略。this
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.編碼
除了字母、數字和一些標點符號以外,還有一些特殊的服務代碼,其中最臭名昭著的是國際遇險信號求救信號(由泰坦尼克號首次發行)。這些特殊的編碼被看成單個的特殊字符,而且一般做爲單獨的單詞進行傳輸。spa
Your task is to implement a function decodeMorse(morseCode)
, that would take the morse code as input and return a decoded human-readable string.code
您的任務是實現一個函數解碼(morseCode),它將把莫爾斯代碼做爲輸入並返回一個解碼的人類可讀的字符串。blog
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 });
答案: