-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorsecode.py
30 lines (29 loc) · 1.26 KB
/
morsecode.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
"""
MORSE CODE
CHALLENGE DESCRIPTION: You have received a text encoded with Morse code and want to decode it.
INPUT SAMPLE: Your program should accept as its first argument a path to a filename. Input example is the following:
.- ...- ..--- .-- .... .. . -.-. -..- ....- .....
-... .... ...--
Each letter is separated by space char, each word is separated by 2 space chars.
OUTPUT SAMPLE: Print out decoded words. E.g.
AV2WHIECX 45
BH3
"""
import sys
test_cases = open(sys.argv[1], 'r')
morse = {'.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E', '..-.': 'F', '--.': 'G', '....': 'H',
'..': 'I', '.---': 'J', '-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N', '---': 'O', '.--.': 'P',
'--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T', '..-': 'U', '...-': 'V', '.--': 'W', '-..-': 'X',
'-.--': 'Y', '--..': 'Z', '.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5',
'-....': '6', '--...': '7', '---..': '8', '----.': '9', '-----': '0'}
for test in test_cases:
words = test.split(" ")
overall = []
for word in words:
letters = word.split(" ")
word = ""
for letter in letters:
word += str(morse.get(letter.strip()))
overall.append(word)
print ' '.join(overall)
test_cases.close()