-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecodeTheMorseCode.kt
58 lines (54 loc) · 1.51 KB
/
DecodeTheMorseCode.kt
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
package com.smlnskgmail.jaman.codewarskotlin.kyu6
// https://www.codewars.com/kata/54b724efac3d5402db00065e
class DecodeTheMorseCode(private val input: String) {
private val dictionary = hashMapOf(
".-" to "A",
"-..." to "B",
"-.-." to "C",
"-.." to "D",
"." to "E",
"..-." to "F",
"--." to "G",
"...." to "H",
".." to "I",
".---" to "J",
"-.-" to "K",
".-.." to "L",
"--" to "M",
"-." to "N",
"---" to "O",
".--." to "P",
"--.-" to "Q",
".-." to "R",
"..." to "S",
"-" to "T",
"..-" to "U",
"...-" to "V",
".--" to "W",
"-..-" to "X",
"-.--" to "Y",
"--.." to "Z",
".----" to "1",
"..---" to "2",
"...--" to "3",
"....-" to "4",
"....." to "5",
"-...." to "6",
"--..." to "7",
"---.." to "8",
"----." to "9",
"-----" to "0",
"--..--" to ",",
".-.-.-" to ".",
"..--.." to "?",
"-.-.--" to "!",
"...---..." to "SOS"
)
fun solution(): String {
return input
.split(" ")
.joinToString("") { s -> dictionary[s] ?: " " }
.replace("\\s+".toRegex(), " ")
.trim()
}
}