Drill

ProblemsPython › pricing

Week-on-week price change

easypricingPython

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

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

Solve it in the editor →

Where you start

def percent_change(before: int, after: int) -> float:
    

Worked examples

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

Hint

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

Reference solution in Python
def percent_change(before: int, after: int) -> float:
    if before == 0:
        return 0.0
    pct = (after - before) * 100 / before
    return math.floor(pct * 10 + 0.5) / 10 if pct >= 0 else -(math.floor(-pct * 10 + 0.5) / 10)

The same problem in another language

More pricing problems in Python