Drill

ProblemsGo › pricing

Discount, but never below the floor

easypricingMathGo

Sales can discount freely, except that a contract sets a price the item may never go under.

discountWithFloor(price: int, percent: int, floorPrice: int) → int

Go 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.

Solve it in Python →

Where you start

func discountWithFloor(price int, percent int, floorPrice int) int {
	
}

Worked examples

CallResult
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 Go
func discountWithFloor(price int, percent int, floorPrice int) int {
	cut := price
	if percent >= 1 && percent <= 100 {
		cut = price - price*percent/100
	}
	if cut < floorPrice {
		return floorPrice
	}
	return cut
}

The same problem in another language

More pricing problems in Go