kata-Decode the Morse code
关于codewars和kata
以前在网上有文章推荐一个练习代码的好网站
https://www.codewars.com/
在这个网站上提供了很多各种语言的练习题目(kata),练习都挺有意思,难度不断提高,可以很好的提高你的编码水平~ 让脑子活跃起来~ 解决问题然后升级也是很有成就感的~
百度了一下kata,解释是:
<日>(空手道的)形(即套路,练习时必须按形进行),(柔道)招数的类型;
当你完成一个kata后,还可以看到其他人的解决方案。有时候觉得,额,别人写的好好啊,好牛逼啊,我怎么没想到呢?
现在我决定每练习一个kata都记录一下,写一下从中获得的收获。
Decode the Morse code 解码莫斯电码
说明
最痛苦的就是看这些说明了。有时候问题比较复杂或者背景知识多,导致说明内容非常之多。
读懂并翻译的过程就当学英文了。
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.
在这个kata中你需要写一个简单的摩尔斯电码解码器。虽然摩尔斯电码现在大多被语音和数字数据沟通渠道取代,但它仍然在世界上的一些应用程序中有用武之处。
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编码为·−,字母Q编码为−−·−,数字1编码为·−−−。摩尔斯电码是不区分大小写的,传统上使用大写字母。当消息使用摩尔斯电码编写时,用一个空格来分隔的字符编码,用3个空间来分隔单词。例如,HEY JUDE的摩尔斯电码是:···· · −·−− ·−−− ··− −·· ·。
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.
除了字母、数字和标点符号,有一些特殊的服务代码,最臭名昭著的是国际遇险信号SOS(首次由《泰坦尼克号》发行),它的编码为···−−−···。这些特殊的代码被视为单一的特殊字符,而且通常被作为单独的单词传播(不太明白)。
Your task is to implement a function ++decodeMorse(morseCode)++, that would take the morse code as input and return a decoded human-readable string.
你的任务是实现一个函数 ==decodeMorse(morseCode)==,这个函数接收摩尔斯电码作为输入,并且返回一个解码为人类可阅读的字符串。
For example:
decodeMorse('.... . -.-- .--- ..- -.. .')
//should return "HEY JUDE"
例如:
decodeMorse('.... . -.-- .--- ..- -.. .')
//should return "HEY JUDE"
The Morse code table is preloaded for you as MORSE_CODE dictionary, feel free to use it.
In Java, the table can be accessed like this: MorseCode.get('.--'). In C#, the preloaded Dictionary can be accessed like this: MorseCode.Get('.--');.
我们已经预加载了摩尔斯电码表,并提供给你一个 MORSE_CODE 字典,可以随意使用它。
(因为kata可以选择用不同语言来写,这里我用js,忽略java和c#)
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 else the engine would simply ignore the tests, resulting in a "valid" solution.)Good luck!
After you complete this kata, you may try yourself at ++Decode the Morse code, advanced++.
(这个kata是一个系列的,共有3个)当你完成这个kata,你可以尝试[解码摩尔斯电码,进阶版]
接着我就试着写了一下
主要思路是3个空格区分一个单词,1个空格区分一个字母
所以写了两个函数,一个用来解码单词,一个用来解码字母。
写完之后也通过全部测试。
decodeMorse = function(morseCode){
morseCode = trimString(morseCode);
var morseWords = morseCode.split(' ');
var result = '';
for(var i =0;i<morseWords.length;i++){
if(i!=0){
result+=' ';
}
result+=decodeMorseWord(morseWords[i]);
}
return result;
//解析摩尔斯电码中的每个单词
function decodeMorseWord(morseWord){
var morseChars = morseWord.split(' ');
var char = '';
for(var i =0;i<morseChars.length;i++){
char+=decodeMorseChar(morseChars[i]);
}
return char;
}
//解析摩尔斯电码中的每个字符
function decodeMorseChar(morseChar){
return MORSE_CODE[morseChar];
}
//字符串去两边空格
function trimString(str){
return str.replace(/(^\s*)|(\s*$)/g,'');
}
}
提交后看了一下最佳答案:
decodeMorse = function(morseCode){
function decodeMorseLetter(letter) {
return MORSE_CODE[letter];
}
function decodeMorseWord(word) {
return word.split(' ').map(decodeMorseLetter).join('');
}
return morseCode.trim().split(' ').map(decodeMorseWord).join(' ');
}
好吧,我总是忘记使用map和join这些极其方便的方法。
还是用的不够多,写代码时要时常想着去用,去代替蛋疼的for循环和字符串拼接。
另外发现trim()函数好像不需要自己写了。查了一下,String.prototype.trim()已经是支持的了。来源
String.prototype.trim()的浏览器支持