Drill

ProblemsGo › pricing

Week-on-week price change

easypricingMathGo

A pricing dashboard shows how far each item moved since last week, as a percentage.

percentChange(before: int, after: int) → float

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 percentChange(before int, after int) float64 {
	
}

Worked examples

CallResult
percentChange(100, 125)25.0
percentChange(200, 150)-25.0
percentChange(300, 310)3.3
percentChange(100, 100)0.0

Hint

difference / before * 100, then round to one decimal by multiplying by 10, rounding, and dividing back.

Reference solution in Go
func percentChange(before int, after int) float64 {
	if before == 0 {
		return 0
	}
	pct := float64(after-before) * 100 / float64(before)
	return math.Round(pct*10) / 10
}

The same problem in another language

More pricing problems in Go