【每日一题1102】时间翻译官

:woman_mage: 请编写一个函数,接收一个整数数字的秒数,返回可读的时间格式:HH:MM:SS。其中HH表示小时,范围是00-99;MM表示分钟数,范围是00-59;SS表示秒数,范围是00-59

示例:
输入:5,输出:00:00:05
输入:60,输出:00:01:00

题目难度:中等
题目来源:codewars: Human Readable Time

def time_trans(seconds: int) -> str:
    pass

assert time_trans(5) == "00:00:05"
assert time_trans(60) == "00:01:00"
assert time_trans(86399) == "23:59:59"
def time_trans(seconds):
    HH=0
    MM=0
    SS=0
    if seconds<60:
        SS=seconds
    if seconds>=60 and seconds<60*60:
        MM=seconds//60
        SS=seconds%60
    if seconds>=3600 and seconds<3600*100:
        HH=seconds//3600
        MM=seconds%3600//60
        SS=seconds%3600%60
    return "%02d:%02d:%02d" %(HH,MM,SS)
    
assert time_trans(5) == "00:00:05"
assert time_trans(60) == "00:01:00"
assert time_trans(86399) == "23:59:59"
def time_trans(seconds: int) -> str:
    m, s = divmod(seconds, 60)
    h, m = divmod(m,60)
    return("%02d:%02d:%02d" % (h, m, s))

assert time_trans(5) == "00:00:05"
assert time_trans(60) == "00:01:00"
assert time_trans(86399) == "23:59:59"
1 个赞
def time_trans(seconds: int) -> str:
    h = seconds // 3600
    m = seconds // 60 % 60
    s = seconds % 60
    return '{}:{}:{}'.format(str(h) if h >= 10 else '0' + str(h),
                             str(m) if m >= 10 else '0' + str(m),
                             str(s) if s >= 10 else '0' + str(s))


assert time_trans(5) == "00:00:05"
assert time_trans(60) == "00:01:00"
assert time_trans(86399) == "23:59:59"
def time_trans(seconds: int) -> str:
    hour = seconds // 3600
    minter = (seconds % 3600) // 60
    second = (seconds % 3600) % 60
    return ("%02d:%02d:%02d" %(hour,minter,second))

assert time_trans(5) == "00:00:05"
assert time_trans(60) == "00:01:00"
assert time_trans(86399) == "23:59:59"
1 个赞

def time_trans(seconds: int) → str:
h = seconds/(6060)%24
m = seconds%(60
60)//60%60
s = seconds%(60*60)%60
return “%02d:%02d:%02d”%(h,m,s)

assert time_trans(5) == “00:00:05”
assert time_trans(60) == “00:01:00”
assert time_trans(86399) == “23:59:59”

def time_trans(seconds: int) -> str:
    HH=00
    MM=00
    SS=00
    if seconds<60:
        SS=seconds
    elif 60<=seconds<3600:
        MM=seconds//60
        SS=seconds%60
    elif 3600<=seconds<60*60*100:
        HH=seconds//(60*60)
        MM=(seconds%(60*60))//60
        SS=(seconds%(60*60))%60
    return "%02d:%02d:%02d" %(HH,MM,SS)


assert time_trans(5) == "00:00:05"
assert time_trans(60) == "00:01:00"
assert time_trans(86399) == "23:59:59"

python一行解题大法

import time
def time_trans(seconds:int):
    return time.strftime('%H:%M:%S',time.gmtime(seconds))
def time_trans(seconds: int) -> str:
    HH= int(seconds/3600)
    MM = int(seconds%3600 / 60)
    SS = int(seconds%60)
    return  "%02d:%02d:%02d" %(HH,MM,SS)

assert time_trans(5) == "00:00:05"
assert time_trans(60) == "00:01:00"
assert time_trans(86399) == "23:59:59"
def time_trans(seconds: int) -> str:
    HH = seconds // 3600
    MM = seconds % 3600 // 60
    SS = seconds % 60
    t1 = [str(HH), str(MM), str(SS)]
    t2 = []
    for i in t1:
        if len(i) == 1:
            i = '0' + i
            t2.append(i)
        else:
            t2.append(i)

    return ":".join(t2)


assert time_trans(5) == "00:00:05"
assert time_trans(60) == "00:01:00"
assert time_trans(86399) == "23:59:59"