알고리즘 문제/CodeWar

Human Readable Time

BEstyle 2022. 10. 5. 13:07

DESCRIPTION:

Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)

  • HH = hours, padded to 2 digits, range: 00 - 99
  • MM = minutes, padded to 2 digits, range: 00 - 59
  • SS = seconds, padded to 2 digits, range: 00 - 59

The maximum time never exceeds 359999 (99:59:59)

You can find some examples in the test fixtures.


def make_readable(seconds):
    hour=0
    mins=0
    while seconds>=60:
        seconds-=60
        mins+=1
        if mins>=60:
            hour+=1
            mins-=60
    if hour<10:
        hour='0'+str(hour)
    else:
        hour=str(hour)
    if mins<10:
        mins='0'+str(mins)
    else:
        mins=str(mins)
    if seconds<10:
        seconds='0'+str(seconds)
    else:
        seconds=str(seconds)
    return (hour+ ":" + mins +":"+ seconds)