Drill

ProblemsGo › pricing

Apply a promo percentage

easypricingMathGo

The till applies a promo code to a price held in minor units — kuruş, cents, whatever the currency splits into.

discountedPrice(price: int, percent: 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 discountedPrice(price int, percent int) int {
	
}

Worked examples

CallResult
discountedPrice(1000, 20)800
discountedPrice(999, 10)900
discountedPrice(1000, 0)1000
discountedPrice(1000, 150)1000

Hint

Reject the bad range first, then integer-divide.

Reference solution in Go
func discountedPrice(price int, percent int) int {
	if percent < 1 || percent > 100 {
		return price
	}
	return price - price*percent/100
}

The same problem in another language

More pricing problems in Go