Last active
October 16, 2018 16:42
-
-
Save lovasoa/d7cf3ac314eeff3ea630072382fde721 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.stream.Stream; | |
import static java.util.stream.Collectors.joining; | |
public class Main { | |
public static String decode(String morseCode) { | |
Map<String, String> map = new HashMap<>(); | |
map.put(".-", "A"); | |
map.put("-...", "B"); | |
map.put("-.-.", "C"); | |
map.put("-..", "D"); | |
map.put(".", "E"); | |
map.put("..-.", "F"); | |
map.put("--.", "G"); | |
map.put("....", "H"); | |
map.put("..", "I"); | |
map.put(".---", "J"); | |
map.put("-.-", "K"); | |
map.put(".-..", "L"); | |
map.put("--", "M"); | |
map.put("-.", "N"); | |
map.put("---", "O"); | |
map.put(".--.", "P"); | |
map.put("--.-", "Q"); | |
map.put(".-.", "R"); | |
map.put("...", "S"); | |
map.put("-", "T"); | |
map.put("..-", "U"); | |
map.put("...-", "V"); | |
map.put(".--", "W"); | |
map.put("-..-", "X"); | |
map.put("-.--", "Y"); | |
map.put("--..", "Z"); | |
map.put(".----", "1"); | |
map.put("..---", "2"); | |
map.put("...--", "3"); | |
map.put("....-", "4"); | |
map.put(".....", "5"); | |
map.put("-....", "6"); | |
map.put("--...", "7"); | |
map.put("---..", "8"); | |
map.put("----.", "9"); | |
map.put("-----", "0"); | |
map.put("", " "); | |
return Stream.of(morseCode.trim().split(" {2,}")) | |
.map(w -> Stream.of(w.split(" ")).map(map::get).collect(joining())) | |
.collect(joining(" ")); | |
} | |
public static void main(String[] args) { | |
System.out.println(decode(" ... ... -. ")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment