Created
September 3, 2018 20:22
-
-
Save yrevar/a98ffdf6e6fd9bb321ec565e1f0508b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Ref: http://code.activestate.com/recipes/578407-simple-morse-code-translator-in-python/\n", | |
"MORSE_ENC = {'A': '.-', 'B': '-...', 'C': '-.-.', \n", | |
" 'D': '-..', 'E': '.', 'F': '..-.',\n", | |
" 'G': '--.', 'H': '....', 'I': '..',\n", | |
" 'J': '.---', 'K': '-.-', 'L': '.-..',\n", | |
" 'M': '--', 'N': '-.', 'O': '---',\n", | |
" 'P': '.--.', 'Q': '--.-', 'R': '.-.',\n", | |
" 'S': '...', 'T': '-', 'U': '..-',\n", | |
" 'V': '...-', 'W': '.--', 'X': '-..-',\n", | |
" 'Y': '-.--', 'Z': '--..',\n", | |
" \n", | |
" '0': '-----', '1': '.----', '2': '..---',\n", | |
" '3': '...--', '4': '....-', '5': '.....',\n", | |
" '6': '-....', '7': '--...', '8': '---..',\n", | |
" '9': '----.',\n", | |
" \n", | |
" ' ':'/', '.':'.-.-.-', ',':'--..--',\n", | |
" ':':'---...', '?':'..--..', \"'\":'.----.',\n", | |
" '-':'-....-', '/':'-..-.', '@': '.--.-.',\n", | |
" '=':'-...-', '(':'-.--.', ')':'-.--.-',\n", | |
" '+':'.-.-.'\n", | |
" }\n", | |
"MORSE_DEC = {v:k for k,v in MORSE_ENC.items()}\n", | |
"def encode(msg, map_fn):\n", | |
" return [map_fn(c) for c in msg]\n", | |
"\n", | |
"def decode(msg, map_fn):\n", | |
" return [map_fn(c) for c in msg]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Encoded : .... . .-.. .-.. --- / .-.-.\n" | |
] | |
} | |
], | |
"source": [ | |
"msg = \"hello +\"\n", | |
"enc_msg = encode(msg, lambda c: MORSE_ENC[c.upper()])\n", | |
"print(\"Encoded :\", \" \".join(enc_msg))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"HELLO +\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"\".join(decode(enc_msg, lambda c: MORSE_DEC[c.upper()])))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "irl_python3", | |
"language": "python", | |
"name": "irl_python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment