Answer to Question #174122 in Python for CHANDRASENA REDDY CHADA

Question #174122

New Year Countdown

Given date-time D, write a program to print the time left for the next New Year.Input


The input will be a single line containing the date-time in the string format similar to "Dec 30 2020 02:43 PM".Output


The output should be a single line containing hours and minutes left for the next new year in the format similar to "33 hours 17 minutes".Explanation


For example, if the given date-time is "Dec 30 2020 02:43 PM", the difference between "Dec 30 2020 02:43 PM" and "Jan 1 2021 00:00 AM" is 1 day 9 hours 17 minutes. The difference in hours and minutes is 1 * 24 + 9 hours and 17 minutes. So the output should be "33 hours 17 minutes".


Sample Input 1

Dec 30 2020 02:43 PM


Sample Output 1

33 hours 17 minutes


Sample Input 2

Jan 31 2020 04:43 AM


Sample Output 2

8059 hours 17 minutes




1
Expert's answer
2021-03-24T16:48:46-0400
month_day = [['Jan',31], ['Feb',28], ['Mar',31], ['Apr',30], ['May',31],
['Jun',30],['Jul',31], ['Aug',31],['Sep',30], ['Oct',31],['Nov',30],['Dec',31]]
# enter string of user
print ('Enter the date and time in the format')
print ('Example Dec 30 2020 02:43 PM')
mon, day, year, time, am_pm = input('>>> ').split()
# leap year check
y = int(year)
if y % 4 != 0 or (y % 100 == 0 and y % 400 != 0):
    month_day[1][1] =28
else:
    month_day[1][1] =29
# get full day 
day, after = int(day), 0
for item in month_day:
    day += item[1] * after
    if item[0] == mon:
        day = item[1]- day
        after = 1
# detect hours and minutes 
hour, min = time.split(':')
if int(min) != 0:
    min = 60 - int(min)
else:
    min = 0
if am_pm == 'PM':
    hour = 12 -int(hour)
if am_pm == 'AM':
    hour = 24 -int(hour)
if min !=0:
    hour -= 1
# output result
print(f'{24*day + hour} hours {min} minutes')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS