{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework: Caesar Cipher" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1**. Ciphers and Statistics\n", "\n", "A Caesar cipher is a very simple method of encoding and decoding data. The cipher simply replaces characters with the character offset by $k$ places. For example, if the offset is 3, we replace `a` with `d`, `b` with `e` etc. The cipher wraps around so we replace `y` with `b`, `z` with `c` and so on. Punctuation, spaces and numbers are left unchanged." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- (25 points) Write a function `encode` that takes as arguments a string and an integer offset and returns the encoded cipher." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- (5 points) Write a function `decode` that takes as arguments a cipher and an integer offset and returns the decoded string. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- (50 points) Write a function `auto_decode` that takes as argument a cipher and uses a statistical method to guess the optimal offset to decode the cipher, assuming the original string is in English which has the following letter frequency:\n", "\n", "```python\n", "freq = {\n", " 'a': 0.08167,\n", " 'b': 0.01492,\n", " 'c': 0.02782,\n", " 'd': 0.04253,\n", " 'e': 0.12702,\n", " 'f': 0.02228,\n", " 'g': 0.02015,\n", " 'h': 0.06094,\n", " 'i': 0.06966,\n", " 'j': 0.00153,\n", " 'k': 0.00772,\n", " 'l': 0.04025,\n", " 'm': 0.02406,\n", " 'n': 0.06749,\n", " 'o': 0.07507,\n", " 'p': 0.01929,\n", " 'q': 0.00095,\n", " 'r': 0.05987,\n", " 's': 0.06327,\n", " 't': 0.09056,\n", " 'u': 0.02758,\n", " 'v': 0.00978,\n", " 'w': 0.0236,\n", " 'x': 0.0015,\n", " 'y': 0.01974,\n", " 'z': 0.00074\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- (20 points) Encode the following nursery rhyme using a random offset from 10 to 20, then recover the original using `auto_decode`:\n", "\n", "```text\n", "Baa, baa, black sheep,\n", "Have you any wool?\n", "Yes, sir, yes, sir,\n", "Three bags full;\n", "One for the master,\n", "And one for the dame,\n", "And one for the little boy\n", "Who lives down the lane.\n", "```" ] }, { "cell_type": "code", "execution_count": 62, "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }