Skip to content

S2Q1 · Process Orders Aggregation with Threshold

⚡ Quick Reference

Function: process_orders(data: list, min_quantity: int) -> dict

Core idea: for each customer, extract quantities, compute aggregations, include only if total ≥ threshold.

def process_orders(data: list, min_quantity: int) -> dict:
    result = {}
    for record in data:
        customer = record["customer"]
        qtys = [o["qty"] for o in record["orders"]]
        if not qtys:
            continue
        total = sum(qtys)
        if total >= min_quantity:
            result[customer] = {
                "total":   total,
                "count":   len(qtys),
                "average": total / len(qtys),
                "max":     max(qtys),
            }
    return result

Key rules: - Skip customers with no orders (avoid division by zero) - Include only if total >= min_quantity - average = total / count as a float - max = largest single order quantity


Problem Statement

Problem

Write a function process_orders(data, min_quantity) that processes customer order records and returns aggregation statistics for customers whose total quantity meets the threshold.

Example:

data = [
    {'customer': 'c1', 'orders': [{'qty': 2}, {'qty': 5}]},
    {'customer': 'c2', 'orders': [{'qty': 1}, {'qty': 1}]},
]
process_orders(data, 5)
Output
{'c1': {'total': 7, 'count': 2, 'average': 3.5, 'max': 5}}

c2 total = 2 < 5 → excluded. c1 total = 7 ≥ 5 → included.


Understanding the problem

For each customer record:

  1. Extract all quantities: [o["qty"] for o in record["orders"]]
  2. Skip if no orders (len == 0)
  3. Compute total, count, average, max
  4. Add to result only if total >= min_quantity
c1: qtys = [2, 5]
    total = 7,  count = 2,  avg = 3.5,  max = 5
    7 >= 5 → include ✓

c2: qtys = [1, 1]
    total = 2,  count = 2,  avg = 1.0,  max = 1
    2 >= 5 → exclude ✗

Solution approaches

def process_orders(data: list, min_quantity: int) -> dict:
    result = {}
    for record in data:
        qtys = [o["qty"] for o in record["orders"]]
        if not qtys:
            continue
        total = sum(qtys)
        if total >= min_quantity:
            result[record["customer"]] = {
                "total":   total,
                "count":   len(qtys),
                "average": total / len(qtys),
                "max":     max(qtys),
            }
    return result
def process_orders(data: list, min_quantity: int) -> dict:
    result = {}
    for record in data:
        customer = record["customer"]
        orders   = record["orders"]

        if len(orders) == 0:
            continue

        total = 0
        maximum = orders[0]["qty"]
        for order in orders:
            qty = order["qty"]
            total += qty
            if qty > maximum:
                maximum = qty

        if total >= min_quantity:
            result[customer] = {
                "total":   total,
                "count":   len(orders),
                "average": total / len(orders),
                "max":     maximum,
            }
    return result
def process_orders(data: list, min_quantity: int) -> dict:
    def aggregate(record):
        qtys = [o["qty"] for o in record["orders"]]
        if not qtys:
            return None
        total = sum(qtys)
        if total < min_quantity:
            return None
        return {
            "total":   total,
            "count":   len(qtys),
            "average": total / len(qtys),
            "max":     max(qtys),
        }

    return {
        r["customer"]: agg
        for r in data
        if (agg := aggregate(r)) is not None
    }

Uses a walrus operator (:=) in the dict comprehension to call aggregate once and filter out None results in one pass.

def process_orders(data: list, min_quantity: int) -> dict:
    get_qtys = lambda r: [o["qty"] for o in r["orders"]]

    valid = filter(lambda r: len(get_qtys(r)) > 0 and sum(get_qtys(r)) >= min_quantity, data)

    def make_agg(r):
        qtys = get_qtys(r)
        total = sum(qtys)
        return (r["customer"], {
            "total":   total,
            "count":   len(qtys),
            "average": total / len(qtys),
            "max":     max(qtys),
        })

    return dict(map(make_agg, valid))

filter selects qualifying customers; map builds the (key, value) pairs; dict() assembles the result.


Key takeaways

01

Extract all quantities first

Building qtys = [o["qty"] for o in orders] upfront lets you call sum(), len(), and max() on the same list without iterating the orders multiple times.

02

Skip empty orders before dividing

Always check if not qtys: continue before computing the average. Dividing by zero raises a ZeroDivisionError - guarding against empty order lists keeps the function safe.

03

Walrus operator for one-pass filtering

if (agg := aggregate(r)) is not None computes and assigns the aggregation in the filter condition itself - no need to call the function twice or use a temporary variable outside the comprehension.