Time Conversion:
Given
a time in the 12-hour format with the suffix , either AM/PM, convert that
into a 24-hour format. 12-hour format is hours:minutes:seconds followed by AM
or PM, where the hours range from 0 to 12, minutes range from 0 to 59, seconds
range from 0 to 59. 24-hours format is hours:minutes and seconds , where
hours range from 0 to 23, minutes range from 0 to 59, seconds range from 0 to
59. All the three components: hours, minutes, seconds are represented in the two
digit format.
Note Midnight
12 o’clock is 12:00:00AM in the 12-hour format and it is 00:00:00 in 24- hour
format. 12 Noon is 12:00:00PM in the 12-hour format and it is 12:00:00 in the
24- hour format.
For
example, if input is 07:05:45PM then the output is 19:05:45 and if
the input is 07:05:45AM then the output is 07:05:45.
Input format:
Time
in 12-hour format with suffix, either AM or PM
Output format:
Print
time in 24-hour format
Boundary Conditions:
0
< hour, minute and seconds < 60
Meridium
should be either “AM” or “PM”
Input:
Time
in correct format from the user
Processing:
hrs = time[0:2]minutes = time[3:5]
seconds = time[6:8]
AM_PM = time[8:10]
flag = True
if int(hrs) > 12:
flag == False
if AM_PM == 'AM' and flag == True:
if int(hrs) == 12:
hrs = '0'
elif AM_PM == 'PM' and flag == True:
if int(hrs) != 12:
hrs = int(hrs) + 12
if int(minutes) > 60 or int(seconds) > 60:
flag = False
if len(str(hrs)) != 2:
hrs = '0' + str(hrs)
if len(str(minutes)) != 2:
minutes = '0' + minutes
if len(str(seconds)) != 2:
seconds = '0' + seconds
Output:
Display
time in the 24 hour format
Algorithm:
Step1.
Get the time in correct format from the user(time).
Step2.
Split the time with respect to : and store each portion in hrs, minutes, seconds
and AM_PM.
Step3.
Check if hrs is greater than 12 if true then change flag to False else proceed
to step4
Step4.
Check if AM_PM is ‘AM’ and also check if flag is equal to True if both these
are true then proceed to Step4.1 else proceed to step5
Step4.1
check if hrs is 12 if true then assign hrs as 0
Step5.
Check if the element present in the third index of array is ‘PM’ and also check
if flag is equal to True if both these are true then proceed to Step5.1 else
proceed to step6
Step5.1
check if zeroth element of array is not equal to 12 if true then assign hrs as
the sum of the integer conversion of hrs and 12
Step6.
Check if the minutes and seconds are greater than 60 all the conditions are
satisfied assign flag as false
Step7.
If length of minutes string is less than two then concatenate it with a 0 at
the front and do the same for seconds if length of the seconds string is less
than 2
Step8.
If flag is true then display the time 24 hr format else display invalid.
Step9.
End
Program:
time = input()hrs = time[0:2]
minutes = time[3:5]
seconds = time[6:8]
AM_PM = time[8:10]
flag = True
if int(hrs) > 12:
flag == False
if AM_PM == 'AM' and flag == True:
if int(hrs) == 12:
hrs = '0'
elif AM_PM == 'PM' and flag == True:
if int(hrs) != 12:
hrs = int(hrs) + 12
if int(minutes) > 60 or int(seconds) > 60:
flag = False
if len(str(hrs)) != 2:
hrs = '0' + str(hrs)
if len(str(minutes)) != 2:
minutes = '0' + minutes
if len(str(seconds)) != 2:
seconds = '0' + seconds
if flag==True:
print("%s:%s:%s"%(hrs,minutes,seconds))
else:
print('Invalid')
Flowchart:
To edit this flowchart click on this
No comments:
Post a Comment