# Words by the Numbers 4, Exercise 3: how often does shuffling produce a pair as often as the real text? import random, re from collections import Counter from pydoc_data.topics import topics def tokens(text): return re.findall(r"[a-z]+(?:'[a-z]+)?", text.lower()) docs = [tokens(doc) for doc in topics.values()] real = Counter() for w in docs: real.update(zip(w, w[1:])) PAIRS = [("floating", "point"), ("except", "clause"), ("can", "be"), ("is", "not")] TRIALS = 20 beaten = Counter() best = Counter() for seed in range(TRIALS): random.seed(seed) shuffled = Counter() for w in docs: w = w[:] random.shuffle(w) shuffled.update(zip(w, w[1:])) for pair in PAIRS: best[pair] = max(best[pair], shuffled[pair]) if shuffled[pair] >= real[pair]: beaten[pair] += 1 for pair in PAIRS: print(f"{' '.join(pair):15} real {real[pair]:4d} | best of {TRIALS} shuffles {best[pair]:4d} | shuffles that matched it: {beaten[pair]}") # Output when run (Python 3.14; the documentation-text figures depend on the Python version): # floating point real 36 | best of 20 shuffles 2 | shuffles that matched it: 0 # except clause real 48 | best of 20 shuffles 5 | shuffles that matched it: 0 # can be real 193 | best of 20 shuffles 5 | shuffles that matched it: 0 # is not real 167 | best of 20 shuffles 22 | shuffles that matched it: 0