#!/usr/bin/python # 2013-01-31 sburdick # To solve a Car Talk puzzler # Goal is to distribute $1000 into 10 envelopes such that when asked for any quantity of whole dollars up to $1000, money can be distributed by paying with any combination of the 10 envelopes without changing their contents # # This test proves that distribution of 489,256,256,128,64,32,16,8,4,2,1 works. import sys # Fetch command line argument try: goal = int(sys.argv[1]) except IndexError: sys.exit("User did not supply an argument.") except ValueError: sys.exit("User's argument was not an integer.") # Validate integer given as command line argument (goal) if (goal < 1) | (goal > 1000): sys.exit("Integer was not within range of 1-1000") array = [489,256,256,128,64,32,16,8,4,2,1] remainder = goal for x in array: if remainder >= x: # print str(remainder) + " - " + str(x) + " = " + str(remainder - x) remainder -= x if remainder == 0: # sys.exit("I WON solving for " + str(goal) + "!") sys.exit() sys.exit("I LOST solving for " + str(goal) + "...")