Skip to main content

Time Conversion


Given a time in -hour AM/PM format, convert it to military (24-hour) time.

Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

#!/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(s):
    if s[-2] == "A" and s[0:2] == "12":
        return("00" + s[2:-2])
    elif s[-2] == "P" and s[0:2] == "12" or s[-2] == "A":
        return(str(s[:-2]))
    elif s[-2] == "P":
        return(str(int(s[:2])+12)+s[2:-2])
        
    # Write your code here

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

    s = input()

    result = timeConversion(s)

    fptr.write(result + '\n')

    fptr.close()


Comments

Popular posts from this blog

Magic constant generator -python3/hacker rank solution / tcs fresco play

  def   generator_Magic ( n1 ):      # the value starts from 3 and m is formula for constant,      #for generator  yield should use      for   a   in   range ( 3 , n1 + 1 ):          m = a * ( a ** 2 + 1 ) / 2          yield   m               # Write your code here if   __name__  ==  '__main__' :

Getting Started with NumPy: A Comprehensive Guide

  NumPy is a fundamental library for scientific computing with Python. It provides support for large, multi-dimensional arrays and matrices, along with mathematical functions to operate on these arrays. This tutorial will introduce you to the basics of NumPy and its core functionalities. Installing NumPy Before diving into NumPy, make sure you have it installed. You can install NumPy using the following command: pip install numpy pip install numpy Importing NumPy Once installed, you can import NumPy in your Python script or Jupyter notebook as follows: import numpy as np Now, let's explore some of the essential features of NumPy. NumPy Arrays NumPy's primary data structure is the array. An array is a grid of values, and it can be one-dimensional or multi-dimensional. Here's how you can create a simple one-dimensional array: import numpy as np # Creating a one-dimensional array arr1 = np.array([1, 2, 3, 4, 5]) print(arr1) For multi-dimensional arrays, you can use nested ...

Difference between Numpy and Python list

  NumPy and Python lists are both data structures used to store collections of data, but they have several key differences: 1. Homogeneity:    - NumPy arrays are homogeneous, meaning that all elements in a NumPy array must have the same data type (e.g., all integers, all floating-point numbers). This homogeneity allows for efficient, element-wise operations.    - Python lists can contain elements of different data types, providing more flexibility but potentially sacrificing performance. 2. Performance:    - NumPy is optimized for numerical operations and is typically faster than Python lists when performing element-wise operations (e.g., addition, multiplication) on large datasets. This is due to the homogeneous nature of NumPy arrays and the fact that NumPy operations are implemented in C and can take advantage of low-level optimizations.    - Python lists are more versatile but are generally slower for numerical computations compared to Nu...