Drill

ProblemsGo › dates

Say a duration the way a person would

mediumdatesMathStringsGo

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

minutesToText(minutes: int) → string

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 minutesToText(minutes int) string {
	
}

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 Go
func minutesToText(minutes int) string {
	if minutes <= 0 {
		return "0m"
	}
	h, m := minutes/60, minutes%60
	if h == 0 {
		return strconv.Itoa(m) + "m"
	}
	if m == 0 {
		return strconv.Itoa(h) + "h"
	}
	return strconv.Itoa(h) + "h " + strconv.Itoa(m) + "m"
}

The same problem in another language

More dates problems in Go