We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def timeConversion(s):
period = s[-2:] #last two digits = pm/am
time = s[:-2] #without last two digits = time
hms = list(map(int, time.split(':'))) #splits the list and converts str into int in one line
if period == 'AM':
if hms[0] == 12:
hms[0] -= 12
if period == 'PM':
if not hms[0] == 12:
hms[0] += 12
hms = [f"{i:02}" for i in hms] #makes sure only two digits allowed
return ":".join(hms) #reproducing string
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Time Conversion
You are viewing a single comment's thread. Return to all comments →
def timeConversion(s): period = s[-2:] #last two digits = pm/am time = s[:-2] #without last two digits = time hms = list(map(int, time.split(':'))) #splits the list and converts str into int in one line