Int 3 for Programmers

What to charge before fees

What to charge before fees

A marketplace takes a percentage of whatever you charge, and then a flat fee on top. If you list an item at pp pence, what you keep is given by a function of pp.

keep(p)=p−f100p−c\text{keep}(p) = p - \frac{f}{100}p - c
f is the percentage fee and c is the flat fee, both fixed for a given sale.

Sellers need that function run backwards. Write gross_needed(target, fee_percent, flat), which returns the smallest whole number of pence to list the item at so that the amount kept is at least target.

Worked example, with target = 1000, fee_percent = 10 and flat = 50. Keeping at least 1000 means p−0.1p−50≥1000p - 0.1p - 50 \ge 1000, so 0.9p≥10500.9p \ge 1050 and p≥1166.67p \ge 1166.67. Prices are whole pence, so round up: the answer is 1167. Checking, listing at 1167 keeps 1167−116.7−50=1000.31167 - 116.7 - 50 = 1000.3 pence, which clears the target, while 1166 keeps 999.4, which does not.

  • Constraints: target and flat are between 0 and 100000, and fee_percent is between 0 and 99.
  • Work out the fee from the exact price rather than rounding it first: the check above uses 116.7 pence, not 117.
  • Round the final price up, never down. Rounding down leaves the seller a fraction of a penny short.
  • A target of 0 with no flat fee needs no price at all, so the answer there is 0.
  • Whole-number arithmetic avoids every rounding argument here: the condition is p(100−f)≥100(target+c)p(100 - f) \ge 100(\text{target} + c).
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.