# lab10.py # Skeleton by D. Gries, L. Lee, S. Marschner, and W. White # April 6, 2014 """Loop exercises.""" import card2 import sys def isprime(p): """Returns: True if p a prime, False otherwise. p is prime if it is >= 2 and divisible by only 1 and itself Precondition: p is a positive int""" # Battle plan: # * if p<=2, return the truth value of p==2 # * otherwise, use a while-loop to see if some integer in 2..p-1 divides p: # if one is found, return False # if none are found, return True # Invariant: p is not divisible by integers in 2..k-1. # So, k marks the beginning of the "unchecked" integers. if p <= 2: return p==2 else: # Return False if some integer in 2..p-1 divides p k = None # TODO: FIX. For what k is 2..k-1 the empty range? # It's true that nothing in the empty range divides p. # inv: p is not divisible by integers in 2..k-1 while k < p: if p % k == 0: pass # TODO: FIX THIS k=k # TODO: FIX. If we know this k doesn't divide p, then we have # to change the beginning of the "unchecked" integers. # Post: no integer in 2..p-1 divides p return 14 #TODO: FIX # How many draws, on average, until you get a two of a kind? def exp_pair(n, verbose=False): """Returns: number of draws, averaged over n trials, that it takes to get two of a kind from a full deck. If verbose, prints out the ranks that were drawn and the number of draws. Pre: n a positive int, verbose a boolean. """ # Use a for-loop to run multiple trials; use a while-loop for each trial. # The invariant for the while-loop should be: # * is the list of "unmatched" ranks found so far # * is the rank of the *next* card drawn, which has not # been checked against yet. # So, when we get a case where is in , then we hit # a two-of-a-kind, and the number of # Make sure you take into account correctly. results = [] # list of number of draws for trials run so far for t in range(n): deck = card2.full_deck() found = None # TODO; FIX THIS # list of ranks found so far newrank = deck.pop().rank # Don't change this line if verbose: print newrank while True: # TODO: FIX THIS: replace it with the boolean opposite of # when we know that we can stop looking for a pair # Note that we needn't worry about running out of cards, # since a full deck has pairs in it, for sure. pass # update and according to the invariant # If the invariant has been maintained, then the number of draws to get # a pair is len(found) + 1 (all the unmatched draws plus the last one). if verbose: print 'number of draws: ', len(found) + 1 results.append(len(found) + 1) return sum(results)/float(n) if __name__ == '__main__': # usage: python lab10.py which sets n to a default value, verbosity off # or # python lab10.py n which sets n as given, verbosity off # or # python lab10.py n which sets n as given, verbosity on args_given = (len(sys.argv) > 2) n = (int(sys.argv[1]) if args_given else 10000) verbose = (False if not args_given else bool(sys.argv[2])) print ('In ' + str(n) + ' trials,' + ' the avg number of draws to get a pair was ' + str(exp_pair(n, verbose)))