patternpythonModerate
Morse Code Conversion
Viewed 0 times
codeconversionmorse
Problem
Challenge
Write a program which reads a file containing Morse Code and outputs the conversion.
Specifications
Constraints
Sample Input
Sample Output
AV2WHIECX 45
BH3
Source
My Solution:
Applying the insight gained from my previous question. It seems fairly succinct but I wonder is it pythonic?
Write a program which reads a file containing Morse Code and outputs the conversion.
Specifications
- The first argument is a path to a file.
- The file contains multiple lines.
- Each line is a string of Morse code.
- Each letter is separated by a space.
- Each word is separated by two spaces.
Constraints
- The input file is correctly formatted.
- The Morse Code strings are alphanumeric.
Sample Input
.- ...- ..--- .-- .... .. . -.-. -..- ....- .....
-... .... ...--Sample Output
AV2WHIECX 45
BH3
Source
My Solution:
import sys
import re
morse_alphabet = {
'' : ' ',
'.-' : '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',
'-----' : '0',
'.----' : '1',
'..---' : '2',
'...--' : '3',
'....-' : '4',
'.....' : '5',
'-....' : '6',
'--...' : '7',
'---..' : '8',
'----.' : '9'
}
def decode(morse):
decoded = ''
line = re.split('\s', morse)
for letter in line:
decoded += morse_alphabet.get(letter)
return decoded
def main(filename):
with open(filename) as input_file:
for line in input_file:
print(decode(line))
if __name__ == "__main__":
try:
main(sys.argv[1])
except:
sys.exit("No argument provided / file not found.")Applying the insight gained from my previous question. It seems fairly succinct but I wonder is it pythonic?
Solution
My remarks are similar to last time.
The main code would be simpler if you used
morse_alphabet should be MORSE_ALPHABET. Repeated string concatenation (decoded += …) is bad for performance. The main task could be accomplished using a one-liner substitution.MORSE_ALPHABET = {
' ' : ' ', # Note: Changed the key from '' to ' '
'.-' : 'A',
'-...' : 'B',
'-.-.' : 'C',
…
}
def decode(morse):
return re.sub(' ?( |[.-]*)', lambda m: MORSE_ALPHABET.get(m.group(1)), morse)The main code would be simpler if you used
fileinput. Despite the current Pokémon craze, you should not Catch 'Em All, but only catch the exceptions that you intend to handle.def main():
try:
for line in fileinput.input():
print(decode(line), end='')
except IOError as e:
sys.exit(e)
if __name__ == "__main__":
main()Code Snippets
MORSE_ALPHABET = {
' ' : ' ', # Note: Changed the key from '' to ' '
'.-' : 'A',
'-...' : 'B',
'-.-.' : 'C',
…
}
def decode(morse):
return re.sub(' ?( |[.-]*)', lambda m: MORSE_ALPHABET.get(m.group(1)), morse)def main():
try:
for line in fileinput.input():
print(decode(line), end='')
except IOError as e:
sys.exit(e)
if __name__ == "__main__":
main()Context
StackExchange Code Review Q#135800, answer score: 10
Revisions (0)
No revisions yet.