The Pottery's Cost Sheet

total cost · average cost · marginal cost · fixed cost · lists

A small pottery knows what a day of work costs at each output. costs[q] is the total cost, in dollars, of making q mugs in a day, for q = 0, 1, ..., n. costs[0] is the fixed cost (the kiln lease), paid even on a day with no mugs.

From the table, two more costs per mug follow, for each output q = 1, ..., n:

  • the average cost AC(q) = \dfrac{C(q)}{q}, the cost per mug when making q of them;
  • the marginal cost MC(q) = C(q) - C(q-1), what the q-th mug adds to the total.

Write cost_sheet(costs) that returns a tuple (average, marginal, cheapest):

  1. average: the list of average costs for q = 1..n (so n numbers);
  2. marginal: the list of marginal costs for q = 1..n (n numbers);
  3. cheapest: the output q (between 1 and n) with the lowest average cost; if several outputs tie for the lowest, the smallest such q.

Press Run with plot(list(range(1, len(costs))), average) and the same for marginal (with new=False) to see the marginal curve cut through the bottom of the average one.

Examples

Input:  costs = [60, 90, 110, 126, 150, 185, 230]
Output: ([90.0, 55.0, 42.0, 37.5, 37.0, 38.333333333333336], [30, 20, 16, 24, 35, 45], 5)
Explanation: 5 mugs cost 185, or 37 per mug, the lowest. The 6th mug adds 45,
more than the average of 37, so it pulls the average up.

Input:  costs = [12, 20, 24, 36]
Output: ([20.0, 12.0, 12.0], [8, 4, 12], 2)
Explanation: 2 and 3 mugs both cost 12 per mug; the smaller output, 2, is returned.

Constraints

  • the returned averages are graded with a tolerance of 1e-6; the cheapest output is decided on the averages exactly as computed, C(q) / q (ties go to the smallest output)

Goals

  • Read average and marginal cost off a table of total costs
  • Find the output with the lowest average cost, breaking ties as stated
  • See that marginal cost crosses average cost where average cost is lowest
Starting Python…