Approach

  Blog    |     February 03, 2026

To complete the unfinished order by filling in missing quantities and prices using the available store products, follow these steps:

  1. Create a Store Dictionary: Convert the store list into a dictionary where each key is a product name, and the value is the product details (quantity and price). This allows O(1) lookups for each product in the order.
  2. Process Each Order Item: For each item in the order:
    • Check Store Availability: If the product exists in the store dictionary:
      • Fill Missing Quantity: If the order item lacks a quantity (key missing or None), replace it with the store's quantity.
      • Fill Missing Price: If the order item lacks a price (key missing or None), replace it with the store's price.
    • Skip Unavailable Products: If the product is not found in the store, leave the item unchanged.

Solution Code

def complete_order(order, store):
    store_dict = {product['name']: product for product in store}
    for item in order:
        product_name = item['name']
        if product_name in store_dict:
            store_item = store_dict[product_name]
            if 'quantity' not in item or item['quantity'] is None:
                item['quantity'] = store_item['quantity']
            if 'price' not in item or item['price'] is None:
                item['price'] = store_item['price']
    return order

Explanation

  1. Store Dictionary: The store list is converted into a dictionary (store_dict) for efficient product lookups. Each product name maps to its details (quantity and price).
  2. Order Processing:
    • For each item in the order, check if its name exists in store_dict.
    • If found, update the item's quantity if it's missing (key absent or None), using the store's quantity.
    • Similarly, update the item's price if missing, using the store's price.
    • If the product is not in the store, the item remains unchanged.
  3. Result: The modified order list is returned, with missing quantities and prices filled from the store where possible.

This approach efficiently leverages dictionary lookups to minimize processing time, ensuring the order is completed accurately based on available store products.


Request an On-site Audit / Inquiry

SSL Secured Inquiry