Supplier Analytics
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Description
The folks at Farmers United Network (FUN) thought you did such a great job last week that they have given you another task! This time, you'll be helping them make sense of a long list of supplier invoices (conveniently formatted as a list of tuples) that contain an item and the quantity (in metric tons) that various suppliers delivered.
Given the descriptions of the functions below, create a program that implements these functions. An example list of orders that will serve as an input to the functions is provided below:
orders = [('carrots', 12), ('tomatoes', 7), ('tomatoes', 1), ('carrots', 1),
('carrots', 2)]
process_items(orders)
- Given a list of orders, create a mapping between the item and list of quantities of that item which have been delivered.
print(process_items(orders))
# {'carrots': [12, 1, 2], 'tomatoes': [7, 1]}
process_stats(orders)
- Given a list of orders, find and return a dictionary that maps the item name to a dictionary containing the keys: "median", "average", and "std" and the associated values across all deliveries of that item.
print(process_stats(orders))
#{'carrots': {'median': 2.0, 'average': 5.0, 'std': 4.96655480858378},
# 'tomatoes': {'median': 4.0, 'average': 4.0, 'std': 3.0}}
best_item(orders, stat)
- Given a list of orders, find and return the name of the item with the maximum desired stat, where stat is either "median", "average", or "std".
print(best_item(orders, 'median'))
#'tomatoes'
Notes:
-
For this assignment you are allowed to
import numpy as np
at the top of your file. Link to relevant reading. -
Look for opportunities to re-use code where possible (you can call functions you have previously written in other functions!)
-
If you are using the statistics module to calculate the standard deviation, you may get slightly different results. This is likely because numpy is calculating the population standard deviation and statistics.stdev calculates the sample standard deviation. The server should now accept both kinds of calculations!
-
Update: The numpy import is now working on the server! However, the server does not like numpy.float64 numbers for some reason, so make sure to cast the results of your calculation to a float.
For example:
>>> x = np.median([1,2,3])
>>> x
2.0
>>> type(x) # server will be sad :(
<class 'numpy.float64'>
>>> x = float(np.median([1,2,3]))
>>> x
2.0
>>> type(x) # server will be happy!
<class 'float'>
When you are confident that your program is working correctly, submit it below for checking.
Next Exercise: Comparisons