Drill

ProblemsC# › dates

Say a duration the way a person would

mediumdatesMathStringsC#

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

MinutesToText(minutes: int) → string

C# 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

public string MinutesToText(int minutes) {
    
}

Worked examples

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

Hint

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

Reference solution in C#
public string MinutesToText(int minutes) {
    if (minutes <= 0) return "0m";
    int h = minutes / 60, m = minutes % 60;
    if (h == 0) return m + "m";
    if (m == 0) return h + "h";
    return h + "h " + m + "m";
}

The same problem in another language

More dates problems in C#