Time Conversion Algorithm🕑🕝
This algorithm is a part of HackerRank's algo warmup challenges.

In this article, I'll be describing how I solved the Time Conversion Challenge on HackerRank using Python 3.
This challenge is part of the Warmup challenges for the Algorithm section on HackerRank.
Problem 🎲
Given a time in 12-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.
Example
- s = '12:01:00PM':
Return '12:01:00'.
- s = '12:01:00AM':
Return '00:01:00'.
Function Description
Complete the timeConversion function in the editor. It should return a new string representing the input time in 24-hour format.
timeConversion has the following parameter(s):
- string
s: a time in 12-hour format
Returns
- string: the time in 24-hour format
Input Format
A single string s that represents a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM)
Constraints
- All input times are valid
Samle Input:
'07:05:45PM'
Samle Ouput:
'19:05:45'
Solution 🔎
As always, we will start by breaking the problem into smaller bits and solving each bit synchronously.
Let's assume
s = '12:01:00AM'to test our solution.
def timeConversion(s):
# Write your code here
Step 1️⃣ : Slicing and splitting the input string into two parts. 🔪
- Since we are given the time format in a string of constant length, we can split all characters minus the last two (i.e.
PMorAM). - This means splitting the string
sby the delimiter:and slicing the first 8 characters. - Let's store the sliced array showing only digits in a variable,
a. - and store the time format part of the array in a variable,
tf.a = s[:8].split(":") >> ['12', '01', '00'] tf = s[-2:] >> AM
Step 2️⃣: String array to int array ⚙️
- We need to convert this string array (containing digits) into an integer array, so we can easily manipulate them in our time conversion logic 🤓.
- To achieve this, we use python's map function.
map(function, iterable, ...)returns an iterator that applies afunctionto every item ofiterable, yielding the result.
a = list(map(int, a))
>> [12, 1, 0]
- Here, we called the
intfunction on every item in the array and converted the result to alist.
Step 3️⃣: Time conversion logic time!⏱️
- We use conditional statements to implement our logic.
- Here, we'll do the following:
- Return
hh + 12, if the time isPMand not12(all digits from0-11),example: return
17if its5PM. - Return
hh - 12, if the time isAMand12,example: return
0if its12AM. - Return the same value in all other cases (example in
mmdigits).if tf == 'PM' and a[0] != 12: a[0]+=12 elif tf == 'AM' and a[0]==12: a[0]-=12 >>
- Return
Step 4️⃣: Convert our manipulated int array back to a string array.
- Here, we'll convert our int array (after applying our time conversion logic) back to a string array (temporarily store its items in a variable
b, while also maintaining leading zeroes using python's .zfill() function - Then, store our new strings in a new string array called
a_new.b = list(map(str, a)) for num in b: a_new.append(num.zfill(2)) >> ['00', '01', '00']
Step 5️⃣: Return our string array in the desired string format. 🥅
- Let's convert our string array back to the desired
HH:SS:MMformat using python's join() function - Finally, we return the string in a variable,
ouput.output = ':'.join(a_new) return output >> 00:01:00
Full code:
def timeConversion(s):
# Write your code here
a = s[:8].split(":")
a = list(map(int, a))
tf = s[-2:]
if tf == 'PM' and a[0] != 12: a[0]+=12
elif tf == 'AM' and a[0]==12: a[0]-=12
b = list(map(str, a))
a_new = []
for num in b:
a_new.append(num.zfill(2))
output = ':'.join(a_new)
return output
timeConversion('12:01:00AM')
Hope this helps with understanding this challenge. Happy Coding!👨🏽💻
About the author
Writing about things I learn helps me understand better, and I hope reading them will help you too! 🙂
Comments
Join the discussionNo comments yet. Be the first to comment.
Algos in Plain English
In this series, I'll code solutions to the HackerRank challenges with succinct explanation. Hope this helps anyone looking to improve their skills on data structures and algorithms!





