time - Hackerrank 中的时间增量问题没有得到很好的答案/Python 3
问题描述
hackerrank 挑战位于以下网址:https ://www.hackerrank.com/challenges/python-time-delta/problem
我得到了测试用例 0 正确,但是网站说我对测试用例 1 和 2 的答案有误,但是在我的 pycharm 中,我复制了网站的预期输出并与我的输出进行比较,它们完全相同。
请看一下我的代码。
#!/bin/pyth
# Complete the time_delta function below.
from datetime import datetime
def time_delta(tmp1, tmp2):
dicto = {'Jan':1, 'Feb':2, 'Mar':3,
'Apr':4, 'May':5, 'Jun':6,
'Jul':7, 'Aug':8, 'Sep':9,
'Oct':10, 'Nov':11, 'Dec':12}
# extracting t1 from first timestamp without -xxxx
t1 = datetime(int(tmp1[2]), dicto[tmp1[1]], int(tmp1[0]), int(tmp1[3][:2]),int(tmp1[3][3:5]), int(tmp1[3][6:]))
# extracting t1 from second timestamp without -xxxx
t2 = datetime(int(tmp2[2]), dicto[tmp2[1]], int(tmp2[0]), int(tmp2[3][:2]), int(tmp2[3][3:5]), int(tmp2[3][6:]))
# converting -xxxx of timestamp 1
t1_utc = int(tmp1[4][:3])*3600 + int(tmp1[4][3:])*60
# converting -xxxx of timestamp 2
t2_utc = int(tmp2[4][:3])*3600 + int(tmp2[4][3:])*60
# absolute difference
return abs(int((t1-t2).total_seconds()-(t1_utc-t2_utc)))
if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input())
for t_itr in range(t):
tmp1 = list(input().split(' '))[1:]
tmp2 = list(input().split(' '))[1:]
delta = time_delta(tmp1, tmp2)
print(delta)
解决方案
t1_utc = int(tmp1[4][:3])*3600 + int(tmp1[4][3:])*60
对于像这样的时区+0715
,您正确添加了“7 小时秒”和“15 分钟秒”</p>
对于像这样的时区-0715
,您添加“-7 小时秒”和“+15 分钟秒”,结果是 -6h45m,而不是 -7h15m。
您需要对两个部分使用相同的“标志”,或者在之后应用该标志。
推荐阅读
- node.js - 在 Node/Express 中,如何获取当前路由处理程序?
- java - 我一运行项目,Java .class 文件就会消失
- c# - 通过 dapper 动态参数将 int 列表发送到 sql sp
- django - /admin/accounts/user/1/change/ 'User' 对象的 AttributeError 没有属性 'profile'
- jquery - Ideal Forms 3 自定义正则表达式验证
- docker - 为什么 docker 容器的行为不同?
- excel - 带有匹配和不匹配的 Vlookup 返回 VBA 中的特定值。请找到以下代码。我需要知道如何获得匹配值
- python - 使用主脚本中的变量调用基本脚本的主 python 脚本
- azure - 无法使用 ARM 在应用服务计划上创建基于 Windows (OS) 的函数应用
- node.js - AWB Elastic Beanstalk 一些请求没有返回响应