CSE 101.02: Introduction to Computers
Stony Brook University
Spring 2019
Homework Assignment #5
Assignment Due: May 10, 2019 by 11:59 pm
Learning Outcomes
After completion of this homework assignment, you should be able to:
• Design and implement algorithms that work with classes and objects.
• Implement a function that reads data from an input CSV (comma-separated values) file.
Preliminaries
For this assignment you will be working with the following classes, which are available in the file
homework5 classes.py:
class Pharmacy:
def __init__(self, inventory, unit_prices):
self.inventory = inventory
self.unit_prices = unit_prices
class Prescription:
def __init__(self, patient, drug_name, quantity):
self.patient = patient
self.drug_name = drug_name
self.quantity = quantity
You will be asked to write several functions that work with the Prescription and Pharmacy classes. To
complete this assignment you may write any helper functions you like inside your homework5.py file.
Pharmacy class attributes:
• inventory: A dictionary that represents the drug inventory in a particular pharmacy. The dictionary
maps the drug name (string) to its stock amount (integer) in units. The unit is adaptive, meaning that you
don’t need to wo
y about whether it counts in pills or milliliters or some other unit.
• unit prices: A dictionary that represents a look-up database that serves as a price book. The dictionary
maps the drug name (string) to its unit price (integer). We will always assume that if a drug’s name exists
as a key in inventory, it will always exist as a key in unit prices also. Note that the converse is not
always true, however.
Example dictionaries are given below. Different dictionaries will likely be used during grading:
CSE 101 – Spring 2019 Homework #5 1
inventory = {’Hydrocodone’: 78, ’Zocor’ : 25,
’Lisinopril’: 150, ’Synthroid’ : 32,
’Norvasc’ : 100, ’Prilosec’ : 44,
’Azithromycin’ : 78, ’Amoxicillin’ : 26,
’Glucophage’ : 17, ’Hydrochlorothiazide’ : 39,
’Crestor’ : 28, ’Cymbalta’ : 55,
’Adderall’ : 0, ’Ibuprofen’ : 44}
unit_prices = {’Hydrocodone’: 4, ’Zocor’ : 5,
’Lisinopril’: 10, ’Synthroid’ : 1,
’Norvasc’ : 4, ’Prilosec’ : 9,
’Azithromycin’ : 4, ’Amoxicillin’ : 8,
’Glucophage’ : 1, ’Hydrochlorothiazide’ : 13,
’Crestor’ : 12, ’Cymbalta’ : 9,
’Adderall’: 5}
Prescription class attributes:
• patient: the patient’s name (a string).
• drug name: the medication’s name for the particular prescription (a string)
• quantity: the quantity specified for the particular prescription (an integer)
Part I: Dispense Medication (20 points)
Write a function dispense() that takes two arguments, in this order:
• pharmacy: a Pharmacy object
• prescription: a Prescription object
The function reads the fields from the prescription object and updates the stocked amount of the drug in
the pharmacy object’s inventory dictionary. After updating the co
ect stocked amount of the drug in the
inventory dictionary, the function returns the updated stocked amount.
The function must be able to handle the following e
or conditions. The associated return value must be returned
when a given e
or condition is present. The arguments must be left unchanged when an e
or occurs. Check fo
the e
or conditions in the order given:
E
or Condition Return Value
The quantity requested in the prescription is zero or negative. -1
The drug does not exist in the pharmacy’s inventory. (That is, the name is invalid.) -2
The stocked amount of the drug is not enough to fill the prescription. -3
Examples:
Because the function arguments contain a lot of data, only a few test cases are given here. See the testing scripts
for additional test cases.
CSE 101 – Spring 2019 Homework #5 2
Function Call Return Value
dispense(Pharmacy({’Nexium’: 915, ’Epogen’: 619,
’Singulair’: 827, ’Lipitor’: 920, ’Gabapentin’:
982, ’Amlodipine’: 629, ’Hydrochlorothiazide’: 426,
’Crestor’: 856, ’Levothyroxine’: 822, ’Plavix’:
134, ’Metformin’: 780},{’Nexium’: 68, ’Epogen’: 92,
’Singulair’: 25, ’Lipitor’: 22, ’Gabapentin’: 58,
’Amlodipine’: 96, ’Hydrochlorothiazide’: 73, ’Crestor’:
60, ’Levothyroxine’: 86, ’Plavix’: 99, ’Metformin’: 63}),
Prescription(’Timmy’,’Gabapentin’,462))
520
Updated pharmacy value
Pharmacy({’Nexium’: 915, ’Epogen’: 619, ’Singulair’: 827, ’Lipitor’:
920, ’Gabapentin’: 520, ’Amlodipine’: 629, ’Hydrochlorothiazide’: 426,
’Crestor’: 856, ’Levothyroxine’: 822, ’Plavix’: 134, ’Metformin’:
780},{’Nexium’: 68, ’Epogen’: 92, ’Singulair’: 25, ’Lipitor’: 22,
’Gabapentin’: 58, ’Amlodipine’: 96, ’Hydrochlorothiazide’: 73,
’Crestor’: 60, ’Levothyroxine’: 86, ’Plavix’: 99, ’Metformin’: 63})
Function Call Return Value
dispense(Pharmacy({’Seroquel’: 956, ’Lisinopril’: 485,
’Zocor’: 383, ’Amoxicillin’: 178, ’Nexium’: 697,
’Simvastatin’: 458, ’Amlodipine’: 664, ’Azithromycin’:
870, ’Abilify’: 995},{’Seroquel’: 40, ’Lisinopril’:
28, ’Zocor’: 99, ’Amoxicillin’: 37, ’Nexium’: 20,
’Simvastatin’: 35, ’Amlodipine’: 67, ’Azithromycin’: 86,
’Abilify’: 46}), Prescription(’Lisa’,’Amoxicillin’,1))
177
Updated pharmacy value
Pharmacy({’Seroquel’: 956, ’Lisinopril’: 485, ’Zocor’: 383,
’Amoxicillin’: 177, ’Nexium’: 697, ’Simvastatin’: 458, ’Amlodipine’:
664, ’Azithromycin’: 870, ’Abilify’: 995},{’Seroquel’: 40,
’Lisinopril’: 28, ’Zocor’: 99, ’Amoxicillin’: 37, ’Nexium’: 20,
’Simvastatin’: 35, ’Amlodipine’: 67, ’Azithromycin’: 86, ’Abilify’:
46})
Function Call Return Value
dispense(Pharmacy({’Gabapentin’: 770, ’Levothyroxine’:
560, ’Plavix’: 560, ’Simvastatin’: 504, ’Amoxicillin’:
340, ’Metformin’: 703, ’Glucophage’: 428, ’Advai
Diskus’: 445},{’Gabapentin’: 70, ’Levothyroxine’: 92,
’Plavix’: 24, ’Simvastatin’: 49, ’Amoxicillin’: 54,
’Metformin’: 41, ’Glucophage’: 58, ’Advair Diskus’: 62}),
Prescription(’Yan’,’Advair Diskus’,-21))
-1
Updated pharmacy value
Pharmacy({’Gabapentin’: 770, ’Levothyroxine’: 560, ’Plavix’:
560, ’Simvastatin’: 504, ’Amoxicillin’: 340, ’Metformin’: 703,
’Glucophage’: 428, ’Advair Diskus’: 445},{’Gabapentin’: 70,
’Levothyroxine’: 92, ’Plavix’: 24, ’Simvastatin’: 49, ’Amoxicillin’:
54, ’Metformin’: 41, ’Glucophage’: 58, ’Advair Diskus’: 62})
CSE 101 – Spring 2019 Homework #5 3
Function Call Return Value
dispense(Pharmacy({’Levothyroxine’: 976, ’Actos’: 697,
’Nexium’: 852, ’Glucophage’: 759, ’Ciprofloxacin’: 180,
’Pantoprazole’: 911},{’Levothyroxine’: 59, ’Actos’: 60,
’Nexium’: 38, ’Glucophage’: 81, ’Ciprofloxacin’: 53,
’Pantoprazole’: 93}), Prescription(’Rex’,’Synthroid’,499))
-2
Updated pharmacy value
Pharmacy({’Levothyroxine’: 976, ’Actos’: 697, ’Nexium’: 852,
’Glucophage’: 759, ’Ciprofloxacin’: 180, ’Pantoprazole’:
911},{’Levothyroxine’: 59, ’Actos’: 60, ’Nexium’: 38, ’Glucophage’:
81, ’Ciprofloxacin’: 53, ’Pantoprazole’: 93})
Function Call Return Value
dispense(Pharmacy({’Pantoprazole’: 125,
’Hydrochlorothiazide’: 978, ’Norvasc’: 999, ’Omeprazole’:
977, ’Amlodipine’: 141, ’Metformin’: 783},{’Pantoprazole’:
81, ’Hydrochlorothiazide’: 60, ’Norvasc’: 19,
’Omeprazole’: 34, ’Amlodipine’: 85, ’Metformin’: 30}),
Prescription(’Erin’,’Norvasc’,1031))
-3
Updated pharmacy value
Pharmacy({’Pantoprazole’: 125, ’Hydrochlorothiazide’: 978,
’Norvasc’: 999, ’Omeprazole’: 977, ’Amlodipine’: 141, ’Metformin’:
783},{’Pantoprazole’: 81, ’Hydrochlorothiazide’: 60, ’Norvasc’: 19,
’Omeprazole’: 34, ’Amlodipine’: 85, ’Metformin’: 30})
Please note that the test cases shown above are different from those that are given in the tester file.
Part II: Dispense a Batch of Medications (20 points)
Write a function batch dispense() that takes two arguments, in this order:
• pharmacy: a Pharmacy object
• prescriptions: a list of Prescription objects
The function iterates through the list, inspects each individual Prescription object, and then dispenses the
prescribed drugs by updating the inventory dictionary as needed. The function returns the total cost of all
dispensed prescriptions in the list. To do so, the function will need to look up the unit prices of the drug in the
unit prices dictionary in the pharmacy object.
While the function is iterating over the prescriptions list, it is possible that e
ors arise due to invalid data
(for example, there are not enough pills in stock to fill a prescription). In all invalid cases (see Part I for a complete
list), simply skip that prescription and continue to inspect the remaining prescriptions. If all prescriptions are
invalid, the function will return zero because they are all skipped.
If the prescriptions list is empty, the function returns 0.
CSE 101 – Spring 2019 Homework #5 4
Examples:
Function Call Return Value
atch dispense(Pharmacy({’Adderall’: 731, ’Pantoprazole’:
932, ’Singulair’: 645, ’Nexium’: 308, ’Crestor’: 613,
’Amoxicillin’: 562, ’Lisinopril’: 808, ’Azithromycin’: 415,
’Hydrochlorothiazide’: 993, ’Epogen’: 531, ’Losartan’:
101, ’Amlodipine’: 749, ’Hydrocodone’: 652, ’Zocor’:
169},{’Adderall’: 33, ’Pantoprazole’: 87, ’Singulair’:
17, ’Nexium’: 83, ’Crestor’: 64, ’Amoxicillin’: 100,
’Lisinopril’: 42, ’Azithromycin’: 60, ’Hydrochlorothiazide’:
88, ’Epogen’: 44, ’Losartan’: 74, ’Amlodipine’: 31,
’Hydrocodone’: 96, ’Zocor’: 38}), [Prescription(’Mack’,
’Zocor’, -27), Prescription(’Rachael’, ’Crestor’, 474),
Prescription(’Janet’, ’Epogen’, 202), Prescription(’Dana’,
’Zocor’, 97), Prescription(’Dave’, ’Nexium’, 99)])
51127
Updated pharmacy value
Pharmacy({’Adderall’: 731, ’Pantoprazole’: 932, ’Singulair’: 645,
’Nexium’: 209, ’Crestor’: 139, ’Amoxicillin’: 562, ’Lisinopril’:
808, ’Azithromycin’: 415, ’Hydrochlorothiazide’: 993, ’Epogen’:
329, ’Losartan’: 101, ’Amlodipine’: 749, ’Hydrocodone’: 652,
’Zocor’: 72},{’Adderall’: 33, ’Pantoprazole’: 87, ’Singulair’: 17,
’Nexium’: 83, ’Crestor’: 64, ’Amoxicillin’: 100, ’Lisinopril’:
42, ’Azithromycin’: 60, ’Hydrochlorothiazide’: 88, ’Epogen’: 44,
’Losartan’: 74, ’Amlodipine’: 31, ’Hydrocodone’: 96, ’Zocor’: 38})
Function Call Return Value
atch dispense(Pharmacy({’Advair Diskus’: 447, ’Gabapentin’:
702, ’Prilosec’: 511, ’Metformin’: 843, ’Singulair’:
718, ’Hydrochlorothiazide’: 690, ’Plavix’: 974,
’Zocor’: 528},{’Advair Diskus’: 46, ’Gabapentin’: 84,
’Prilosec’: 53, ’Metformin’: 95, ’Singulair’: 99,
’Hydrochlorothiazide’: 58, ’Plavix’: 85, ’Zocor’: 86}),
[Prescription(’Frank’, ’Plavix’, 932), Prescription(’Crissy’,
’Metformin’, 276), Prescription(’Ha
y’, ’Singulair’, 693),
Prescription(’Mike’, ’Crestor’, 476), Prescription(’Janet’,
’Metformin’, 912), Prescription(’Rachael’, ’Gabapentin’, 445),
Prescription(’Timmy’, ’Metformin’, 580), Prescription(’Timmy’,
’Gabapentin’, 685), Prescription(’Robert’, ’Singulair’, 488)])
211427
Updated pharmacy value
Pharmacy({’Advair Diskus’: 447, ’Gabapentin’: 257, ’Prilosec’:
511, ’Metformin’: 567, ’Singulair’: 25, ’Hydrochlorothiazide’:
690, ’Plavix’: 42, ’Zocor’: 528},{’Advair Diskus’: 46,
’Gabapentin’: 84, ’Prilosec’: 53, ’Metformin’: 95, ’Singulair’: 99,
’Hydrochlorothiazide’: 58, ’Plavix’: 85, ’Zocor’: 86})
CSE 101 – Spring 2019 Homework #5 5
Function Call Return Value
atch dispense(Pharmacy({’Pantoprazole’: 710, ’Glucophage’:
325, ’Amlodipine’: 521, ’Singulair’: 227, ’Lipitor’:
570, ’Gabapentin’: 447, ’Nexium’: 644, ’Zocor’: 686,
’Norvasc’: 553, ’Hydrocodone’: 284, ’Amoxicillin’: 418,
’Simvastatin’: 974, ’Seroquel’: 452},{’Pantoprazole’:
67, ’Glucophage’: 94, ’Amlodipine’: 93, ’Singulair’:
35, ’Lipitor’: 63, ’Gabapentin’: 50, ’Nexium’: 75,
’Zocor’: 40, ’Norvasc’: 27, ’Hydrocodone’: 47,
’Amoxicillin’: 85, ’Simvastatin’: 17, ’Seroquel’: 59}),
[Prescription(’Lisa’, ’Lipitor’, -16), Prescription(’Robert’,
’Hydrocodone’, 164), Prescription(’Frank’, ’Gabapentin’, 476),
Prescription(’Frank’, ’Zocor’, 580), Prescription(’Iris’,
’Pantoprazole’, 13), Prescription(’Iris’, ’Singulair’, 30),
Prescription(’Frank’, ’Nexium’, 179), Prescription(’Ba
’,
’Amoxicillin’, 216), Prescription(’Erin’, ’Seroquel’, 0),
Prescription(’Mike’, ’Gabapentin’, 315), Prescription(’Lisa’,
’Simvastatin’, 993)])
80364
Updated pharmacy value
Pharmacy({’Pantoprazole’: 697, ’Glucophage’: 325, ’Amlodipine’: 521,
’Singulair’: 197, ’Lipitor’: 570, ’Gabapentin’: 132, ’Nexium’: 465,
’Zocor’: 106, ’Norvasc’: 553, ’Hydrocodone’: 120, ’Amoxicillin’:
202, ’Simvastatin’: 974, ’Seroquel’: 452},{’Pantoprazole’: 67,
’Glucophage’: 94, ’Amlodipine’: 93, ’Singulair’: 35, ’Lipitor’:
63, ’Gabapentin’: 50, ’Nexium’: 75, ’Zocor’: 40, ’Norvasc’: 27,
’Hydrocodone’: 47, ’Amoxicillin’: 85, ’Simvastatin’: 17