Discount, but never below the floor
Sales can discount freely, except that a contract sets a price the item may never go under.
- Apply the percentage, then lift the result back up to the floor if it went under.
- A floor above the list price wins — the item simply sells at the floor.
- A percentage outside 1 to 100 means no discount at all.
DiscountWithFloor(price: int, percent: int, floorPrice: int) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int DiscountWithFloor(int price, int percent, int floorPrice) {
}
Worked examples
| Call | Result |
|---|---|
DiscountWithFloor(1000, 20, 500) | 800 |
DiscountWithFloor(1000, 70, 500) | 500 |
DiscountWithFloor(1000, 20, 1200) | 1200 |
DiscountWithFloor(1000, 0, 500) | 1000 |
Hint
Two steps, in order: discount, then clamp.
Reference solution in C#
public int DiscountWithFloor(int price, int percent, int floorPrice) {
int cut = (percent < 1 || percent > 100) ? price : price - (price * percent) / 100;
return Math.Max(cut, floorPrice);
}