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.
Most of the solutions im seeing are overdoing it or using packages (which imo is lowkey cheating LOL) but here's how i kept it beginner friendly 🫡 This also has a time complexity of O(1) iirc
deftimeConversion(s):Hours=int(s[0:2])#indexingthehoursMinutesSeconds=s[2:8]AMPM=s[8:]#indexingtheAM/PMifAMPM=="AM":#ifitsintheAMifHours==12:Hours=0else:#whenitsNOTAMsoithastobePMifHours!=12:#whilewe'rebetweenthehoursof01pm-11pmHours+=12#add12toHHreturnf"{Hours:02}{MinutesSeconds}"#f"{Hours:02} means that the HH should always have two integers, so if it were to be 7:42:41AM, that translates to 07:42:41 where it would be invalid without the 0#MinutesSeconds just reserves the mm:ss since we're not touching that, so we can join it together via formatting
with comments because those are important
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Time Conversion
You are viewing a single comment's thread. Return to all comments →
Most of the solutions im seeing are overdoing it or using packages (which imo is lowkey cheating LOL) but here's how i kept it beginner friendly 🫡 This also has a time complexity of O(1) iirc
with comments because those are important