美文网首页我爱编程
Decode the Morse code

Decode the Morse code

作者: Magicach | 来源:发表于2017-12-25 22:39 被阅读0次

    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 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.

    For example:

    MorseCodeDecoder.decode(".... . -.-- .--- ..- -.. .")
    //should return "HEY JUDE"

    The Morse code table is preloaded for you as a dictionary, feel free to use it. In CoffeeScript, C++, Go, 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.

    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.

    Good luck!

    After you complete this kata, you may try yourself at Decode the Morse code, advanced.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 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.

    For example:

    MorseCodeDecoder.decode(".... . -.--   .--- ..- -.. .")
    //should return "HEY JUDE"
    
    

    The Morse code table is preloaded for you as a dictionary, feel free to use it. In CoffeeScript, C++, Go, 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.

    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.

    Good luck!

    After you complete this kata, you may try yourself at Decode the Morse code, advanced.

    Good Solution 1:

    public class MorseCodeDecoder {
        public static String decode(String morseCode) {
          String result = "";
          for(String word : morseCode.trim().split("   ")) {
            for(String letter : word.split("\\s+")) {
              result += MorseCode.get(letter);
            }
            result += ' ';
          }
          return result.trim();
        }
    }
    

    Good Solution 2:

    import java.util.Arrays;
    import java.util.stream.Collectors;
    
    public class MorseCodeDecoder {
    
        public static String decode(String morseCode) {
            return Arrays.stream(morseCode.trim().split("   "))
                    .map(MorseCodeDecoder::decodeWord)
                    .collect(Collectors.joining(" "));
        }
    
        private static String decodeWord(String word) {
            return Arrays.stream(word.split(" ")).map(MorseCode::get).collect(Collectors.joining());
        }
    }
    

    相关文章

      网友评论

        本文标题:Decode the Morse code

        本文链接:https://www.haomeiwen.com/subject/mkbrgxtx.html