+ 4
[Solved] Python: Dots and Dashes as Dictionary Keys
An example, the dictionay key for an "A" which is ".-" outputs an "E" and a "T" The "E" correlating to the "." and the "T" to the "-" How can you code the ".-" to output as an "A"? Here's the code in question: https://code.sololearn.com/ciWv4JKZ3RtC/?ref=app I made a morse code generator that works, (it's the base code for the problematic code above): https://code.sololearn.com/cvNH77tREW7C/?ref=app The decoder at the top is just a reversal of the generator's dictionary keys. Update: Run this code: ... - .-. .- -. --. . .-. / - .... .. -. --. ... / .---- .---- / -.- .. -.-. -.- ... / .- ... ... Here: https://code.sololearn.com/cWr30iwQr7ax/?ref=app
10 Respostas
+ 8
Your code is fine
You just need to give it correct input and handle it in a sensible way
The change i made was splitting the user input by single spaces and pass it thru the same loop you made
https://code.sololearn.com/cNE96PTO83kI/?ref=app
+ 4
Burey
Cbrâ[ Most active ]
I copied the codes you gave me. Thank you for taking the time to help me. I can learn from both answers đđ
+ 3
rodwynnejones Letters are separated by spaces and words by "/"
Thanks for your answer.
+ 2
Consider using the builtin maketrans() and translate() functions . Converting FROM text TO morse is relatively ease but the other way....oh ...not so sure...e.g. if an input is .--.-.-.-.-..---. how do you know where on character finishes and the next one starts (that's always puzzled me). You could probable use regular expression to pull the group of dots and dashes if a group was inputted with a space in between.
+ 2
In python there's a split(separator, maxsplit) method for string literals. This method serves to separate parts of a string based on a common separator. If what you want is to translate a Morse code sentence into proper English, that's relatively simple, so long as you have that separator between Morse code "Characters".
There's various references for this method on python.org, as well as here: https://www.geeksforgeeks.org/python-string-split/.
Hope this helps.
+ 1
Thanks to everyone that answered âșïžđ
0
alguien Me Puede Ayudar henryksen1913@gmail.com
0
write a function that takes multiple words as its argument and returns a concatenated version of those words separated by dashes (-).
0
def concatenate(*args):
space="-"
h=""
for i in args:
if i ==args[-1]:
h= h + i
break
else:
h= h + i + space
return h
print(concatenate("I", "love", "Python", "!"))