From 3b4ace111d95882c609bec3fd8145a5682259abb Mon Sep 17 00:00:00 2001 From: Martin Valter Date: Sat, 31 Oct 2020 02:07:22 +0100 Subject: [PATCH] add morse code snippet --- snippets/python/morse_code.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 snippets/python/morse_code.md diff --git a/snippets/python/morse_code.md b/snippets/python/morse_code.md new file mode 100644 index 0000000..5d04a47 --- /dev/null +++ b/snippets/python/morse_code.md @@ -0,0 +1,30 @@ +# Morse code conversion +A small snippet to convert text into morse code. +Created the core of it working on another project, but thought this could be useful for others too. + +## Dictionary +``` +CHARACTERS = {'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': '----. '} +``` + +## Function +``` +#Input should be string +def encode(msg): + m_msg='' + for char in msg: + m_msg += CHARACTERS[char.upper()] + return m_msg +``` \ No newline at end of file