Page 1 of 1

Background Dictionaries give an enriched way to store values by more than just sequential indexes; we identify key-value

Posted: Thu Jul 14, 2022 2:11 pm
by answerhappygod
Background Dictionaries give an enriched way to store values bymore than just sequential indexes; we identify key-value pairs, andtreat keys like indexes of various other types. The onlyrestriction on keys is that they are "hashable", which we canapproximate by thinking that they are "immutable all the way down".Though unordered, dictionaries help us simplify many tasks bykeeping those key-value associations. Each key can only be pairedwith one value at a time in a dictionary. When submitting yourproject to Gradescope, avoid the input() and print() functions toget input from the user or provide output back, otherwise it willgenerate an error message.
Guidelines
• Any arithmetic/comparison/boolean operatorsare all fine
• Any control flow statements (selection statements,break/continue, for, while, etc. loops).
• From built-in functions, you are allowed only to use range(),int(), str(), len(), set(), list(), only if needed.
• From list/dict methods, you are allowed to use only .sort(),.keys(), .values(), .items(), i.e. you can’t use .pop(), .count(),.append(), .extend(), etc.
• You are not allowed to import anything
• You are not allowed to use slicing, i.e. something in the formvariable[x:y:z]
• You are not allowed to use any feature that hasn’t beencovered in lecture yet
Assumptions
• You may assume that the types of the values that are sent tothe functions are the proper ones.FunctionsIn this assignment you’re going to implement only functions.Be reminded that the generic structure of a function is thefollowing:
def function_name(arg1, arg2, etc.): #This line isthe signature # commands go here # morecommands # result = ... returnresult #This line returns the result of your computations
The signature of each function is provided below, do not make anychanges to them otherwise thetester will not work properly. Keep in mind that you must not writea main body for your program inthis assignment. You should only implement these functions; it isthe tester that will be calling andtesting each one of them.
DESCRIPTIONYakko, Wakko and Dot went out to collect candies each ontheir own. Sincethey decide to eat them until the next day, they put all them onthe table andpack the same type of candies in a box, writing the name and howmany areinside. The next day they check that no one has eaten candiesduring the nightand they sort them in alphabetical order.
def packing_candy(Yakko_lst, Wakko_lst,Dot_lst): Description: Given a dictionary specifying each candy andhow many of them inside the box Parameters: Yakko_lst, Wakko_lst, Dot_lst (each a list ofstrings) Assume The length of each list is >=1 Acandy can more than once in a list Return value: candies and quantity (a dictionary withkey:value pair the form {'string':int} which specify candy name and how many of them areinside. NOTE: candy names are sorted in ascending.
Examples:packing_candy(['twix','m&m','twix'], ['twix','snickers'],['m&m']) → {'m&m':2, 'snickers':1, 'twix':3}packing_candy(['kit kat'], ['kit kat','twix'],['twix','musketeers']) → {'kit kat':2, 'musketeers':1,'twix':2}packing_candy(['skittles','rolo'], ['m&m','skittles'],['m&m', 'm&m']) → {'m&m':3, 'rolo':1, 'skittles':2}
At night, Pinky and Brain notice that there are candies inthe boxes and startto open them and eat them. Brain doesn't want Yakko, Wakko, and Dottonotice that someone ate candy, so he updates the labels on each boxwith thenumber of candies left.
def update_candy(candies, candies_eaten): Description: Given candies (a dictionary with pairs'sting':int) and candies_eaten (a list of candies eaten) return the updated information of candiesleft in each box Parameters: candies (a dictionary with pairs'sting':int) candies letft in each box (a dictionary with 'sting':intpairs) Assume The length of candies and candies_eaten is>=1 A candy can be more thanonce in candies_eaten All candynames in candies_eaten are in candies There is at least 1 candy in a boxbefore they are eaten candies in candies_eaten are >= than the current amount in abox Return value: the updated information of each candy left(dictionary) Note: In case there are no candies of some kind left, that box isremovedExamples:update_candy ({'m&m':5, 'snickers':1, 'twix':3}, ['m&m','snickers', 'm&m']) → {'m&m':3, 'twix':3}update_candy ({'rolo':2, 'skittles':2, 'm&m':3}, ['m&m','rolo', 'm&m']) → {'rolo':1, 'skittles':2, 'm&m':1}update_candy ({'kit kat':1, 'musketeers':1}, ['musketeers', 'kitkat']) → {}4
Slappy wants to cook something for her nephew Skkipy.Slappy writes down the ingredients she needsto buy and which department section she can find them in. Beforevisiting the supermarket, she verifieson how many different sections she will find allingredients.
def how_many_sections(ingredients): Description: Given a dictionary that specifies aningredient and which section of the supermarket it is in, return how many differentsections must be visited to find all the ingredients. Parameters: ingredients (a dictionary with'sting':'string' pairs), indicating an ingredient and in which section of the supermarket it isfound. Assume: The length of ingredients is >= 1 Return value: How many different sections of thesupermarket Slappy needs to visit(int)
Examples:how_many_sections({'tomatoes':'vegetables', 'onions':'vegetables'})→ 1how_many_sections({'apples':'fruit', 'eggs':'grocery','milk':'dairy', 'bananas':'fruit'}) → 3how_many_sections({'pears':'fruit', 'yeast':'bakery','chocolate':'bakery', 'limes':'fruit'}) → 2