Last active
December 24, 2015 11:09
-
-
Save superjer/6788717 to your computer and use it in GitHub Desktop.
Lego buying
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
try: | |
N = int(sys.argv[1]) # number of characters, the only input! | |
assert N >= 1 | |
except: | |
N = 16 | |
print "Defaulting to", N, "characters." | |
print "You could also supply a different number as argument 1." | |
print "Let's see how many ways we can buy all", N, "characters, having bought B..." | |
print "bought (B): (ways to get all " + str(N) + ") / (ways to buy any B) = (% good)" | |
success_rate = 0.0 | |
collex = [0] * (N+1) | |
collex[1] = 1 | |
buy = 1 | |
denom = 1 | |
while success_rate < 50.0 or buy < 10: | |
new = [0] * (N+1) | |
new[1] = 1 | |
for k in range(2,N+1): | |
new[k] += collex[k-1] * (N-k+1) # ways to get a new one | |
new[k] += collex[k] * k # ways to get a duplicate | |
collex = new | |
buy += 1 | |
denom *= N | |
if buy >= N: # gotta have at least N | |
success_rate = float(collex[N]) / float(denom) * 100.0 | |
print "bought", buy, ":", collex[N], "/", denom, "=", str(success_rate) + "%" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment