Question: PYTHON 6: Details of summary_text_2 : The summary_text_2 function takes a dict argument in the form of the data structure above. (a dict whose keys
PYTHON 6: Details of summary_text_2 : The summary_text_2 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 string whose exact formatting is specified as follows 1. The first line contains the header 'DRUG DATE NET' This string appears inside the summary_text_2 function in the exam.py file. 2. The data appears with drug names in alphabetical order (each drug name appears just once, to the left of the first date), left justified in a field width of 10 characters, followed by a space; for each drug name, its dates appear in numerical order, left justified in a field width of 10 characters, followed by a space; for each date, the net number of pills distributed/received appears right justified in a field width of 6 characters: e.g., if the transactions are the tuple (4, 2, -3) the net is 4+2-3 = 3. 3. The last line contains the text 'DONE' 4. Every line except the last line (DONE) ends with a new line. So calling this function with the db1 dictionary shown above summary_text_2 ( db1 ) returns a str which when printed is DRUG DATE NET Azor 2016-04-01 95 2016-04-02 -4 Benz 2016-04-01 3 Caml 2016-04-02 1 Depr 2016-04-02 -3 DONE If we printed the repr of this string, it would start as follows 'DRUG DATE NET Azor 2016-04-01 95 2016-04-02 -4 2016-04-02 -4 Benz...' There is not enough room to print everything here; but it ends with '...Benz 2016-04-01 3 Caml 2016-04-02 1 Depr 2016-04-02 -3 DONE' Important: You can assume no drug name is longer than 10 characters. The format {: <10} produces a string that is left justified in a field width of 10: read it as the characters left-brace, colon, space, <10, right-brace. If s is a string, we can use it like '{: <10}'.format(s). The format {: >10} produces a string that is right justified in a field width of 10 (the only difference from left-justification is to replace < by >). We can also use the format in an f string, like f'{s:<10}'. Hint: build a reasonable data structure and then convert it to a string.
def summary_text(db : {str:{str:(int,)}}) -> str: header = 'DRUG DATE NET ' if prompt.for_bool('Test summary_text_1: worth only 1 point extra credit?', 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 = summary_text(db1) print(' answer (as repr) =', repr(answer)) print(' answer (as str) =') print(answer) check(repr(answer), repr('DRUG DATE NET Azor 2016-04-01 95 2016-04-02 -4 Benz 2016-04-01 3 Caml 2016-04-02 1 Depr 2016-04-02 -3 DONE')) 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 = summary_text(db1) print(' answer (as repr) =', repr(answer)) print(' answer (as str) =') print(answer) check(repr(answer), repr('DRUG DATE NET Azor 2016-04-01 15 2016-04-02 -12 2016-04-03 -4 Benz 2016-04-01 23 2016-04-03 12 Caml 2016-04-01 -2 2016-04-02 17 2016-04-03 -9 Depr 2016-04-01 11 Efos 2016-04-01 -3 2016-04-02 10 2016-04-03 35 DONE')) Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
