Code of the Day
IntermediateAI for Problem Solving

Lab: Solve a real problem with AI

Work through a realistic multi-part coding problem using decomposition, structured prompting, integration, testing, and AI-assisted debugging.

Lab · optionalUsing AIIntermediate35 min

This lab is a mini project. You'll take a realistic problem specification, decompose it into sub-tasks, tackle each one with AI using the prompt crafting techniques from the previous module, integrate the pieces, write test cases, and debug any failures that come up.

You'll need an AI assistant and a Python environment where you can run code locally.

The problem

You are building a sales report generator. The input is a CSV file containing daily sales records. The output is a plain-text summary report.

Input format: A CSV file where each row has:

  • date — a date string in YYYY-MM-DD format
  • product — a product name (string)
  • quantity — an integer
  • unit_price — a float (price per item)

Output format: A plain-text report containing:

  1. Total revenue (sum of quantity * unit_price across all rows)
  2. Total number of transactions (rows)
  3. Best-selling product by total quantity sold
  4. Month with the highest total revenue (e.g., "2024-03")

Here is a small sample CSV to test with — save it as sales.csv:

date,product,quantity,unit_price
2024-03-01,Widget A,5,12.00
2024-03-01,Widget B,3,25.50
2024-03-15,Widget A,8,12.00
2024-04-02,Widget C,2,100.00
2024-04-10,Widget B,6,25.50
2024-04-10,Widget A,4,12.00

Expected output from this sample:

Total revenue: $560.00
Total transactions: 6
Best-selling product: Widget A (17 units)
Top month by revenue: 2024-04 ($380.00)

Step 1: Decompose the problem

Before talking to AI, write down your decomposition. What are the distinct sub-tasks?

Think about:

  • What needs to be read and parsed?
  • What computations need to happen? Are they independent or do they depend on each other?
  • What needs to be formatted and output?

A reasonable decomposition for this problem:

  1. Parse the CSV file into a list of records (dictionaries or a data structure of your choice)
  2. Calculate total revenue
  3. Calculate total transactions
  4. Find the best-selling product by quantity
  5. Find the month with highest revenue
  6. Format and print the report

Write out your decomposition before moving to Step 2. Even if it matches this one, the habit of thinking before prompting matters.


Step 2: Define your data structure

Before asking AI for any of the computation functions, decide on and define your data representation. Ask AI to help you write a clear data model:

"I'm writing a Python script that reads a CSV with columns: date (YYYY-MM-DD string), product (string), quantity (integer), unit_price (float). Write a Python dataclass or typed dictionary definition for a single sales record, with a note about what type each field should be after parsing."

Take the output and note what type the model chose for date — did it suggest parsing to a datetime.date object or keeping it as a string? Either works for this problem, but you should know your choice before writing computation functions, since it affects how you group by month.

Defining the data model first is the "natural seam" strategy from the decomposition lesson. Every subsequent sub-task will take a list of these records as input. Having a single, agreed-on definition prevents the mismatch bugs that come from each function having different assumptions about the data shape.


Step 3: Implement each sub-task with AI

Tackle each sub-task separately. For each one, write a prompt that includes:

  • The function's purpose
  • The input shape (reference your data model from Step 2)
  • The expected output
  • Any constraints or edge cases you want handled

Sub-task 1: CSV parser

Write a prompt asking for a function that reads sales.csv and returns a list of your data model objects. Specify: what happens if a row has a missing or malformed field? Ask for graceful error handling.

Sub-task 2: Revenue calculation

Write a prompt asking for a function that takes your list of records and returns total revenue as a float rounded to 2 decimal places. Ask the model to use chain-of-thought: "Before writing the function, explain the calculation step by step."

Sub-task 3: Best-selling product

Ask for a function that returns a tuple of (product_name, total_quantity) for the product with the highest total quantity sold across all records.

Sub-task 4: Month with highest revenue

This is the trickiest sub-task. Ask for a function that groups records by year-month and returns a tuple of (month_string, total_revenue) for the month with the highest total. Ask the model to handle the month grouping explicitly and show you the grouping logic.

Sub-task 5: Report formatter

Ask for a function that takes the four computed values as parameters and returns a formatted string matching the output format shown in the problem description.

For each function, verify independently that it produces the expected output for the sample data before moving on.


Step 4: Integrate the pieces

Write a main() function that calls all five sub-tasks in sequence and prints the final report. You can ask AI to help assemble this, or write it yourself — it should be straightforward given the well-scoped functions.

Run the full script on sales.csv and confirm the output matches the expected output exactly:

Total revenue: $560.00
Total transactions: 6
Best-selling product: Widget A (17 units)
Top month by revenue: 2024-04 ($380.00)

If it doesn't match, note which line is wrong — that tells you which sub-task to revisit.


Step 5: Write three test cases

Before moving to debugging, write at least three test cases that exercise edge cases:

Test case 1: An empty CSV (header row only). The script should handle this gracefully — not crash. What should the report say? Decide, then test it.

Test case 2: All transactions in the same month. The "top month" function should return that month.

Test case 3: A tie for best-selling product (two products with identical total quantities). What should happen? Ask AI how the current implementation handles ties — then decide if that's the behaviour you want.

Create a second CSV file for each test case and run the script. If a test case fails or produces unexpected behaviour, move to Step 6.


Step 6: Debug with AI if any test fails

If a test case produces incorrect output or a crash, structure a debugging request using the format from the debugging lesson:

  1. Paste the full error message and stack trace (if there is one)
  2. Paste the relevant function
  3. Describe what you expected vs. what happened
  4. State what you already know (e.g., "I confirmed the input CSV is correctly formatted")

Use the conversation to diagnose the root cause, not just get the error to disappear. Ask: "Is this a root cause fix or a workaround?" before accepting any suggested change.

If the tie-handling test (Test case 3) produces surprising behaviour, look carefully at what the implementation does. "Surprises" in edge cases are often a sign that the implementation has an implicit assumption that isn't true in all inputs. Understanding what that assumption is — not just patching the output — is the goal.


Step 7: Validate the AI output

After the script passes all test cases, do a line-by-line review of the computation functions. For each function, check:

  • Is there an assumption about data types that could fail with real-world data?
  • Is the rounding behaviour for revenue correct? (e.g., is it rounding to 2 decimal places throughout, or only in the formatter?)
  • Does the month-grouping function handle dates correctly across year boundaries?

Ask AI: "What edge cases in real sales data could cause any of these functions to fail or produce wrong results?" Review the list it produces and decide which ones are worth handling.


What you've practised

This lab applied the full problem-solving toolkit:

  • Decomposition — breaking the problem into independently implementable sub-tasks with clear interfaces
  • Data model first — defining the shared representation before any computation code
  • Structured prompting — using Task/Context/Format/Constraints on each sub-task
  • Chain-of-thought — requesting explicit reasoning on the trickiest sub-task
  • Integration — assembling sub-tasks into a working whole and verifying against known output
  • Test cases before debugging — writing edge case tests rather than hoping for correctness
  • Debugging with AI — using the four-part debugging request format when something fails
  • Validation — reviewing for assumptions and edge cases that tests didn't catch

These are the same steps you'd take on a real project. The problems are larger and the stakes are higher, but the loop is identical.

Finished reading? Mark it complete to track your progress.

On this page