We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I meet some difficulties to solve this problem because I first think that we have to used Calendar module.
But in fact, we have to use datetime module. From the documentation, we have a full table with all meaning of %a, %b etc...
Once we have this knowledge, we can just go step by step by solving each error one after one.
Here, I convert both datetime in delta_time thanks to the conversion value of the documentation.
Then, I do the difference.
Finally, because test case 1 and 2 were not solved cause of values like "-123456" instead of "123456", I just add a small if/else condition to remove this possible "-".
Here the code :
#!/bin/python3importmathimportosimportrandomimportreimportsysfromdatetimeimportdatetime# Complete the time_delta function below.deftime_delta(t1,t2):delta_time_t1=datetime.strptime(t1,'%a %d %b %Y %H:%M:%S %z')delta_time_t2=datetime.strptime(t2,'%a %d %b %Y %H:%M:%S %z')difference_delta_time=delta_time_t1-delta_time_t2#print(delta_time_t1)#print(delta_time_t2)#print(int(difference_delta_time.total_seconds()))output=str(int(difference_delta_time.total_seconds()))#print(output[0]) output=output[1:]ifoutput.startswith('-')elseoutputreturnoutputreturn500if__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')t=int(input())fort_itrinrange(t):t1=input()t2=input()delta=time_delta(t1,t2)fptr.write(delta+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Time Delta
You are viewing a single comment's thread. Return to all comments →
I meet some difficulties to solve this problem because I first think that we have to used Calendar module. But in fact, we have to use datetime module. From the documentation, we have a full table with all meaning of %a, %b etc...
Once we have this knowledge, we can just go step by step by solving each error one after one.
Here, I convert both datetime in delta_time thanks to the conversion value of the documentation.
Then, I do the difference.
Finally, because test case 1 and 2 were not solved cause of values like "-123456" instead of "123456", I just add a small if/else condition to remove this possible "-".
Here the code :