Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are tasked with implementing a series of functions that perform various operations on dictionaries in Python. These functions will manipulate dictionaries that represent fruit
You are tasked with implementing a series of functions that perform various operations on dictionaries in Python. These functions will manipulate dictionaries that represent fruit prices and perform different operations as specified.
dictionaryoperationsfruitprices: dict, fruits: list
Perform a series of operations on the given fruitprices dictionary based on the fruits list:
Add fruits with a cost of
Modify the cost of fruits to
Increase the cost of fruits by
Delete fruits from fruitprices.
Print the price of fruits
Print the names of fruits in fruitprices as a sorted list.
Print the prices of fruits in fruitprices as a sorted list.
increasepricesfruitprices: dict None
Increase the prices of every fruit by and round to two decimal places. Modify the dictionary in place.
dictfromstringstring: str keytype, valuetype
Convert a string with commaseparated keyvalue pairs into a dictionary, converting the keys and values to the specified types.
dicttostringD: dict str
Convert a dictionary back into a string with each keyvalue pair on a new line, using comprehensions.
Template code:
def dictionaryoperationsfruitprices:dict, fruits:list:
# add the fruit fruits to fruitprices with cost
orderprintfruitprices # this function is in the hidden code
# modify the cost of fruits as in fruitprices
orderprintfruitprices
# increase the cost of fruits by in fruitprices
orderprintfruitprices
# delete both key and value for fruits from fruitprices
orderprintfruitprices
# print the price of fruits
# print the names of fruits in fruit prices as a list sorted in ascending order
# print the prices of the fruits as a list sorted in ascending order.
def increasepricesfruitprices:dict None:
Increase the prices of every fruit by percent and round to two decimal places
Arguments:
fruitprices: dict fruit name as key and price as value
Return:
None Do not return any thing modify inplace
def dictfromstringstring:strkeytype,valuetype:
Given a string where each line has a comma seperated keyvalue pair, create a dictionary out of it Also convert the types of key and values to the given types.
Arguments:
string str: string to be parsed
keytype class: the data type of the keys
valuetype class: the data type of the values
Return:
D dict: the output dictionary
def dicttostringD:dict str:
Convert the given dictionary back to the string fromat produced by dictfromstring Again, do not use loops or conditionals, use comprehensions.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started