Skip to content

Instantly share code, notes, and snippets.

@nickponline
Created December 29, 2015 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickponline/333f6d4f9ed039d8c9a5 to your computer and use it in GitHub Desktop.
Save nickponline/333f6d4f9ed039d8c9a5 to your computer and use it in GitHub Desktop.
import collections
unique = collections.defaultdict(list)
def simulate(sample, B, N, C, K):
global unique
if len(sample) == B:
counts = [0] * N
for s in sample: counts[s] += 1
success = filter(lambda h: h >= C, counts)
success = len(success) >= K
if success:
h = map(str, counts)
h = ''.join(h)
unique[h].append(''.join(map(str, sample)))
return 1
else:
return 0
ret = 0
for i in xrange(N):
sample.append(i)
ret += simulate(sample, B, N, C, K)
sample.pop()
return ret
B = 6
N = 3
C = 2
K = 2
sample = []
print 'Total possibilities: ', N**B
print 'Number that have at least C copies of K elements: ', simulate(sample, B, N, C, K)
print 'Core problem solutions: ', len(unique.keys())
print 'Bounds warning: ', B - C*K > C
for k in sorted(unique.keys()):
print '+'.join(k), '=', B, len(unique[k]), 'combinations.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment