Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b779ef

Browse files
author
Adrián Catalán
committed
Decode morse kata
1 parent 46b7277 commit 6b779ef

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
| 3 | [Prime Streaming (PG-13)](https://www.codewars.com/kata/5519a584a73e70fa570005f5) | [Pg13PrimeStreaming.java](https://github.com/adrianblade/codewars_java_solution/blob/master/src/main/java/kyu3/prime_streaming_pg_13/Pg13PrimeStreaming.java) |
1010
| 4 | [Finite state machine](https://www.codewars.com/kata/a-simplistic-tcp-finite-state-machine-fsm/java) | [TCP.java](https://github.com/adrianblade/codewars_java_solution/blob/master/src/main/java/kyu4/finite_state_machine/TCP.java) |
1111
| 6 | [Buying a car](https://www.codewars.com/kata/554a44516729e4d80b000012) | [BuyingCar.java](https://github.com/adrianblade/codewars_java_solution/blob/master/src/main/java/kyu6/buying_a_car/BuyingCar.java) |
12+
| 6 | [Decode morse](https://www.codewars.com/kata/54b724efac3d5402db00065e/java) | [MorseCodeDecoder.java](https://github.com/adrianblade/codewars_java_solution/blob/master/src/main/java/kyu6/decode_morse/MorseCodeDecoder.java) |
1213
| 6 | [Ranking NBA teams](https://www.codewars.com/kata/5a420163b6cfd7cde5000077) | [RankingNbaTeams.java](https://github.com/adrianblade/codewars_java_solution/blob/master/src/main/java/kyu6/nba_teams/RankingNbaTeams.java) |
1314
| 8 | [Multiply](https://www.codewars.com/kata/50654ddff44f800200000004) | [Multiply.java](https://github.com/adrianblade/codewars_java_solution/blob/master/src/main/java/kyu8/multiply/Multiply.java) |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package kyu6.decode_morse;
2+
3+
class MorseCode {
4+
static String get(String letter) {
5+
return null;
6+
}
7+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package kyu6.decode_morse;
2+
3+
import java.util.Arrays;
4+
import java.util.Objects;
5+
import java.util.stream.Collectors;
6+
7+
public class MorseCodeDecoder {
8+
9+
private static final String ONE_SPACE = " ";
10+
private static final String THREE_SPACES = " {3}";
11+
12+
public static String decode(String morseCode) {
13+
StringBuilder result = new StringBuilder();
14+
// your brilliant code here, remember that you can access the preloaded Morse code table through MorseCode.get(code)
15+
String[] words = morseCode.split(THREE_SPACES);
16+
for (String word : words) {
17+
String[] letters = word.split(ONE_SPACE);
18+
for (String letter : letters) {
19+
String translation = MorseCode.get(letter);
20+
if (translation != null) result.append(translation);
21+
}
22+
if (result.length() > 0) {
23+
result.append(ONE_SPACE);
24+
}
25+
}
26+
27+
return result.toString().trim();
28+
}
29+
30+
public static String decode2(String morseCode) {
31+
return Arrays.stream(morseCode.split(THREE_SPACES))
32+
.map(i -> Arrays.stream(i.split(ONE_SPACE))
33+
.map(MorseCode::get)
34+
.filter(Objects::nonNull)
35+
.collect(Collectors.joining()))
36+
.collect(Collectors.joining(" "))
37+
.trim();
38+
}
39+
40+
public static String decode3(String morseCode) {
41+
return Arrays.stream(morseCode.trim().split(THREE_SPACES))
42+
.map(MorseCodeDecoder::decodeWord)
43+
.collect(Collectors.joining(" "));
44+
}
45+
46+
private static String decodeWord(String morseWord) {
47+
return Arrays.stream(morseWord.trim().split(ONE_SPACE))
48+
.map(MorseCode::get)
49+
.collect(Collectors.joining());
50+
}
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Decode the Morse code
2+
Your task is to implement a function that would take the morse code as input and return a decoded human-readable string.
3+
4+
For example:
5+
6+
MorseCodeDecoder.decode `(".... . -.-- .--- ..- -.. .")`
7+
//should return `"HEY JUDE"`
8+
NOTE: For coding purposes you have to use ASCII characters . and -, not Unicode characters.
9+
10+
The Morse code table is preloaded for you as a dictionary, feel free to use it:
11+
12+
```
13+
Coffeescript/C++/Go/JavaScript/Julia/PHP/Python/Ruby/TypeScript: MORSE_CODE['.--']
14+
C#: MorseCode.Get(".--") (returns string)
15+
Elixir: @morse_codes variable (from use MorseCode.Constants). Ignore the unused variable warning for morse_codes because it's no longer used and kept only for old solutions.
16+
Elm: MorseCodes.get : Dict String String
17+
Haskell: morseCodes ! ".--" (Codes are in a Map String String)
18+
Java: MorseCode.get(".--")
19+
Kotlin: MorseCode[".--"] ?: "" or MorseCode.getOrDefault(".--", "")
20+
Rust: self.morse_code
21+
Scala: morseCodes(".--")
22+
C: provides parallel arrays, i.e. morse[2] == "-.-" for ascii[2] == "C"
23+
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.
24+
```
25+
26+
Good luck!
27+
28+
After you complete this kata, you may try yourself at Decode the Morse code, advanced.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /