Sequences and Cartesian Products
Sets ignore order and repetition. Often we need both: a password is a sequence of characters, a point is an ordered pair of coordinates, a database row is a tuple of field values. This lesson introduces sequences, tuples and the Cartesian product, the set of all tuples.
Sequences and tuples
A sequence (or tuple) lists elements in order and may repeat them. Write it with parentheses: . Two sequences are equal when they have the same length and the same element at each position, so although .
| Set | Sequence | |
|---|---|---|
| notation | ||
| order matters | no | yes |
| repetition | ignored | counts |
| Python | set, frozenset |
tuple, list, str |
A sequence of length 2 is an ordered pair, of length 3 a triple, of length an -tuple. The empty sequence is (or the empty string "" in code).
The Cartesian product
The Cartesian product of sets and is the set of all ordered pairs with first element from and second from :
More generally is the set of -tuples, and ( times). Sizes multiply:
from itertools import product
ranks = ["A", "K", "Q"]
suits = ["S", "H"]
cards = list(product(ranks, suits))
print(len(cards), cards)
bits3 = ["".join(t) for t in product("01", repeat=3)]
print("{0,1}^3:", bits3, len(bits3))6 [('A', 'S'), ('A', 'H'), ('K', 'S'), ('K', 'H'), ('Q', 'S'), ('Q', 'H')]
{0,1}^3: ['000', '001', '010', '011', '100', '101', '110', '111'] 8
The plane is a Cartesian product, which is where the name comes from (René Descartes). Note that in general, since .
Strings are sequences
A string over an alphabet is a finite sequence of symbols from . is the set of strings of length , and is the set of all finite strings. For a bit alphabet , . This is the basic counting fact behind password strength and brute-force attack costs:
import math
for name, size in [("digits", 10), ("lowercase", 26), ("letters+digits", 62), ("printable ASCII", 95)]:
for length in (8, 12):
count = size ** length
print(f"{name:16s} length {length:2d}: {count:.2e} strings = {math.log2(count):5.1f} bits")digits length 8: 1.00e+08 strings = 26.6 bits
digits length 12: 1.00e+12 strings = 39.9 bits
lowercase length 8: 2.09e+11 strings = 37.6 bits
lowercase length 12: 9.54e+16 strings = 56.4 bits
letters+digits length 8: 2.18e+14 strings = 47.6 bits
letters+digits length 12: 3.23e+21 strings = 71.5 bits
printable ASCII length 8: 6.63e+15 strings = 52.6 bits
printable ASCII length 12: 5.40e+23 strings = 78.8 bits
Twelve characters from the 95 printable ASCII symbols give about 79 bits of entropy (if chosen uniformly at random), far beyond brute force.
In software
Tuples are records: a row of a relational table with columns of types is an element of . A SQL CROSS JOIN computes a Cartesian product, which is why forgetting the join condition makes a query return rows. Nested loops for a in A: for b in B: visit , and their running time is iterations.
Exercises
Exercise 1: List a product
List and give its size.
Answer
; size 6.
Solution
Each of the 2 first elements pairs with each of the 3 second elements, giving pairs.
Exercise 2: Sets versus tuples
Which are true? (a) ; (b) ; (c) ; (d) .
Answer
(a), (c) and (d) are true; (b) is false.
Solution
(a) both sets are . (b) the tuples have different lengths. (c) and . (d) there is no first element to choose, so the product is empty: .
Exercise 3: License plates (code)
A plate has 3 uppercase letters followed by 4 digits. How many plates are there? How many if the letters must be different?
Answer
; with distinct letters .
Solution
A plate is an element of , and sizes multiply. With distinct letters, the second letter has 25 choices and the third 24.
print(26 ** 3 * 10 ** 4)
print(26 * 25 * 24 * 10 ** 4)175760000
156000000
Test yourself
If and , what is ?
- 9
- 20
How many binary strings of length 10 are there?
- 10
- 20
- 100
- 1024
Which SQL operation returns a Cartesian product?
UNIONCROSS JOININTERSECTGROUP BY