At My Fingertips
Rapid Playground
There is a chance you had to memorize the multiplication table in school. It probably looked something like this:
Did you know that you can use this kind of table to look at MUCH more than just the multiplication of numbers? You can look at any binary operation (multiplication is just one of these) over any set of values (numbers are just one of these).
You can look at how Boolean logic functions work on truth values, how PyTamaro functions compose graphics, how strings get concatenated, how set union works, or what the modulo operator does.
Can we develop a function that can produce all these kinds of tables? Of course!
Before we develop a generic function that can compose a table for any binary operation over any type of values, let's develop the good old multiplication table.
The table consists of four parts:
The row and column headers show the values used as operands for our binary operation, i.e., the factors we multiply in a multiplication table. The body shows the result of the operation. And the label shows the symbol or name of the operation.
Now let's create a function for each of the four parts. Let's build the entire table (including the top-left label, and the row and column headers) from cells of the same size. Let's use a black background for the row and column header cells, and a white background for the label and the body cells.
Great!
Do you see the symmetry in the table? This is because a * b == b * a; multiplication is commutative.
And can you see that the first row of the body contains the exact same values as the top header row? That's because 1 is the neutral element of multiplication: 1 * a == a. The same applies to the first column: a * 1 == a.
These are interesting algebraic properties of multiplication on natural numbers.
So we have five functions to compose a multiplication table.
Yes, we need to create two new functions,
addition_table
and the addition_table_body
,
which work like the multiplication_table
and the multiplication_table_body
functions,
but for addition.
Luckily, we do not need to change the other three functions
(table_label
, row_header
, and col_header
);
they work for any kind of operation!
So let's create the two functions needed for an addition table.
Look at the addition table and answer the following questions:
Yes, addition on natural numbers commutes; we can see the symmetry. a + b == b + a.
The neutral element of addition is 0. 0 + a == a == a + 0.
Addition and multiplication seem to share interesting properties.
The only difference is the expression we use to compute the cell values.
We use row_value * col_value
for the multiplication table,
and row_value + col_value
for the addition table.
Such a small difference! And so much code that we have to copy!
And while we're at it: what about the two header functions?
The only difference is that one uses above
and the other uses beside
to compose the header cells.
Also here, the difference is very small,
and the similarity is large.
In such a situation, it makes a lot of sense to refactor the code, so we can eliminate the code duplication. We will learn how to do so in a future activity.
You learned how to compose a multiplication table and an addition table. You saw how similar they are; most of the code for them is the same. Just a tiny part differs. In a future activity we can develop a more general function, so that it can produce a table for any binary operation. Specifically, we want to pass the binary operation to our function as an argument.
In terms of algebra, you learned about the commutative property of multiplication and addition and the neutral elements of those operations. There are many other binary operations, and we can create similar tables for any of them.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Multiplication Table
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version 1cd5229 (Tue, 05 Nov 2024 16:55:57 GMT)