Skip to content

Instantly share code, notes, and snippets.

@crackwitz
Created January 4, 2015 04:19
Show Gist options
  • Save crackwitz/684344749c3524d6a493 to your computer and use it in GitHub Desktop.
Save crackwitz/684344749c3524d6a493 to your computer and use it in GitHub Desktop.
morsecode
alphabet = {
' ': ' ',
'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': '----.'
}
timings = {
'.': [1],
'-': [1] * 3,
' ': [0],
}
def morsecode(message):
# inter-letter space
message = ' '.join(message)
message = ''.join(alphabet[letter] for letter in message)
message = ' '.join(message)
message = sum([timings[c] for c in message], [])
return message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment