Workbench / Information Theory Exercises
B1·02
Counting Possibilities: Bits, Logarithms and Questions
Exercises for chapter 2: the multiplication principle, orders and choices in your own words, why logarithms make bits add, fixed-length codes and their waste, blocks, question trees, the sorting bound; counting checked by listing, a decimal packer that rounds trips exactly, three sorts against log₂ n!, password policies and passphrases, storing a set in fewer bits than a list, and senior exercises on column widths, a shuffle that isn't random, and sorting a billion keys.
Start here: how these exercises work
Write your answer in the box first; hints open one at a time as you write, and the solution a couple of minutes after the last hint. Theory is in your own words, practice and programs come with real output (Python 3.13), cumulative projects use chapters 1 and 2 together, what the chapter leaves out explains what it skips, and senior exercises put the ideas into designs and reviews.
Theory
T1. Three ways to count
Explain, with your own example of each, the difference between counting sequences (), orders () and choices (). Which one counts the possible results of a race, of a raffle with 3 equal prizes, and of a 4-digit lock?
T2. Why bits add when possibilities multiply
Explain in your own words why the information in an 8-character password is 8 times the information in one character, while the number of passwords is the 8th power of the number of characters.
T3. Where the waste in a fixed-length code comes from
A fixed-length code for the 10 decimal digits uses 4 binary digits. Explain where the waste is, measure it, and explain why coding 3 digits at a time removes almost all of it.
T4. A question strategy is a code
Explain why any strategy of yes-or-no questions for finding one of things can be drawn as a binary tree, and how that tree gives a code. What do the depth of a leaf and the length of a codeword have to do with each other?
T5. The sorting bound in plain words
Explain to a programmer who knows merge sort why no comparison sort, however clever, can sort every list of different items in fewer than comparisons. Then say what kind of sort escapes the bound.
T6. Is a byte the "natural" unit?
Is the 8-bit byte the result of information theory, or a convention? Use this chapter's ideas to argue both ways.
Practice and programs
P1. By hand
(a) How many 3-letter "words" can be made from 26 letters, with repeats allowed? Without repeats? (b) How many ways can 5 people sit in a row of 5 chairs? Around a round table (where rotating everyone gives the same seating)? (c) How many binary digits does a fixed-length code need for the 64 squares of a chessboard, and for a chess piece type and colour (12 kinds)? (d) What's the minimum worst-case number of comparisons to sort 8 items?
P2. Counting, checked by listing
For , list every subset of each size with itertools.combinations and every order with itertools.permutations, and check the counts against math.comb and math.factorial, including and that the subsets total . Then print each count's bits.
P3. A decimal packer
Write encode(digits) that packs a string of decimal digits 3 at a time into 10 binary digits each (with a shorter final group: 7 digits for 2 decimal digits, 4 for 1), and decode(bits, count) that undoes it. Test on phone numbers and on 10,000 random digit strings, and compare sizes with ASCII and with 4 digits per decimal digit.
P4. Three sorts against the bound
Count the comparisons made by insertion sort and by merge sort on shuffled lists of 100, 1000 and 4000 numbers, and compare with . Check both sorts against sorted().
Cumulative projects (chapters so far: 01–02)
C1. Password policies and passphrases
Compute the information in passwords under four policies: 8 lowercase letters, 8 and 12 characters from the 94 printable symbols, and a 6-digit PIN. Then compute the information in passphrases of 3 to 6 words drawn uniformly from the different words in Shakespeare's plays. Which passphrase length matches the 8-character password? What assumption do all these numbers share, and when is it false?
C2. Storing a set in fewer bits
A system must store which 1,000 of 1,000,000 customer ids belong to a segment. Compare: a list of 20-bit ids; a bitmap of a million bits; the gaps between sorted ids at a fixed width; and the information, . Which representation is closest to the information, and why can't any representation beat it?
What the chapter leaves out
Generating functions and harder counting
The chapter counts with three formulas. Real counting problems (how many binary strings have no two 1s in a row? how many ways to make change?) need more: recurrences, generating functions and the inclusion–exclusion principle, the subject of combinatorics (the Mathematics shelf's Discrete Mathematics). The information-theoretic step is always the same: count, then take . For example, binary strings of length with no two adjacent 1s are counted by Fibonacci numbers, which grow like , so such strings carry about bits per digit, not 1.
Constrained codes
That last example is a real engineering problem. Hard disks, CDs and magnetic tape can't store arbitrary binary strings: the read head needs transitions frequently enough to stay in sync and not too often to be distinguished. Codes like the eight-to-fourteen modulation (EFM) of the CD map 8 data digits to 14 recorded ones obeying such limits. The capacity of a constraint, the bits per stored digit it permits, is of the growth rate of the number of allowed strings, a direct use of this chapter's counting. Shannon's 1948 paper treats it in its first section.
Information-theoretic lower bounds in algorithms
The sorting bound is one of a family. Searching a sorted list needs comparisons; finding the maximum needs (an "adversary argument", not just counting); merging two sorted lists of needs about ; and data structures have "succinct" versions that use close to of the number of possible structures, plus a little. Algorithm design courses (Software Engineering, Volume 1, chapter 4, and beyond) develop these.
Stirling's formula, proved
The chapter uses Stirling's approximation and the Deeper page derives its main term from an area. The full formula, with , needs calculus (Wallis's product or the Gamma function); it's proved in the Mathematics shelf's Calculus book.
Senior exercises
S1. Sizing an enumerated column
A database table of 2 billion orders stores a status column as text ("pending", "paid", "shipped", "delivered", "cancelled", "refunded"), averaging 8 bytes. (a) How many binary digits does a fixed-length code for the status need? (b) What does the column cost as text and as a 1-byte code? (c) A product manager says more statuses will be added. How do you choose the column's width, and what does chapter 2 say about it?
S2. A shuffle that isn't random
In a code review you see an online card game shuffling with random.seed(int(time.time())); random.shuffle(deck). Explain what's wrong, using information, estimate how many shuffles are possible in practice, and propose a fix.
S3. Sorting a billion keys
A nightly job sorts 1 billion 64-bit keys on one machine using a comparison sort. (a) Estimate the minimum number of comparisons. (b) At about comparisons per second per core, how long at best on one core? (c) What would you suggest, and why does it escape the estimate?