Validate myReduce¶
Let us validate myReduce Function.
Compute order revenue for a given order id using order_items.
We will use myFilter to filter for the order items for the given order id.
Use myMap to extract order_item_subtotal. We will also convert data type of order_item_subtotal.
We can now compute order_revenue using myReduce Function.
%run 02_develop_myFilter.ipynb
%run 04_develop_myMap.ipynb
%run 06_develop_myReduce.ipynb
order_items_path = "/data/retail_db/order_items/part-00000"
order_items = open(order_items_path). \
read(). \
splitlines()
order_items[:10]
['1,1,957,1,299.98,299.98',
'2,2,1073,1,199.99,199.99',
'3,2,502,5,250.0,50.0',
'4,2,403,1,129.99,129.99',
'5,4,897,2,49.98,24.99',
'6,4,365,5,299.95,59.99',
'7,4,502,3,150.0,50.0',
'8,4,1014,4,199.92,49.98',
'9,5,957,1,299.98,299.98',
'10,5,365,5,299.95,59.99']
order_item = '2,2,1073,1,199.99,199.99'
int(order_item.split(',')[1]) == 2
True
order_items_filtered = myFilter(order_items,
lambda order_item: int(order_item.split(',')[1]) == 2
)
order_items_filtered
['2,2,1073,1,199.99,199.99', '3,2,502,5,250.0,50.0', '4,2,403,1,129.99,129.99']
order_item = '2,2,1073,1,199.99,199.99'
float(order_item.split(',')[4])
199.99
order_item_subtotals = myMap(order_items_filtered,
lambda order_item: float(order_item.split(',')[4])
)
order_item_subtotals
[199.99, 250.0, 129.99]
sum(order_item_subtotals)
579.98
myReduce(order_item_subtotals, lambda t, e: t + e)
579.98
myReduce(order_item_subtotals, lambda t, e: min(t, e))
129.99