{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "########################################################################\n", "########################################################################\n", "# \n", "# Morse.py \n", "# write a function to return the Morse code for a text string\n", "# write a function to return the text string for a morse code string\n", "#\n", "########################################################################\n", "########################################################################\n", "\n", "# table that defines the ITU morse code standard for letters and numbers\n", "morse = { 'a' : '.-', \n", " 'b' : '-...', \n", " 'c' : '-.-.', \n", " 'd' : '-..', \n", " 'e' : '.', \n", " 'f' : '..-.', \n", " 'g' : '--.', \n", " 'h' : '....', \n", " 'i' : '..', \n", " 'j' : '.----', \n", " 'k' : '-.-', \n", " 'l' : '.-..', \n", " 'm' : '--', \n", " 'n' : '-.', \n", " 'o' : '---', \n", " 'p' : '.--.', \n", " 'q' : '--.-', \n", " 'r' : '.-.', \n", " 's' : '...', \n", " 't' : '-', \n", " 'u' : '..-', \n", " 'v' : '...-', \n", " 'w' : '.--', \n", " 'x' : '-..-', \n", " 'y' : '-.--', \n", " 'z' : '--..', \n", " '1' : '.----', \n", " '2' : '..---', \n", " '3' : '...--', \n", " '4' : '....-', \n", " '5' : '.....', \n", " '6' : '-....', \n", " '7' : '--...', \n", " '8' : '---..',\n", " '9' : '----.', \n", " '0' : '-----' }\n", "\n", "# Note: need a single space between letters and a double space between words\n", "\n", "message = \"hello world\" \n", "\n", "def textToMorse(s):\n", " \"\"\"Convert a text string s, containing letters numbers and spaces to Morse code\"\"\"\n", " code = \"\" # write your function here\n", " return code\n", "\n", "def morseToText(s):\n", " \"\"\"Convert a text string s, containing '.','-' and ' ' to text\"\"\"\n", " text = \"\" # write your function here\n", " return text\n", "\n", "print('The message is in text \"{}\"'.format(message))\n", "print('The message is in Morse code \"{}\"'.format(textToMorse(message)))\n", "print('The message is converted back to text \"{}\"'.format(morseToText(textToMorse(message))))\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "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.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }