Tokens for the Fair

budget set · budget line · nested loops · counting

Ines takes income coins to the village fair. A ride costs px coins and a bag of roasted nuts costs py coins. A bundle is a pair (x, y): x rides and y bags, both whole numbers, at least 0. The bundle costs

p_x \, x + p_y \, y

It is affordable when that cost is at most income. All affordable bundles together form Ines's budget set; the affordable bundles that spend the income exactly lie on her budget line. Buying nothing, (0, 0), is always affordable.

Write fair_bundles(income, px, py) that returns a tuple (affordable, exact): the number of affordable bundles, and how many of them cost exactly income.

Collect the affordable bundles in two lists xs and ys and press Run with plot(xs, ys, kind="scatter") to see the budget set as a triangle of dots.

Examples

Input:  income = 10, px = 3, py = 2
Output: (14, 2)
Explanation: with 0 rides she can buy 0 to 5 bags (6 bundles), with 1 ride 0 to 3
(4 bundles), with 2 rides 0 to 2 (3), with 3 rides only 0 bags (1): 14 in all.
(0, 5) and (2, 2) cost exactly 10.

Input:  income = 0, px = 4, py = 1
Output: (1, 1)
Explanation: only (0, 0), which costs exactly 0.

Input:  income = 5, px = 7, py = 6
Output: (1, 0)

Goals

  • Test whether a bundle of two goods is affordable: $p_x x + p_y y \le m$
  • Count every affordable whole-unit bundle, including the empty one
  • Tell the budget set (spend at most $m$) from the budget line (spend exactly $m$)
Starting Python…