Run the inclusion–exclusion over all sixteen subsets of omitted classes: print the alphabet size, the unrestricted count, the count admitted by the four-class rule, and the fraction of the space the rule keeps.
Run the inclusion–exclusion over all sixteen subsets of omitted classes: print the alphabet size, the unrestricted count, the count admitted by the four-class rule, and the fraction of the space the rule keeps.
Answer
from itertools import combinations L = 10 classes = {'lower': 26, 'upper': 26, 'digit': 10, 'special': 32} A = sum(classes.values()) total = 0 for r in range(len(classes) + 1): for omitted in combinations(classes, r): remaining = A - sum(classes[c] for c in omitted) total += (-1) ** r * remaining ** L print('alphabet size ', A) print('unrestricted A**L ', A ** L) print('at least one each ', total) print('fraction admitted ', round(total / A ** L, 4))
Stallings & Brown, Computer Security 5e, ch3 §3.2 and end-of-chapter problems