def marginal_tax(income, lower, upper, rate): '''calculate the marginal tax paid within a given tax bracket''' if income <= lower: return 0 elif upper == None: return (income - lower) * rate elif income > upper: return (upper - lower) * rate else: return (income - lower) * rate def effective_tax(income): '''return effective tax rate given the following marginal tax rates: 0% up to $10k 10% up to $30k 25% up to $60k 35% up to $100k 50% over $100k''' taxes = 0 taxes += marginal_tax(income, 0, 10000, 0.0) taxes += marginal_tax(income, 10000, 30000, 0.1) taxes += marginal_tax(income, 30000, 60000, 0.25) taxes += marginal_tax(income, 60000, 100000, 0.35) taxes += marginal_tax(income, 100000, None, 0.5) return taxes / income def fruit_purchase(is_ripe, on_sale): '''return the number of pieces of fruit you will purchase, given whether the fruit is ripe and/or on sale''' if is_ripe: if on_sale: return 10 else: return 5 else: # not ripe return 0 def last_element(some_list): '''return the last element of some_list''' if len(some_list) > 0: return some_list[len(some_list) - 1] else: # handle case of empty list return None