dgadiraju
12/16/2017 - 10:13 AM

GetRevenueForOrderId.py

def getRevenueForOrderIdUsingLoops(orderItems, orderId):
  totalRevenue = 0.0
  for oi in orderItems:
    if(int(oi.split(",")[1]) == orderId):
      totalRevenue = totalRevenue + float(oi.split(",")[4])
  return totalRevenue

orderItems = open("/data/retail_db/order_items/part-00000").read().splitlines()
getRevenueForOrderIdUsingLoops(orderItems, 68883)

def getRevenueForOrderId(orderItems, orderId):
  orderItemsFiltered = filter(lambda oi: int(oi.split(",")[1]) == orderId, orderItems)
  orderItemsMap = map(lambda oi: float(oi.split(",")[4]), orderItemsFiltered)
  totalRevenue = reduce(lambda t, v: t + v, orderItemsMap)
  return totalRevenue

orderItems = open("/data/retail_db/order_items/part-00000").read().splitlines()
getRevenueForOrderId(orderItems, 68883)