Drill

ProblemsGo › billing

Compute the final charge on an order

easybillingMathGo

Combine a subtotal, an optional discount, and shipping to produce the final amount due.

finalCharge(subtotal: int, discount: int, shipping: 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 finalCharge(subtotal int, discount int, shipping int) int {
	
}

Worked examples

CallResult
finalCharge(5000, 500, 800)5300
finalCharge(1000, 2000, 500)500
finalCharge(3000, 0, 1000)4000
finalCharge(0, 0, 0)0

Hint

Max(0, subtotal − discount) then add shipping.

Reference solution in Go
func finalCharge(subtotal int, discount int, shipping int) int {
	net := subtotal - discount
	if net < 0 {
		net = 0
	}
	return net + shipping
}

The same problem in another language

More billing problems in Go