Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python function using numpy Task 2.4: Computing Number of Purchaseable Masks Our fourth task is to implement compute_n_masks_purchaseable. This function takes two arguments, namely healthcare_spending

Python function using numpy

Task 2.4: Computing Number of Purchaseable Masks

Our fourth task is to implement compute_n_masks_purchaseable. This function takes two arguments, namely healthcare_spending and mask_prices, and it should compute the total number of masks that each country can purchase if she spends all her emergency healthcare spending on masks.

Assume that all countries bought the masks at the global average costs, and to reduce administrative hassle, they only buy masks on day (+1)(+1) with their emergency funds on day (+1)(+1), i.e. the masks that are bought on a particular day are not purchased with funding from the previous days.

The return value should be the total number of masks, which each country can purchase, represented as a 1D np.ndarray such that the -th entry corresponds to the total number of masks purchaseable by the -th country as represented in healthcare_spending.

For example, if we have healthcare_spending = np.array([[0, 100, 0], [100, 0, 200]]) and mask_prices = np.array([4, 3, 20]). Then, we expect the return value to be np.array([3300. 3500.]). This is because country 0 can only buy 33100=330033100=3300 masks on day 2 with 1001003 and are sold in batches of 100. Similarly, we find that country 1 can only buy (25+10)100=3500(25+10)100=3500 masks.

In this task, you may use the np.sum and the np.floor functions.

In [ ]:

 
def compute_n_masks_purchaseable(healthcare_spending, mask_prices):
 '''
 Computes the total number of masks that each country can purchase if she
 spends all her emergency healthcare spending on masks.
 Parameters
 ----------
 healthcare_spending: np.ndarray
 2D `ndarray` with each row representing the data of a country, and the columns
 of each row representing the time series data of the emergency healthcare
 spending made by that country, i.e. the ith row of `healthcare_spending`
 contains the data of the ith country, and the (i, j) entry of
 `healthcare_spending` is the amount which the ith country spent on healthcare
 on (j + 1)th day.
 mask_prices: np.ndarray
 1D `ndarray` such that the jth entry represents the cost of 100 masks on the
 (j + 1)th day.
 
 Returns
 -------
 Total number of masks which each country can purchase as a 1D `ndarray` such
 that the ith entry corresponds to the total number of masks purchaseable by the
 ith country as represented in `healthcare_spending`.
 Note
 ----
 The masks can only be bought in batches of 100s.
 '''
 
 # TODO: add your solution here and remove `raise NotImplementedError`
 raise NotImplementedError

In [ ]:

 
# Test case for Task 2.4
prices_constant = np.ones(5)
healthcare_spending_constant = np.ones((7, 5))
actual = compute_n_masks_purchaseable(healthcare_spending_constant, prices_constant)
expected = np.ones(7) * 500
assert(np.all(actual == expected))
 
healthcare_spending = np.array([[0, 100, 0], [100, 0, 200]])
mask_prices = np.array([4, 3, 20])
expected2 = np.array([3300, 3500])
assert(np.all(compute_n_masks_purchaseable(healthcare_spending, mask_prices)==expected2))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Find the value of combination. 10C2

Answered: 1 week ago

Question

sharing of non-material benefits such as time and affection;

Answered: 1 week ago