Skip to content

Instantly share code, notes, and snippets.

@superjer
Last active December 24, 2015 11:09
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 superjer/6788717 to your computer and use it in GitHub Desktop.
Save superjer/6788717 to your computer and use it in GitHub Desktop.
Lego buying
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