Time Conversion

  • + 0 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

    def timeConversion(s):
        Hours = int(s[0:2]) #indexing the hours
        MinutesSeconds = s[2:8]
        AMPM = s[8:] #indexing the AM/PM
        
        if AMPM == "AM": #if its in the AM
            if Hours == 12:
                Hours = 0
        else: #when its NOT AM so it has to be PM
            if Hours != 12 : #while we're between the hours of 01pm - 11pm   
                 Hours += 12 # add 12 to HH
    
        return f"{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