Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
Kernel: Python 3
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.
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.
import numpy as np

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:

aa bb a∧ba\wedge b
0 0 0
0 1 0
1 0 0
1 1 1

Each row corresponds to different values of aa (first column) and bb (second column), and the third column corresponds to whatever a∧ba\wedge b is.

We can represent the different values of aa and bb using boolean arrays in numpy:

# create boolean vectors a and b a = np.array([0, 0, 1, 1], dtype=bool) b = np.array([0, 1, 0, 1], dtype=bool)

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).

# run this cell to see what happens when we use the & operator on a and b a & b
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!

Part A: AND Table (1.25 points)

def and_table(a, b): """ Takes two boolean vectors, `a` and `b`, and returns an AND truth table consisting three columns: a, b, and (a AND b). If you forget how an AND operator works, see the Wikipedia page on logical conjunctions: https://en.wikipedia.org/wiki/Logical_conjunction Parameters ---------- a : boolean array with shape (n,) b : boolean array with shape (n,) Returns ------- boolean array with shape (n, 3) """ ### BEGIN SOLUTION return np.array([a, b, a & b]).T ### END SOLUTION

After you have implemented and_table above, you can try running it with inputs a and b:

and_table(a, b)
# add your own test cases in this cell!
"""Check whether and_table returns an array with the correct shape for random inputs.""" from nose.tools import assert_equal for i in range(10): # randomize the vector length n = np.random.randint(1, 11) # randomly generate binary vectors v1 = np.random.randint(0, 2, n).astype(bool) v2 = np.random.randint(0, 2, n).astype(bool) # check the shape of the AND table assert_equal(and_table(v1, v2).shape, (n, 3)) print("Success!")
"""Check whether and_table returns the correct answer for two particular vectors.""" from numpy.testing import assert_array_equal # create the two vectors v1 = np.array([0, 0, 1, 1], dtype=bool) v2 = np.array([0, 1, 0, 1], dtype=bool) # check (v1 AND v1) v11 = np.array([[0, 0, 0], [0, 0, 0], [1, 1, 1], [1, 1, 1]], dtype=bool) assert_array_equal(and_table(v1, v1), v11) # check (v1 AND v2) v12 = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 1]], dtype=bool) assert_array_equal(and_table(v1, v2), v12) # check (v2 AND v1) v21 = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 1]], dtype=bool) assert_array_equal(and_table(v2, v1), v21) # check (v2 AND v2) v22 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 1]], dtype=bool) assert_array_equal(and_table(v2, v2), v22) print("Success!")

Part B: OR Table (1.25 points)

def or_table(a, b): """ Takes two boolean vectors, `a` and `b`, and returns an OR truth table consisting three columns: a, b, and (a OR b). Parameters ---------- a : boolean array with shape (n,) b : boolean array with shape (n,) Returns ------- boolean array with shape (n, 3) """ ### BEGIN SOLUTION return np.array([a, b, a | b]).T ### END SOLUTION

After you have implemented or_table above, you can try running it with inputs a and b:

or_table(a, b)
# add your own test cases in this cell!
"""Check whether or_table returns an array with the correct shape for random inputs.""" for i in range(10): # randomize the vector length n = np.random.randint(1, 11) # randomly generate binary vectors v1 = np.random.randint(0, 2, n).astype(bool) v2 = np.random.randint(0, 2, n).astype(bool) # check the shape of the OR table assert_equal(or_table(v1, v2).shape, (n, 3)) print("Success!")
"""Check whether or_table returns the correct answer for two particular vectors.""" # create the two vectors v1 = np.array([0, 0, 1, 1], dtype=bool) v2 = np.array([0, 1, 0, 1], dtype=bool) # check (v1 OR v1) v11 = np.array([[0, 0, 0], [0, 0, 0], [1, 1, 1], [1, 1, 1]], dtype=bool) assert_array_equal(or_table(v1, v1), v11) # check (v1 OR v2) v12 = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]], dtype=bool) assert_array_equal(or_table(v1, v2), v12) # check (v2 OR v1) v21 = np.array([[0, 0, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1]], dtype=bool) assert_array_equal(or_table(v2, v1), v21) # check (v2 OR v2) v22 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 1]], dtype=bool) assert_array_equal(or_table(v2, v2), v22) print("Success!")

Part C: NOT Table (1.25 points)

def not_table(a): """ Takes one boolean vector, `a`, and returns a NOT truth table consisting two columns: a and (NOT a). Parameters ---------- a : boolean array with shape (n,) Returns ------- boolean array with shape (n, 2) """ ### BEGIN SOLUTION return np.array([a, ~a]).T ### END SOLUTION

After you have implemented not_table above, you can try running it with inputs a and b:

not_table(a)
# add your own test cases in this cell!
"""Check whether or_table returns an array with the correct shape for random inputs.""" for i in range(10): # randomize the vector length n = np.random.randint(1, 11) # randomly generate a binary vector v = np.random.randint(0, 2, n).astype(bool) # check the shape of the OR table assert_equal(not_table(v).shape, (n, 2)) print("Success!")
"""Check whether or_table returns the correct answer for two particular vectors.""" # create the two vectors v1 = np.array([0, 0, 1, 1], dtype=bool) v2 = np.array([0, 1, 0, 1], dtype=bool) # check (NOT v1) vn1 = np.array([[0, 1], [0, 1], [1, 0], [1, 0]], dtype=bool) assert_array_equal(not_table(v1), vn1) # check (NOT v2) vn2 = np.array([[0, 1], [1, 0], [0, 1], [1, 0]], dtype=bool) assert_array_equal(not_table(v2), vn2) print("Success!")