Could anyone please help with one right solution? Convert morse to english with certain spaces between every letter and words. l did:
morse_eng_dict = {'.-': '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'
}
nomorse = input("Enter your Morse code here: ")
nomorse_list = nomorse.split(' ')
text = ''
morse = True
for letter in nomorse_list:
for key in morse_eng_dict.keys():
if letter == key:
text = text str(morse_eng_dict[key])
if letter == '':
text = text " "
if morse == True:
string = "".join(text)
print(string)
the problem.. Sometimes there can be not possible conversion of some coded symbols. that symbols can be displayed like " *** "
if try to put like
if letter != key:
letter = '***'
text = text str(morse_eng_dict[key] '***')
that shows *** after every doubled letter the rest of my attempts all resulted text with spaces in every certain letters.
CodePudding user response:
You can specifically check if a given key is present in the dictionary; you don't have to loop through all possible keys.
if letter in morse_eng_dict:
# letter is in the dict. handle appropriately
else:
# letter is not in the dict. handle appropriately
CodePudding user response:
A couple of notes on the code above. When trying to access the value of a dictionary instead of this...
for key in morse_eng_dict.keys():
if letter == key:
text = text str(morse_eng_dict[key])
try
if letter in morse_eng_dict:
text = text str(morse_eng_dict[letter])
This will accomplish the same thing in a much more efficient way.
If you want to only replace dots and dashes, you have a few options. One would be this...
if letter in morse_eng_dict:
text = str(morse_eng_dict[letter])
else:
text = letter
Alternatively, you can use regular expressions to match values and substitute them. In this case r'[\.-]*'
can be used to match words morse code words.
Here is a runnable example!
#!/usr/bin/env python
import re
morse_eng_dict = {'.-': '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'
}
nomorse = input("Enter your Morse code here:")
def replacer(match: re.Match):
m = match.group()
return morse_eng_dict.get(m, m)
text = re.sub(r'[\.-]*', replacer, nomorse)
morse = text != nomorse
if morse:
string = "".join(text)
print(string)
<script src="https://modularizer.github.io/pyprez/pyprez.min.js"></script>