it says the following:
Write a function that converts the time to 24hr format.
Examples
>>> time24hr('12:34am')
'0034hr'
>>> time24hr('12:15pm')
'1215hr'
so i wrote the following:
def time24hr(tstr):
a = tstr.split(':')
am = {'12':'00','1':'01','2':'02','3':'03','4':'04','5':'05','6':'06',
'7':'07','8':'08','9':'09','10':'10','11':'11'}
pm = {'12':'12','1':'13','2':'14','3':'15','4':'16','5':'17','6':'18',
'7':'19','8':'20','9':'21','10':'22','11':'23'}
if 'am' in tstr:
return am[a[0]] + a[1][:2] + 'hr'
elif 'pm' in tstr:
return pm[a[0]] + a[1][:2] + 'hr'
But what would have been a better way to do it? i imagine their is an inbuilt method of doing this, but is there a more efficient way to do this without inbuilts?
and could you show any inbuilt ways too, would be much appreciated.