Human-Readable Sizes and Durations

easy
Unit 1 · Strings I0/3 to milestone

Dashboards and CLIs should say 1.5 MB and 2h 5m, not 1572864 and 7500.

Implement:

  • human_bytes(n): use 1024-based units B, KB, MB, GB and TB. Pick the largest unit where the value is at least 1, show one decimal place, and drop a trailing .0. Bytes are always whole: "512 B". Examples: 1536 → "1.5 KB", 1048576 → "1 MB".
  • human_duration(seconds): split a whole number of seconds into days, hours, minutes and seconds (d, h, m, s). Show the largest non-zero unit, followed by the next unit down if that one is non-zero. 0 → "0s", 3725 → "1h 2m", 3600 → "1h", 90061 → "1d 1h", 65 → "1m 5s".

Raise ValueError for negative input.

#strings#formatting2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Human-Readable Sizes and Durations · TechGradify