Say a duration the way a person would
A ticket screen shows how long something took, and "90m" reads worse than "1h 30m".
- Hours and minutes, separated by a single space: 90 becomes "1h 30m".
- Drop the part that is zero: 120 is "2h", 45 is "45m".
- Zero, or anything negative, is "0m".
minutes_to_text(minutes: int) → string
Where you start
def minutes_to_text(minutes: int) -> str:
Worked examples
| Call | Result |
|---|---|
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'