Chapter 70
Can You Decipher The Secret Message?
Riddler Express
Break a wand of length at any points you like and multiply the lengths of the pieces. What is the largest product? Extra credit: with a wand of length ?
Solution
Suppose you cut the wand of length into pieces. By the arithmetic–geometric mean inequality their product is largest when the pieces are equal, each , giving . So the only real choice is how many pieces to make. Treating as continuous, is maximised at , and since must be a whole number we test the integers nearest .
For , , so wins (it beats ), and For , , so , and
The computation
For each candidate piece count, equal pieces are optimal, so just maximise over whole .
def best_product(L):
n = max(range(1, L + 1), key=lambda n: (L / n)**n)
return n, (L / n)**n
print(best_product(10)) # (4, 39.0625)
print(best_product(100)) # (37, 9.47e15)
Riddler Classic
In a sans-serif uppercase font, many letters are topologically equivalent (one can be continuously deformed into another without cutting or gluing). The coded message YIRTHA is equivalent, letter by letter, to exactly one English word. What is it?
Solution
Sort the letters by topological type, counting holes and loose ends. The classes are: a single arc with no holes (C, G, I, J, M, N, S, U, V, W, Z); one hole with one loose end (P, Q); one hole with two loose ends (A, R); a plain loop (D, O); two holes (B); a shape with three loose ends (E, F, T, Y); and one with four (H, K, X). Two letters decode to each other only if they share a class. Reading YIRTHA class by class, and the only six-letter English word whose letters fall in exactly these classes is
The computation
Replace each letter by its topological class, then scan the dictionary for the unique six-letter word matching the class pattern of YIRTHA.
import urllib.request
words = [w.decode().strip().upper() for w in
urllib.request.urlopen("https://norvig.com/ngrams/enable1.txt")]
classes = ['AR', 'B', 'CGIJMNSUVWZ', 'DO', 'EFTY', 'HKX', 'PQ']
def cls(ch): return next(g for g in classes if ch in g)
pat = [cls(c) for c in "YIRTHA"]
print({w for w in words if len(w) == 6
and all(c in g for c, g in zip(w, pat))}) # {'EUREKA'}