Question
5: Details of dict_by_drug: The dict_by_drug function takes a dict argument in the form of the data structure above. (a dict whose keys are dates
5: Details of dict_by_drug: The dict_by_drug function takes a dict argument in the form of the data structure above. (a dict whose keys are dates (str), each associated with an inner dict whose keys are drug names (str) and whose associated value is a tuple of int transactions) and returns a dictionary (dict or defaultdict) associating a drug name (str) with another dictionary: this inner dictionary associates a date (str) with a tuple of int representing all its transactions (non-0 int values). So calling this function with the db1 dictionary shown above dict_by_drug( db1 ) returns a dictionary whose contents are {'Azor': {'2016-04-01': (100, -3, -3, 1), '2016-04-02': (-3, -3, 2)), 'Benz': {'2016-04-01': (4, 2, -3)}, 'Depr': {'2016-04-02': (4, -7)}, 'Caml': {'2016-04-02': (6, -2, -3)}} Of course, dictionaries print on one line, and can print their key/values (value) in any order, because they are not ordered.
def dict_by_drug(db : {str:{str:(int,)}}) -> {str:{str,(int)}}:
if name == 'main':
if prompt.for_bool('Test dict_by_drug?', True): db1 = {'2016-04-01': {'Benz': (4, 2, -3), 'Azor': (100, -3, -3, 1)}, '2016-04-02': {'Depr': (4, -7), 'Azor': (-3, -3, 2), 'Caml': (6, -2, -3)}} print(' argument =',db1) answer = dict_by_drug(db1) print(' answer =', answer) check(answer, {'Caml': {'2016-04-02': (6, -2, -3)}, 'Depr': {'2016-04-02': (4, -7)}, 'Azor': {'2016-04-01': (100, -3, -3, 1), '2016-04-02': (-3, -3, 2)}, 'Benz': {'2016-04-01': (4, 2, -3)}})
db1 = {'2016-04-02': {'Azor': (-5, -2, 3, -1, -7), 'Efos': (-3, 20, -7), 'Caml': (4, 6, -2, 20, -8, -3)}, '2016-04-03': {'Benz': (-4, 16), 'Azor': (-4,), 'Efos': (4, 1, 30), 'Caml': (-2, -2, -3, -2)}, '2016-04-01': {'Benz': (4, 2, -3, 20), 'Depr': (30, -2, -5, -10, -2), 'Azor': (20, -3, -3, 1), 'Efos': (4, -7), 'Caml': (4, 6, -2, 1, -8, -3)}} print(' argument =',db1) answer = dict_by_drug(db1) print(' answer =', answer) check(answer, {'Caml': {'2016-04-03': (-2, -2, -3, -2), '2016-04-01': (4, 6, -2, 1, -8, -3), '2016-04-02': (4, 6, -2, 20, -8, -3)}, 'Azor': {'2016-04-02': (-5, -2, 3, -1, -7), '2016-04-01': (20, -3, -3, 1), '2016-04-03': (-4,)}, 'Benz': {'2016-04-01': (4, 2, -3, 20), '2016-04-03': (-4, 16)}, 'Efos': {'2016-04-03': (4, 1, 30), '2016-04-01': (4, -7), '2016-04-02': (-3, 20, -7)}, 'Depr': {'2016-04-01': (30, -2, -5, -10, -2)}})
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