import math from operator import itemgetter def shared_values(set_list): ''' Arguments: set_list: a list of sets Returns: A set of the items that exist in all of the given sets ''' if len(set_list) == 0: return set() output_set = set_list[0] # guaranteed to be at least one set in list for input_set in set_list[1:]: output_set = output_set & input_set return (output_set) def strongest_pokemon(pokemon_list): strongest = {"strength": (None, float("-inf")), "speed": (None, float("-inf")), "defense": (None, float("-inf"))} for pokemon in pokemon_list: for attr in strongest.keys(): name = pokemon["name"] value = pokemon[attr] if value > strongest[attr][1]: strongest[attr] = (name, value) return strongest def best_veggie_value(market, quantities): ''' Arguments: market: a dictionary of dictionaries. Keys are stand names. Values (inner dictionaries) are the items this stand sells, as well as the stand's discount thresholds. quantities: a dictionary of item names as keys with the desired quantities as values. Returns: A new dictionary, keys are stand names and values are a list of items. ''' ret = {} for item, count in quantities.items(): lowest_store_name = "" lowest_store_price = math.inf for store, menu in market.items(): store_price = menu.get(item, math.inf) total = store_price * count if count >= menu['bulk_discount_quantity']: total = total * (1 - menu['bulk_discount_percent']) if total < lowest_store_price: lowest_store_price = total lowest_store_name = store if lowest_store_name != "": ret.setdefault(lowest_store_name, []) ret[lowest_store_name].append(item) return ret def best_veggie_value_alt(market, quantities): ret = {} # intended to be a map, item: (store, price) item_prices = {} for store, menu in market.items(): ret.setdefault(store, []) for item, count in quantities.items(): store_price = menu.get(item, math.inf) total = store_price * count if count >= menu['bulk_discount_quantity']: total = total * (1 - menu['bulk_discount_percent']) item_prices.setdefault(item, []) item_prices[item].append((store, total)) for item, store_prices in item_prices.items(): sorted_prices = sorted(store_prices, key=itemgetter(1)) cheapest_store = sorted_prices[0][0] ret[cheapest_store].append(item) return ret if __name__ == '__main__': print("Nothing to do here. Did you mean to run final_22wi_tests.py?")