couponcollector.py


Below is the syntax highlighted version of couponcollector.py from §1.4 Arrays.


#-----------------------------------------------------------------------
# couponcollector.py
#-----------------------------------------------------------------------

import stdio
import sys
import stdarray
import random

# Accept integer n as a command-line argument. Write to standard
# output the number of coupons you collect before obtaining one of
# each of n types.

n = int(sys.argv[1])

count = 0
collectedCount = 0
isCollected = stdarray.create1D(n, False)

while collectedCount < n:
    # Generate another coupon.
    value = random.randrange(0, n)
    count += 1
    if not isCollected[value]:
        collectedCount += 1
        isCollected[value] = True

stdio.writeln(count)

#-----------------------------------------------------------------------

# python couponcollector.py 1000
# 8507

# python couponcollector.py 1000
# 6602

# python couponcollector.py 1000000
# 14238292


Copyright © 2000–2015, Robert Sedgewick, Kevin Wayne, and Robert Dondero.
Last updated: Fri Oct 20 20:45:16 EDT 2017.