Drill

ProblemsPython › dates

Say a duration the way a person would

mediumdatesPython

A ticket screen shows how long something took, and "90m" reads worse than "1h 30m".

minutes_to_text(minutes: int) → string

Solve it in the editor →

Where you start

def minutes_to_text(minutes: int) -> str:
    

Worked examples

CallResult
minutes_to_text(90)"1h 30m"
minutes_to_text(120)"2h"
minutes_to_text(45)"45m"
minutes_to_text(0)"0m"

Hint

Work out both parts, then decide which ones to print.

Reference solution in Python
def minutes_to_text(minutes: int) -> str:
    if minutes <= 0:
        return '0m'
    h, m = divmod(minutes, 60)
    if h == 0:
        return str(m) + 'm'
    if m == 0:
        return str(h) + 'h'
    return str(h) + 'h ' + str(m) + 'm'

The same problem in another language

More dates problems in Python