{ "cells": [ { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "
\n", "As a reminder, one of the prerequisites for this course if programming experience, especially in Python. If you do not have experience in Python specifically, we strongly recommend you go through the Codecademy Python course as soon as possible to brush up on the basics of Python.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "
Before going through this notebook, you may want to take a quick look at [7 - Debugging.ipynb](7 - Debugging.ipynb) if you haven't already for some tips on debugging your code when you get stuck.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "This is a final exercise to get you comfortable working with numpy arrays. In this exercise, we're going to be programmatically constructing truth tables from boolean vectors. A *truth table* lists all possible Boolean outputs for the given inputs. For example, an AND truth table would look like this:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
$a$$b$$a\\wedge b$\n", "
000
010
100
111
" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "Each row corresponds to different values of $a$ (first column) and $b$ (second column), and the third column corresponds to whatever $a\\wedge b$ is.\n", "\n", "We can represent the different values of $a$ and $b$ using boolean arrays in numpy:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "# create boolean vectors a and b\n", "a = np.array([0, 0, 1, 1], dtype=bool)\n", "b = np.array([0, 1, 0, 1], dtype=bool)" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "You can compute the logical AND of `a` and `b` using the `&` operator (and the symbols for the Boolean operations OR and NOT are `|` and `~`, respectively)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "# run this cell to see what happens when we use the & operator on a and b\n", "a & b" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "
Complete the functions below to produce truth tables for AND, OR, and NOT. Be sure to use the logical operators discussed above in your solution!
" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "## Part A: AND Table (1.25 points)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "and_table", "locked": false, "schema_version": 1, "solution": true } }, "outputs": [], "source": [ "def and_table(a, b):\n", " \"\"\"\n", " Takes two boolean vectors, `a` and `b`, and returns an AND truth\n", " table consisting three columns: a, b, and (a AND b). If you forget\n", " how an AND operator works, see the Wikipedia page on logical\n", " conjunctions: https://en.wikipedia.org/wiki/Logical_conjunction\n", "\n", " Parameters\n", " ----------\n", " a : boolean array with shape (n,)\n", " b : boolean array with shape (n,)\n", "\n", " Returns\n", " -------\n", " boolean array with shape (n, 3)\n", "\n", " \"\"\"\n", " ### BEGIN SOLUTION\n", " return np.array([a, b, a & b]).T\n", " ### END SOLUTION" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "After you have implemented `and_table` above, you can try running it with inputs `a` and `b`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "and_table(a, b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "# add your own test cases in this cell!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "and_correct_shape", "locked": false, "points": 0.25, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "\"\"\"Check whether and_table returns an array with the correct shape for random inputs.\"\"\"\n", "from nose.tools import assert_equal\n", "for i in range(10):\n", " # randomize the vector length\n", " n = np.random.randint(1, 11)\n", " # randomly generate binary vectors\n", " v1 = np.random.randint(0, 2, n).astype(bool)\n", " v2 = np.random.randint(0, 2, n).astype(bool)\n", " # check the shape of the AND table\n", " assert_equal(and_table(v1, v2).shape, (n, 3))\n", "\n", "print(\"Success!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "and_correct_answer", "locked": false, "points": 1.0, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "\"\"\"Check whether and_table returns the correct answer for two particular vectors.\"\"\"\n", "from numpy.testing import assert_array_equal\n", "# create the two vectors\n", "v1 = np.array([0, 0, 1, 1], dtype=bool)\n", "v2 = np.array([0, 1, 0, 1], dtype=bool)\n", "\n", "# check (v1 AND v1)\n", "v11 = np.array([[0, 0, 0], [0, 0, 0], [1, 1, 1], [1, 1, 1]], dtype=bool)\n", "assert_array_equal(and_table(v1, v1), v11)\n", "\n", "# check (v1 AND v2)\n", "v12 = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 1]], dtype=bool)\n", "assert_array_equal(and_table(v1, v2), v12)\n", "\n", "# check (v2 AND v1)\n", "v21 = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 1]], dtype=bool)\n", "assert_array_equal(and_table(v2, v1), v21)\n", "\n", "# check (v2 AND v2)\n", "v22 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 1]], dtype=bool)\n", "assert_array_equal(and_table(v2, v2), v22)\n", "\n", "print(\"Success!\")" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "## Part B: OR Table (1.25 points)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "or_table", "locked": false, "schema_version": 1, "solution": true } }, "outputs": [], "source": [ "def or_table(a, b):\n", " \"\"\"\n", " Takes two boolean vectors, `a` and `b`, and returns an OR truth\n", " table consisting three columns: a, b, and (a OR b).\n", "\n", " Parameters\n", " ----------\n", " a : boolean array with shape (n,)\n", " b : boolean array with shape (n,)\n", "\n", " Returns\n", " -------\n", " boolean array with shape (n, 3)\n", "\n", " \"\"\"\n", " ### BEGIN SOLUTION\n", " return np.array([a, b, a | b]).T\n", " ### END SOLUTION" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "After you have implemented `or_table` above, you can try running it with inputs `a` and `b`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "or_table(a, b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "# add your own test cases in this cell!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "or_correct_shape", "locked": false, "points": 0.25, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "\"\"\"Check whether or_table returns an array with the correct shape for random inputs.\"\"\"\n", "for i in range(10):\n", " # randomize the vector length\n", " n = np.random.randint(1, 11)\n", " # randomly generate binary vectors\n", " v1 = np.random.randint(0, 2, n).astype(bool)\n", " v2 = np.random.randint(0, 2, n).astype(bool)\n", " # check the shape of the OR table\n", " assert_equal(or_table(v1, v2).shape, (n, 3))\n", "\n", "print(\"Success!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "or_correct_answer", "locked": false, "points": 1.0, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "\"\"\"Check whether or_table returns the correct answer for two particular vectors.\"\"\"\n", "# create the two vectors\n", "v1 = np.array([0, 0, 1, 1], dtype=bool)\n", "v2 = np.array([0, 1, 0, 1], dtype=bool)\n", "\n", "# check (v1 OR v1)\n", "v11 = np.array([[0, 0, 0], [0, 0, 0], [1, 1, 1], [1, 1, 1]], dtype=bool)\n", "assert_array_equal(or_table(v1, v1), v11)\n", "\n", "# check (v1 OR v2)\n", "v12 = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]], dtype=bool)\n", "assert_array_equal(or_table(v1, v2), v12)\n", "\n", "# check (v2 OR v1)\n", "v21 = np.array([[0, 0, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1]], dtype=bool)\n", "assert_array_equal(or_table(v2, v1), v21)\n", "\n", "# check (v2 OR v2)\n", "v22 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 1]], dtype=bool)\n", "assert_array_equal(or_table(v2, v2), v22)\n", "\n", "print(\"Success!\")" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "## Part C: NOT Table (1.25 points)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "not_table", "locked": false, "schema_version": 1, "solution": true } }, "outputs": [], "source": [ "def not_table(a):\n", " \"\"\"\n", " Takes one boolean vector, `a`, and returns a NOT truth\n", " table consisting two columns: a and (NOT a).\n", "\n", " Parameters\n", " ----------\n", " a : boolean array with shape (n,)\n", "\n", " Returns\n", " -------\n", " boolean array with shape (n, 2)\n", "\n", " \"\"\"\n", " ### BEGIN SOLUTION\n", " return np.array([a, ~a]).T\n", " ### END SOLUTION" ] }, { "cell_type": "markdown", "metadata": { "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "source": [ "After you have implemented `not_table` above, you can try running it with inputs `a` and `b`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "not_table(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbgrader": { "grade": false, "locked": false, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "# add your own test cases in this cell!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "not_correct_shape", "locked": false, "points": 0.25, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "\"\"\"Check whether or_table returns an array with the correct shape for random inputs.\"\"\"\n", "for i in range(10):\n", " # randomize the vector length\n", " n = np.random.randint(1, 11)\n", " # randomly generate a binary vector\n", " v = np.random.randint(0, 2, n).astype(bool)\n", " # check the shape of the OR table\n", " assert_equal(not_table(v).shape, (n, 2))\n", "\n", "print(\"Success!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "not_correct_answer", "locked": false, "points": 1.0, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "\"\"\"Check whether or_table returns the correct answer for two particular vectors.\"\"\"\n", "# create the two vectors\n", "v1 = np.array([0, 0, 1, 1], dtype=bool)\n", "v2 = np.array([0, 1, 0, 1], dtype=bool)\n", "\n", "# check (NOT v1)\n", "vn1 = np.array([[0, 1], [0, 1], [1, 0], [1, 0]], dtype=bool)\n", "assert_array_equal(not_table(v1), vn1)\n", "\n", "# check (NOT v2)\n", "vn2 = np.array([[0, 1], [1, 0], [0, 1], [1, 0]], dtype=bool)\n", "assert_array_equal(not_table(v2), vn2)\n", "\n", "print(\"Success!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Create Assignment", "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.4.0" } }, "nbformat": 4, "nbformat_minor": 0 }