{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The message is in text \"Cardiff CDT meeting 2017\"\n", "The message is in Morse code \"-.-. .- .-. -.. .. ..-. ..-. -.-. -.. - -- . . - .. -. --. ..--- ----- .---- --...\"\n", "The message is converted back to text \"cardiff cdt meeting 2017\"\n" ] } ], "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", "def textToMorse(s):\n", " \"\"\"Convert a text string s, containing letters numbers and spaces to Morse code\"\"\"\n", " code = \"\"\n", " for c in s: #loop over string s one character at a time\n", " if c is ' ':\n", " code += ' ' # double space between words\n", " else:\n", " code += morse[c.lower()]+' ' # single space between letters\n", " while(code[-1] == ' '):\n", " code = code[:-1] # remove last space(s) from message\n", " return code \n", "\n", "def morseToText(s):\n", " \"\"\"Convert a text string s, containing '.','-' and ' ' to text\"\"\"\n", " # note need iteritems() below if using python2\n", " revMorse = {v: k for k, v in morse.items()}\n", " text = \"\" \n", " newS = s.replace(' ',' X ') # indentify double spaces before splitting text\n", " sList = newS.split() # split on white space so have each letter and X for spaces\n", " for t in sList:\n", " if t == 'X':\n", " text += ' '\n", " else:\n", " text += revMorse[t]\n", " return text\n", "\n", "message = \"Cardiff CDT meeting 2017\" \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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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 }