Skip to content

Commit 9d13f58

Browse files
committed
feat: Add MorseCodeConverter new algorithm with Junit tests
1 parent 4a03f42 commit 9d13f58

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Converts text to Morse code and vice-versa.
8+
* Text to Morse code: Each letter is separated by a space and each word is separated by a pipe (|).
9+
* Example: "HELLO WORLD" -> ".... . .-.. .-.. --- | .-- --- .-. .-.. -.."
10+
*
11+
* Morse code to text: Each letter is separated by a space and each word is separated by a pipe (|).
12+
* Example: ".... . .-.. .-.. --- | .-- --- .-. .-.. -.." -> "HELLO WORLD"
13+
*
14+
* Applications: Used in radio communications and algorithmic challenges.
15+
*
16+
* @author Hardvan
17+
*/
18+
public final class MorseCodeConverter {
19+
private MorseCodeConverter() {
20+
}
21+
22+
private static final Map<Character, String> MORSE_MAP = new HashMap<>();
23+
private static final Map<String, Character> REVERSE_MAP = new HashMap<>();
24+
25+
static {
26+
MORSE_MAP.put('A', ".-");
27+
MORSE_MAP.put('B', "-...");
28+
MORSE_MAP.put('C', "-.-.");
29+
MORSE_MAP.put('D', "-..");
30+
MORSE_MAP.put('E', ".");
31+
MORSE_MAP.put('F', "..-.");
32+
MORSE_MAP.put('G', "--.");
33+
MORSE_MAP.put('H', "....");
34+
MORSE_MAP.put('I', "..");
35+
MORSE_MAP.put('J', ".---");
36+
MORSE_MAP.put('K', "-.-");
37+
MORSE_MAP.put('L', ".-..");
38+
MORSE_MAP.put('M', "--");
39+
MORSE_MAP.put('N', "-.");
40+
MORSE_MAP.put('O', "---");
41+
MORSE_MAP.put('P', ".--.");
42+
MORSE_MAP.put('Q', "--.-");
43+
MORSE_MAP.put('R', ".-.");
44+
MORSE_MAP.put('S', "...");
45+
MORSE_MAP.put('T', "-");
46+
MORSE_MAP.put('U', "..-");
47+
MORSE_MAP.put('V', "...-");
48+
MORSE_MAP.put('W', ".--");
49+
MORSE_MAP.put('X', "-..-");
50+
MORSE_MAP.put('Y', "-.--");
51+
MORSE_MAP.put('Z', "--..");
52+
53+
// Build reverse map for decoding
54+
MORSE_MAP.forEach((k, v) -> REVERSE_MAP.put(v, k));
55+
}
56+
57+
/**
58+
* Converts text to Morse code.
59+
* Each letter is separated by a space and each word is separated by a pipe (|).
60+
*
61+
* @param text The text to convert to Morse code.
62+
* @return The Morse code representation of the text.
63+
*/
64+
public static String textToMorse(String text) {
65+
StringBuilder morse = new StringBuilder();
66+
String[] words = text.toUpperCase().split(" ");
67+
for (int i = 0; i < words.length; i++) {
68+
for (char c : words[i].toCharArray()) {
69+
morse.append(MORSE_MAP.getOrDefault(c, "")).append(" ");
70+
}
71+
if (i < words.length - 1) {
72+
morse.append("| ");
73+
}
74+
}
75+
return morse.toString().trim();
76+
}
77+
78+
/**
79+
* Converts Morse code to text.
80+
* Each letter is separated by a space and each word is separated by a pipe (|).
81+
*
82+
* @param morse The Morse code to convert to text.
83+
* @return The text representation of the Morse code.
84+
*/
85+
public static String morseToText(String morse) {
86+
StringBuilder text = new StringBuilder();
87+
String[] words = morse.split(" \\| ");
88+
for (int i = 0; i < words.length; i++) {
89+
for (String code : words[i].split(" ")) {
90+
text.append(REVERSE_MAP.getOrDefault(code, '?'));
91+
}
92+
if (i < words.length - 1) {
93+
text.append(" ");
94+
}
95+
}
96+
return text.toString();
97+
}
98+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class MorseCodeConverterTest {
8+
9+
@Test
10+
public void testTextToMorse() {
11+
assertEquals(".- -...", MorseCodeConverter.textToMorse("AB"));
12+
assertEquals(".... . .-.. .-.. --- | .-- --- .-. .-.. -..", MorseCodeConverter.textToMorse("HELLO WORLD"));
13+
}
14+
15+
@Test
16+
public void testMorseToText() {
17+
assertEquals("AB", MorseCodeConverter.morseToText(".- -..."));
18+
}
19+
}

0 commit comments

Comments
 (0)