Time Conversion

  • + 0 comments

    complete python3 code with comments :::::::::

    !/bin/python3

    import math import os import random import re import sys

    #

    Complete the 'timeConversion' function below.

    #

    The function is expected to return a STRING.

    The function accepts STRING s as parameter.

    #

    def timeConversion(time_str): # Write your code here time_parts = time_str[:-2].split(':') seconds = time_parts[-1] period = time_str[-2:]

    # Convert hours, minutes, and seconds to integers
    hours = int(time_parts[0])
    minutes = int(time_parts[1])
    seconds = int(seconds)
    
    # Convert 12-hour AM/PM time to military time
    if period == 'PM' and hours < 12:
        hours += 12
    elif period == 'AM' and hours == 12:
        hours = 0
    
    # Format hours, minutes, and seconds as 2-digit strings
    hours_str = str(hours).zfill(2)
    minutes_str = str(minutes).zfill(2)
    seconds_str = str(seconds).zfill(2)
    
    # Return the military time string
    military_time_str = f"{hours_str}:{minutes_str}:{seconds_str}"
    return military_time_str
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()
    
    result = timeConversion(s)
    
    fptr.write(result + '\n')
    
    fptr.close()