# Name: ... # CSE 160 # Autumn 2021 # Final Exam # Problem 1 def least_exp_store(ingred_price_dict, ingred1, ingred2): ''' Arguments: ingred_price_dict: a dict of price_lists ingred1, ingred2: 2 strings representing ingredients Returns: A list containing two elements: a string representing the least expensive store to purchase ingred1 and ingred2 at, and the total cost of the two ingredients at that store. If there is a tie for lowest cost, returns the store with the higher index. ''' # your solution code should start here assert len(ingred_price_dict[ingred1]) == len(ingred_price_dict[ingred2]) num_stores = len(ingred_price_dict[ingred1]) index_of_low_cost = None curr_low_cost = None # Find lowest cost for store_num in range(num_stores): ingred1_cost = ingred_price_dict[ingred1][store_num] ingred2_cost = ingred_price_dict[ingred2][store_num] curr_cost = ingred1_cost + ingred2_cost # Use <= to break ties so higher index wins if (curr_low_cost is None) or (curr_cost <= curr_low_cost): curr_low_cost = curr_cost index_of_low_cost = store_num return ["Store " + str(index_of_low_cost), curr_low_cost] def test_least_exp_store(): ingred_price_dict = {"Flour": [1, 2, 3], "Sugar": [20, 19, 24], "Butter": [2, 3, 4], "Molasses": [6, 7, 5]} assert least_exp_store(ingred_price_dict, "Butter", "Molasses") == ["Store 0", 8] assert least_exp_store(ingred_price_dict, "Flour", "Sugar") == ["Store 1", 21] ingred_price_dict2 = {"Flour": [10], "Sugar": [30]} assert least_exp_store(ingred_price_dict2, "Flour", "Sugar") == ["Store 0", 40] # Problem 2 def customer_loyalty(visit_tracker): ''' Arguments: visit_tracker: a dictionary mapping customer names to a list of their visits to bubble tea stores Returns: a dictionary mapping stores to a set of their loyal customers. If a store does not have any loyal customers, it is not included in the returned dictionary. If the visit_tracker is empty or if there are no loyal customers, return an empty dictionary. ''' # your solution code should start here loyalty_dict = {} # Examine each customer, 3x?, < 2 stores total? # If so, add to new dictionary for customer, visits in visit_tracker.items(): uniq_stores = set(visits) # Customer must have only visited 2 stores max # Could have visited 2 stores 3 or more times # So could be in two stores' loyalty set. # Only consider customers that visit at most 2 stores if len(uniq_stores) <= 2: # This will just iterate twice, since there are <= 2 stores for store in uniq_stores: # If customer visited this store >= 3 times if visits.count(store) >= 3: # Add a key for this store if not already there if store not in loyalty_dict: loyalty_dict[store] = set() loyalty_dict[store] = loyalty_dict[store] | {customer} # OR: loyalty_dict[store].add(customer) return loyalty_dict def test_customer_loyalty(): visit_tracker1 = {"Evan": ["Ding Tea", "Ding Tea", "Yi Fang", "Ding Tea", "Ding Tea"], "Izzy": ["Boba Up", "Ding Tea", "Boba Up", "Boba Up", "Ding Tea", "Ding Tea", "Ding Tea"]} assert customer_loyalty(visit_tracker1) == {"Boba Up": {"Izzy"}, "Ding Tea": {"Evan", "Izzy"}} visit_tracker2 = {"Jackie": ["DIY Tea", "DIY Tea", "DIY Tea"], "Evan": ["Yum Tea", "Ding Tea", "Yi Fang", "Yum Tea", "Yum Tea"]} assert customer_loyalty(visit_tracker2) == {"DIY Tea": {"Jackie"}} # Problem 3 def never_win(match_tuples): ''' Arguments: match_tuples: a list of tuples that are size 2 representing matches played. Each tuple is in the form (string, boolean), which represents the champion played for the match and whether the match was won (True for won, False for lost). Assume match_tuples is not empty. Returns: a list of the unique champions that have no matches won in match_tuples, sorted in alphabetical order. Returns an empty list if there are no champions with zero matches won. ''' # your solution code should start here win_set = set() lose_set = set() for character, won in match_tuples: if won: win_set.add(character) else: lose_set.add(character) ret_list = list(lose_set - win_set) ret_list.sort() return ret_list def test_never_win(): win_list1 = [("Ezreal", False), ("Yasuo", False), ("Teemo", False), ("Jinx", True), ("Ezreal", True), ("Teemo", False)] assert never_win(win_list1) == ["Teemo", "Yasuo"] assert never_win([('Ezreal', True), ('Yasuo', True), ('Jinx', False), ('Jinx', True)]) == [] def main(): test_least_exp_store() test_customer_loyalty() test_never_win() if __name__ == "__main__": main() # You will answer a question about collaboration in part 2.