# # Dictionaries # # Practice # # Preliminary notes: - Based on Chapter 9 - Important! - Use only studied material # Q. 1 # Given is a dictionary in which each key # is a product code and each value is a list # of the prices of product in 5 stores # (the stores are the same for all products, # and the information about their prices is # arranged in the same order). # NA means unavailable data. # Write a program that recieves an index of # a store and cacculates the average price # of all products in that store. The calculation # does not take the NAs into account. products = {'A': [2, 3, 1, "NA", 2], 'B': [3, "NA", 4, 2, 4], 'C': [1, 2, 3, "NA", 3], 'D': [4, 1, 2, 1, 2]} ind = 3 prices = [] for v in products.values(): if v[ind] != "NA": prices.append(v[ind]) print(sum(prices) / len(prices)) # Q. 2 # Given are two dictionaries of prices # of three products sold in two different # stores. Produce a dictionary holding # the price difrerences for each product. store1 = {'a': 5, 'b': 7, 'c': 2} store2 = {'b': 5, 'c': 7, 'a': 2} diffs = {} for k in store1.keys(): diffs[ k ] = store1[k] - store2[k] print(diffs) # Q. 3 # You have a dictionary of all cheapest products # in each of the three stores in the neighborhood. # Write a program that recevies, one after another, # a name of a store and a list of products sold in # that store and their prices, having this structure: # [['a', 7],['b',9],['c',2]] # The iteration stops when the string q is entered. # The program updates the dictionary. If the name of # store already exists in the dictionary, the program also # should update the minimal price, if needed. In that case # it issues a proper message. cheapest = {'store1': 5, 'store2': 7, 'store3': 2} storeName = input("Please enter a store name: ") while storeName != 'q': prices = eval(input("Please enter a list of prices: ")) minPrice = min(list(dict(prices).values())) if storeName not in cheapest: cheapest[storeName] = minPrice else: if cheapest[storeName] > minPrice: cheapest[storeName] = minPrice print(storeName, "minimal price was updated.") storeName = input("Please enter a store name: ") print(cheapest) lst = [['a', 7],['b',9],['c',2]] d = dict(lst) print(d) prices = d.values() print(min(list(prices))) minValue = min(list(dict(lst).values())) print(minValue) cheapest = {'store1': 5, 'store2': 7, 'store3': 2} storeName = input("Please enter a store name: ") while storeName != 'q': prices = eval(input("Please enter a list of prices: ")) minPrice = min(list(dict(prices).values())) currentMinimal = cheapest.get(storeName, False) if currentMinimal == False : cheapest[storeName] = minPrice else: if currentMinimal > minPrice: cheapest[storeName] = minPrice print(storeName, "minimal price was updated.") storeName = input("Please enter a store name: ") print(cheapest) # Q. 4 # We are 4 friends. We meet every week. # The host serves each of us with a plate # of 4 cookies. The meeting ends when at # least one of use ate all her/his cookies. # Write a program that receives, one by # another, a friend-s name who eats one # cookie. The program prints the name # of the friend who ate all his cookies first. cookies = {'A': 4, 'B': 4, 'C':4, 'D': 4} proceed = True while proceed: friend = input("Please enter friend's code: ") cookies[friend] = cookies[friend] - 1 if cookies[friend] == 0: proceed = False print(friend) print(cookies) # Q. 5 # Given is a dictionary of prices in a # certain store: keys are product codes, # values are product prices. Write a program # that receives a positive number and prints # codes of all pairs of products whose prices # sum is less or equal to the received number. # Each such pair should be printed only once. # Try to avoid checking a pair more than once. prices = {'A':5, 'B':7, 'C':1, 'D':2} num = 4 codes = list(prices.keys()) while len(codes) > 1: curCode = codes.pop() for code in codes: if prices[curCode] + prices[code] <= num: print(curCode, code) # Q. 6 # Your store has four departments, # A, B, C and D. The dictionary stock # holds the number of products sold in # each departement. The keys are letters # representing the department, and the values # are lists of lists: each inner lists includes # a name of a products and the number of items # of this product in stock. E.g. # store = {'A':{'p1':5, 'p2':3, 'p3':2}, # 'B':{'p6':2}, # 'C':{'p1':1, 'p7':1, 'p8':3}, # 'D':{'p9':1, 'p10':1, 'p3':1}} # Now you went bankrupt. # Write a program that receives a name a # department and number of items of a product # sold in that department. The program updates # the number of items. If it is 0 or below, # it deletes the inner list of that product. # If all products of this department are no # longer in stock, the department is "deleted" # from the dictionary. store = {'A':{'p1':5, 'p2':3, 'p3':2}, 'B':{'p6':2}, 'C':{'p1':1, 'p7':1, 'p8':3}, 'D':{'p9':1, 'p10':1, 'p3':1}} department = 'A' code = 'p1' items = 6 store[department][code] = store[department][code] - items if store[department][code] <= 0: del store[department][code] if not store[department]: del store[department] print(store) tuple lst = [3, 4, 5] lst[1] = 999 lst.sort() tup = (3, 4, 5) tup[1] = 990 # Q. 7 # Given is a dictionary of products: each key # is the product code and the store in which # it is sold, and the value is the product's # price and that store. E.g. # { ('A','store1') :6, # ('A','store2') :5, # ('B','store3') :3, # ('C','store2') :1} # Write a program that: # (a) Receives a price and prints a list # of all product codes sold in prices # smaller than the given price in any store, # with no duplicates. # version 1 products = {('A','store1'):6, ('A','store2'):5, ('B','store3'):3, ('C','store2'):1} price = 7 ['A', 'B', 'C'] codes = set() for k in products: if products[k] < price: codes.add(k[0]) print(list(codes)) # version 2 products = {('A','store1'):6, ('A','store2'):5, ('B','store3'):3, ('C','store2'):1} price = 7 ['A', 'B', 'C'] codes = set() for k, v in products.items(): if v < price: codes.add(k[0]) print(list(codes)) # (b) Receives a product code and finds its # cheapest price. products = {('A','store1'):6, ('A','store2'):5, ('B','store3'):3, ('C','store2'):1} myCode = 'C' prices = [] for code, store in products: if code == myCode: prices.append(products[ (code, store) ]) print(min(prices)) # (c) Produces a new dictionary, in which each key # is a product code and each value is a list # of two values: the number of stores in which # it is sold, and the product-s highest price, # in any store. products = {('A','store1'):6, ('A','store2'):5, ('B','store3'):3, ('C','store2'):1} myDict = {} for k, v in products.items(): code = k[0] if code not in myDict: myDict[code] = [1,v] else: myDict[code][0] += 1 if myDict[code][1] < v: myDict[code][1] = v print(myDict) products = {('A','store1'):6, ('A','store2'):5, ('B','store3'):3, ('C','store2'):1} myDict = {} for code, store in products: myDict[code] = [0, 0] for k, v in products.items(): code = k[0] myDict[code][0] += 1 if myDict[code][1] < v: myDict[code][1] = v print(myDict)