coupon.py


Below is the syntax highlighted version of coupon.py from §2.1 Using and Defining Functions.


#-----------------------------------------------------------------------
# coupon.py
#-----------------------------------------------------------------------

import stdio
import stdarray
import sys
import random

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

# Return a random coupon between 0 and n-1.

def getCoupon(n):
    return random.randrange(0, n)

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

# Collect coupons until getting one of each value in the range
# 0 to n-1.  Return the number of coupons collected.

def collect(n):
    found = stdarray.create1D(n, False)
    couponCount = 0
    distinctCouponCount = 0
    while distinctCouponCount < n:
        coupon = getCoupon(n)
        couponCount += 1
        if not found[coupon]:
            distinctCouponCount += 1
            found[coupon] = True
    return couponCount

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

# 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])
couponCount = collect(n)
stdio.writeln(couponCount)

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

# python coupon.py 1000
# 6456

# python coupon.py 1000
# 8815

# python coupon.py 1000000
# 16079795


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