-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseCodeDecoder.java
More file actions
237 lines (215 loc) · 8.39 KB
/
Copy pathMorseCodeDecoder.java
File metadata and controls
237 lines (215 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package kyu4;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringJoiner;
public class MorseCodeDecoder {
//4 https://www.codewars.com/kata/54b72c16cd7f5154e9000457
//6 https://www.codewars.com/kata/54b724efac3d5402db00065e
private static final Map<String, String> ALPHABET_TO_MORSE = new LinkedHashMap<>();
private static final Map<String, String> MORSE_TO_ALPHABET = new LinkedHashMap<>();
static {
ALPHABET_TO_MORSE.put("a", ".-");
ALPHABET_TO_MORSE.put("b", "-...");
ALPHABET_TO_MORSE.put("c", "-.-.");
ALPHABET_TO_MORSE.put("d", "-..");
ALPHABET_TO_MORSE.put("e", ".");
ALPHABET_TO_MORSE.put("f", "..-.");
ALPHABET_TO_MORSE.put("g", "--.");
ALPHABET_TO_MORSE.put("h", "....");
ALPHABET_TO_MORSE.put("i", "..");
ALPHABET_TO_MORSE.put("j", ".---");
ALPHABET_TO_MORSE.put("k", "-.-");
ALPHABET_TO_MORSE.put("l", ".-..");
ALPHABET_TO_MORSE.put("m", "--");
ALPHABET_TO_MORSE.put("n", "-.");
ALPHABET_TO_MORSE.put("o", "---");
ALPHABET_TO_MORSE.put("p", ".--.");
ALPHABET_TO_MORSE.put("q", "--.-");
ALPHABET_TO_MORSE.put("r", ".-.");
ALPHABET_TO_MORSE.put("s", "...");
ALPHABET_TO_MORSE.put("t", "-");
ALPHABET_TO_MORSE.put("u", "..-");
ALPHABET_TO_MORSE.put("v", "...-");
ALPHABET_TO_MORSE.put("w", ".--");
ALPHABET_TO_MORSE.put("x", "-..-");
ALPHABET_TO_MORSE.put("y", "-.--");
ALPHABET_TO_MORSE.put("z", "--..");
ALPHABET_TO_MORSE.put("1", ".----");
ALPHABET_TO_MORSE.put("2", "..---");
ALPHABET_TO_MORSE.put("3", "...--");
ALPHABET_TO_MORSE.put("4", "....-");
ALPHABET_TO_MORSE.put("5", ".....");
ALPHABET_TO_MORSE.put("6", "-....");
ALPHABET_TO_MORSE.put("7", "--...");
ALPHABET_TO_MORSE.put("8", "---..");
ALPHABET_TO_MORSE.put("9", "----.");
ALPHABET_TO_MORSE.put("0", "-----");
ALPHABET_TO_MORSE.put(" ", " ");
MORSE_TO_ALPHABET.put("a", "b");
MORSE_TO_ALPHABET.put("c", "d");
MORSE_TO_ALPHABET.put("-.-.-.", ";");
MORSE_TO_ALPHABET.put("-...-", "=");
MORSE_TO_ALPHABET.put("---", "O");
MORSE_TO_ALPHABET.put("----.", "9");
MORSE_TO_ALPHABET.put("-..-.", "/");
MORSE_TO_ALPHABET.put(".-...", "&");
MORSE_TO_ALPHABET.put("...--", "3");
MORSE_TO_ALPHABET.put(".--", "W");
MORSE_TO_ALPHABET.put("--", "M");
MORSE_TO_ALPHABET.put("--..", "Z");
MORSE_TO_ALPHABET.put(".----.", "'");
MORSE_TO_ALPHABET.put("-.-.--", "!");
MORSE_TO_ALPHABET.put("-...", "B");
MORSE_TO_ALPHABET.put("..-", "U");
MORSE_TO_ALPHABET.put(".----", "1");
MORSE_TO_ALPHABET.put("-.--.-", ")");
MORSE_TO_ALPHABET.put(".-", "A");
MORSE_TO_ALPHABET.put("-....-", "-");
MORSE_TO_ALPHABET.put("...-", "V");
MORSE_TO_ALPHABET.put("...---...", "SOS");
MORSE_TO_ALPHABET.put("-.--", "Y");
MORSE_TO_ALPHABET.put("..", "I");
MORSE_TO_ALPHABET.put("--.-", "Q");
MORSE_TO_ALPHABET.put("-.", "N");
MORSE_TO_ALPHABET.put("..---", "2");
MORSE_TO_ALPHABET.put("-....", "6");
MORSE_TO_ALPHABET.put("---...", ";");
MORSE_TO_ALPHABET.put(".-.-.", "+");
MORSE_TO_ALPHABET.put(".--.-.", "@");
MORSE_TO_ALPHABET.put("....-", "4");
MORSE_TO_ALPHABET.put("-----", "0");
MORSE_TO_ALPHABET.put(".-.-.-", ".");
MORSE_TO_ALPHABET.put("-.-.", "C");
MORSE_TO_ALPHABET.put(".", "E");
MORSE_TO_ALPHABET.put("..-.", "F");
MORSE_TO_ALPHABET.put(".---", "J");
MORSE_TO_ALPHABET.put("-.-", "K");
MORSE_TO_ALPHABET.put(".-..", "L");
MORSE_TO_ALPHABET.put(".-.", "R");
MORSE_TO_ALPHABET.put("...", "S");
MORSE_TO_ALPHABET.put("--.", "G");
MORSE_TO_ALPHABET.put("---..", "8");
MORSE_TO_ALPHABET.put("..--..", "?");
MORSE_TO_ALPHABET.put("-.--.", "(");
MORSE_TO_ALPHABET.put(".--.", "P");
MORSE_TO_ALPHABET.put(".....", "5");
MORSE_TO_ALPHABET.put("..--.-", "_");
MORSE_TO_ALPHABET.put("-..", "D");
MORSE_TO_ALPHABET.put(".-..-.", "\"");
MORSE_TO_ALPHABET.put("-", "T");
MORSE_TO_ALPHABET.put("....", "H");
MORSE_TO_ALPHABET.put("--..--", ",");
MORSE_TO_ALPHABET.put("...-..-", "$");
MORSE_TO_ALPHABET.put("--...", "7");
MORSE_TO_ALPHABET.put("-..-", "X");
MORSE_TO_ALPHABET.put("", " ");
}
public static String decodeMorse(String morseCode) {
if (morseCode == null || morseCode.isBlank()) {
return "";
}
List<String> decodedWords = new ArrayList<>();
String[] words = morseCode.trim().split("\\s{3,}");
for (String word : words) {
StringBuilder decodedWord = new StringBuilder();
for (String symbol : word.split("\\s+")) {
decodedWord.append(MORSE_TO_ALPHABET.getOrDefault(symbol, ""));
}
decodedWords.add(decodedWord.toString());
}
return String.join(" ", decodedWords).toUpperCase(Locale.ROOT);
}
public static String encode(String toMorseCode) {
if (toMorseCode == null || toMorseCode.isBlank()) {
return "";
}
List<String> encodedWords = new ArrayList<>();
for (String word : toMorseCode.trim().split("\\s+")) {
StringJoiner letters = new StringJoiner(" ");
for (char letter : word.toCharArray()) {
String morse = ALPHABET_TO_MORSE.get(String.valueOf(letter).toLowerCase(Locale.ROOT));
if (morse == null) {
throw new IllegalArgumentException("Unsupported Morse character: " + letter);
}
letters.add(morse);
}
encodedWords.add(letters.toString());
}
return String.join(" ", encodedWords);
}
public static String decodeBits(String bits) {
if (bits == null || bits.isBlank()) {
return "";
}
String signal = trimSignalZeros(bits);
if (signal.isEmpty()) {
return "";
}
int unit = checkDigits(signal);
StringBuilder result = new StringBuilder();
for (int i = 0; i < signal.length();) {
char bit = signal.charAt(i);
int end = i + 1;
while (end < signal.length() && signal.charAt(end) == bit) {
end++;
}
appendMorseRun(result, bit, (end - i) / unit);
i = end;
}
return result.toString().trim();
}
public static String trimStartZeros(String bits) {
// Only the active part from the first transmitted pulse is relevant;
// trailing leading zeros must be discarded before digit sampling.
if (bits == null) {
return "";
}
while (bits.startsWith("0")) {
bits = bits.substring(1);
}
return bits;
}
public static int checkDigits(String bits) {
// Finds the smallest consecutive run of ones/zeros across the signal.
// That minimum is the time unit used to discretize pulse lengths.
if (bits == null || bits.isBlank()) {
return 0;
}
int minRun = Integer.MAX_VALUE;
for (int i = 0; i < bits.length();) {
char bit = bits.charAt(i);
int end = i + 1;
while (end < bits.length() && bits.charAt(end) == bit) {
end++;
}
minRun = Math.min(minRun, end - i);
i = end;
}
return minRun;
}
private static String trimSignalZeros(String bits) {
int start = 0;
int end = bits.length();
while (start < end && bits.charAt(start) == '0') {
start++;
}
while (end > start && bits.charAt(end - 1) == '0') {
end--;
}
return bits.substring(start, end);
}
private static void appendMorseRun(StringBuilder result, char bit, int units) {
if (bit == '1') {
result.append(units >= 3 ? "-" : ".");
return;
}
if (units >= 7) {
result.append(" ");
} else if (units >= 3) {
result.append(" ");
}
}
}