-
Notifications
You must be signed in to change notification settings - Fork 0
/
decodemorse.py
68 lines (50 loc) · 1.96 KB
/
decodemorse.py
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
import re
MORSE_CODE = {
"....": "H",
".": "E",
"-.--": "Y",
".---": "J",
"..-": "U",
"-..": "D",
" ": " ",
"": " ",
}
def decodeMorse(morse_code):
# ToDo: Accept dots, dashes and spaces, return human-readable message
############
# With Regex
# m_letters = [re.split("\s", m_word) for m_word in re.split("\s\s\s", morse_code)]
# print(m_letters)
# morse_code.split
# v = [MORSE_CODE[letter] for word in m_letters for letter in word]
# print(v)
# planets = [['Mercury', 'Venus', 'Earth'], ['Mars', 'Jupiter', 'Saturn'], ['Uranus', 'Neptune', 'Pluto']]
# Nested List comprehension with an if condition
# flatten_planets = [planet for sublist in planets for planet in sublist if len(planet) < 6]
###############
# Without Regex
# Split morse string into list of morse words: '.... . -.-- .--- ..- -.. .'.split(" ") -> ['.... . -.--', '.--- ..- -.. .']
# morse_words = morse_code.split(" ")
# Split morse string into list of morse letters: '.... . -.-- .--- ..- -.. .'.split() -> ['....', '.', '-.--', '.---', '..-', '-..', '.']
# morse_letters = morse_code.split()
# Create and replace items in list of morse letters with letters:
# mletters = " ".join(morse_code.split(" ")).split(" ")
# lst = [MORSE_CODE[letter] for letter in " ".join(morse_code.split(" ")).split(" ")]
return " ".join(
"".join(MORSE_CODE[letter] for letter in word.strip().split())
for word in morse_code.split(" ")
)
# debugging:
# print(f"original message:\t\t\t\t{morse_code}")
# print(f"Split into list of morse words:\t{morse_words}")
# print(f"split into morse letters:\t\t{mletters}")
# print(f"List:\t{lst}")
# print(f"String:\t{string}")
"""
return (
morse_code.replace(".", MORSE_CODE["."])
.replace("-", MORSE_CODE["-"])
.replace(" ", "")
)
"""
print(decodeMorse(".... . -.-- .--- ..- -.. ."))