def encode( text, code_table, usecaps=True, insep="", outsep=" "):
for x in text:
if x != outsep:
insep += code_table[x] + outsep
return(code_table[x])
print(encode("Hello World", {'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': '----.'
}))
Hello there, can pls someone explain me how exactly I can make a solution to get the full "Hello World" encrypted into the morse code?
89 Views Asked by MkMs m AtThere are 6 best solutions below
On
You should try to explain in what way your code is not working. But, I suspect the problem is that your return statement is indented so that it is inside the for loop. This means that the function will return after processing the first character. You should unindent the return so that it will execute after the for loop is complete.
On
Here's a simple way to do it. You can iterate over the hw string and check to see if each letter is a key in morse_dict. I added the .upper() string method to account for the string containing a mix of upper and lowercase letters, while all the dict keys are uppercase.
morse_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': '----.'
}
hw = 'Hello World'
for x in hw:
if x.upper() in morse_dict:
print(x, morse_dict[x.upper()])
else:
print(' ')
H ....
e .
l .-..
l .-..
o ---
W .--
o ---
r .-.
l .-..
d -..
On
Another way to accomplish this
#!/usr/bin/env python
code_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': '--..',
'1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....',
'7': '--...', '8': '---..', '9': '----.',
'0': '-----', ', ': '--..--', '.': '.-.-.-',
'?': '..--..', '/': '-..-.', '-': '-....-',
'(': '-.--.', ')': '-.--.-', " ": " "
}
def encode(text, code_dictionary):
# Create empty results variable to be returned once finished processing
results = ""
# For each in character in text.upper(). This will make the text uppercase so we can match our dictionary
for character in text.upper():
# This will append the value (morris code) from the character (key) from the morris code dictionary defined above
results += code_dictionary[character] + " "
return results
#print the results of the definition
print(encode("Hello World", code_dict))
What you are trying to accomplish is looking up each letter within a dictionary. More info on Dictionaries can also be found here
Note I added a white space to the code_dict variable so we can match spaces with spaces when evaluating within your encode definition.
There are probably a few thousand ways to accomplish what you are trying to do. This will help you understand how looking up key's within dictionaries, appending them to a variable using a for loop.
On
Ok ty already for the help, it was my first question here and I am pretty new to python and kinda struggle with easy things, but I will take notice for future. My biggest problem is that the code need to be solved with the exact parameter. The insep and outsep should define the input and output separator and must be backed up with a default value. I can´t really build your answers in my code and struggle to solve the problem of upper the letters in my code above and to not overwrite the output with the each following letter.
On
Hrm... Maybe something like this assuming the input/output separator.
#!/usr/bin/env python
code_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': '--..',
'1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....',
'7': '--...', '8': '---..', '9': '----.',
'0': '-----', ', ': '--..--', '.': '.-.-.-',
'?': '..--..', '/': '-..-.', '-': '-....-',
'(': '-.--.', ')': '-.--.-', " ": " "
}
def encode( text, code_table, usecaps=True, insep="", outsep=" "):
if usecaps:
text = text.upper()
else:
return 'use caps must be true'
for x in text:
if x != outsep:
insep += code_table[x] + outsep
return insep
#print the results of the definition
print(encode("Hello World", code_dict))
Returning code_table[x] within the for loop will only return the first match. This is due to the return breaking out of the for loop.
Best of luck to you learning python!
Here's one approach using (generator) comprehension and
dict.get; I am not sure whatinsepandoutsepare supposed to mean.Note that
code_table.get(..., c)returns the input charactercas is, ifc.upper()is not in the dictcode_table. In particular, a space character' 'will be preserved.