Beyondstrike
Beyondstrike

Reputation: 21

decode morse coder using string replace method

    def decodeMorse(morseCode):
x = morseCode.replace(".-", "A")
x2 = x.replace("-...","B")
x = x2.replace("-.-.", "C")
x2 = x.replace("-..", "D")
x = x2.replace(".", "E")
x2 = x.replace("..-.", "F")
x = x2.replace("..-.", "G")
x2 = x.replace("....", "H")
x = x2.replace("..", "I")
x2 = x.replace(".---", "J")
x = x2.replace("-.-", "K")
x2 = x.replace(".-..", "L")
x = x2.replace("--", "M")
x2 = x.replace("-.", "N")
x = x2.replace("---", "O")
x2 = x.replace(".--.", "P")
x = x2.replace("--.-", "Q")
x2 = x.replace(".-.", "R")
x = x2.replace("...", "S")
x2 = x.replace("-", "T")
x = x2.replace("..-", "U")
x2 = x.replace("...-", "V")
x = x2.replace(".--", "W")
x2 = x.replace("-..-", "X")
x = x2.replace("-.--", "Y")
x2 = x.replace("--..", "Z")
x = x2.replace(" ", " ")
print(x)

it replace every "." with letter E for example whenever i type ".... . -.--" it suppose to give me HEY but i get "EEEE E TAT" any help thanks!

Upvotes: 1

Views: 363

Answers (1)

Zack Tarr
Zack Tarr

Reputation: 119

You will need to separate each letter. with another character. So TED would be, "- . -.." then use split to split each letter or sting of -'s and .'s. Then replace each letter. To do back to back words you will need to split the it into a 2d list. ie sentence "AC BA CA"-> [['.-','-.-.'],['-...','.-'],['-.-.','.-']] then you would use this method but for each item in the outer cell add a space to the string.

def cnvtletter(code):
    if(code==".-"):
        return 'A'
    if(code=="-..."):
        return 'B'
    if(code=="-.-."):
        return 'C'

word= '.-|-.-.|-...'
delimiter='|'
letters=word.split(delimiter)
out=''
for letter in letters:
    out+=cnvtletter(letter)
print out

Upvotes: 1

Related Questions