|
| 1 | +# Morse code conversion |
| 2 | +A small snippet to convert text into morse code. |
| 3 | +Created the core of it working on another project, but thought this could be useful for others too. |
| 4 | + |
| 5 | +## Dictionary |
| 6 | +``` |
| 7 | +CHARACTERS = {'A': '.- ', 'B': '-... ', 'C': '-.-. ', |
| 8 | + 'D': '-.. ', 'E': '. ', 'F': '..-. ', |
| 9 | + 'G': '--. ', 'H': '.... ', 'I': '.. ', |
| 10 | + 'J': '.--- ', 'K': '-.- ', 'L': '.-.. ', |
| 11 | + 'M': '-- ', 'N': '-. ', 'O': '--- ', |
| 12 | + 'P': '.--. ', 'Q': '--.- ', 'R': '.-. ', |
| 13 | + 'S': '... ', 'T': '- ', 'U': '..- ', |
| 14 | + 'V': '...- ', 'W': '.-- ', 'X': '-..- ', |
| 15 | + 'Y': '-.-- ', 'Z': '--.. ', ' ': ' ', |
| 16 | + '0': '----- ', '1': '.---- ', '2': '..--- ', |
| 17 | + '3': '...-- ', '4': '....- ', '5': '..... ', |
| 18 | + '6': '-.... ', '7': '--... ', '8': '---.. ', |
| 19 | + '9': '----. '} |
| 20 | +``` |
| 21 | + |
| 22 | +## Function |
| 23 | +``` |
| 24 | +#Input should be string |
| 25 | +def encode(msg): |
| 26 | + m_msg='' |
| 27 | + for char in msg: |
| 28 | + m_msg += CHARACTERS[char.upper()] |
| 29 | + return m_msg |
| 30 | +``` |
0 commit comments