Skip to content

Instantly share code, notes, and snippets.

@ohwang
Last active December 1, 2016 05:21
Show Gist options
  • Save ohwang/9b2665aa5c2d54d175318effcca38594 to your computer and use it in GitHub Desktop.
Save ohwang/9b2665aa5c2d54d175318effcca38594 to your computer and use it in GitHub Desktop.
how many passwords?
import itertools
"""
Consider we have n sets, each contains n_i elements, sets are disjoint with each other
Q: How many distinct k arrangements are there if we want each of
the n sets to have at least one element in the arrangement ?
"""
def get_count(ns, k):
n = len(ns)
char_n = sum(ns)
result = 0
for m in range(n, 0, -1):
sign = (-1) ** (n - m)
for sets in itertools.combinations(ns, m):
result += sign * sum(sets) ** k
return result
def get_count_in_range(ns, k_min, k_max):
return sum(get_count(ns, k) for k in [k_min, k_max +1])
if __name__ == '__main__':
# lowercase, uppercase, numbers, special characters
ns = [26, 26, 10, 33]
k = 8
x = get_count()
print("Number of possible passwords in %s digits: %s" % (k, x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment