提问人:Ow Ji 提问时间:11/11/2023 更新时间:11/17/2023 访问量:37
Kattis 时区测试用例失败
Failing test case on timezones on Kattis
问:
我正在尝试解决 Python 3 中标题为 Kattis 时区的问题。问题的链接在这里 我目前的解决方案:
zones = {
"UTC": 0,
"GMT": 0,
"BST": 1,
"IST": 1,
"WET": 0,
"WEST": 1,
"CET": 1,
"CEST": 2,
"EET": 2,
"EEST": 3,
"MSK": 3,
"MSD": 4,
"AST": -4,
"ADT": -3,
"NST": -3.5,
"NDT": -2.5,
"EST": -5,
"EDT": -4,
"CST": -6,
"CDT": -5,
"MST": -7,
"MDT": -6,
"PST": -8,
"PDT": -7,
"HST": -10,
"AKST": -9,
"AKDT": -8,
"AEST": 10,
"AEDT": 11,
"ACST": 9.5,
"ACDT": 10.5,
"AWST": 8,
}
def add_hours_to_time(input_time, hours_to_add):
# Split the time string into hours, minutes, and am/pm/noon/midnight parts
time_parts = input_time.split()
# Handle "noon" and "midnight" cases
if len(time_parts) == 1:
if time_parts[0] == "noon":
hours = 12
minutes = 0
elif time_parts[0] == "midnight":
hours = 0
minutes = 0
else:
time = time_parts[0]
period = time_parts[1]
hours, minutes = tuple(map(int, time.split(':')))
# Convert to 24-hour format
if period == 'p.m.':
if hours != 12:
hours += 12
elif period == 'a.m.':
if hours == 12:
hours = 0
# Calculate new hours and minutes
new_hours = hours + hours_to_add
new_hours = new_hours % 24
new_minutes = minutes
# Decimal handling
if str(new_hours).endswith(".5"):
new_minutes += 30
if new_minutes >= 60:
new_hours += 1
over_minutes = new_minutes % 60
new_minutes = over_minutes
new_hours = int(new_hours)
# Final output
if new_hours == 12:
if new_minutes == 0:
time_str = "noon"
else:
time_str = f"{new_hours}:{new_minutes:02d} p.m."
elif new_hours == 0:
if new_minutes == 0:
time_str = "midnight"
else:
time_str = f"{new_hours + 12}:{new_minutes:02d} a.m."
else:
if new_hours < 12:
time_str = f"{new_hours}:{new_minutes:02d} a.m."
else:
new_hours -= 12
if new_hours == 0:
time_str = f"{new_hours + 12}:{new_minutes:02d} p.m."
else:
time_str = f"{new_hours}:{new_minutes:02d} p.m."
return time_str
n = int(input())
results = []
for i in range(n):
times = input()
times = times.split()
if len(times) == 4:
area1 = times[2]
area2 = times[3]
diff = zones[area2] - zones[area1]
result = add_hours_to_time(f"{times[0]} {times[1]}", diff)
results.append(result)
elif len(times) == 3:
area1 = times[1]
area2 = times[2]
diff = zones[area2] - zones[area1]
result = add_hours_to_time(times[0], diff)
results.append(result)
for something in results:
print(something)
我的代码总是在第二个测试用例中失败(解决了 1/2 个测试用例)。当我第一次提交它时,我没有通过第二个测试用例。就在那时,我意识到我应该包括十进制支持,因为有些时区有半小时的差异。我合并了十进制支持(由十进制处理注释显示),再次提交,但它仍然未能通过第二个。 我的代码对我来说似乎非常好,我尝试了许多自己的测试用例,所有这些都成功了。 我尝试了十进制测试用例、阴性测试用例、负十进制测试用例等。 它们都不是不正确的。 我目前的策略是有意识地尝试获取不正确的测试用例,并对其进行调试以查明问题。 我希望我的代码会很好,并且会成功解决问题,但是,我仍然停留在最后一个隐藏的测试用例上。 我试过使用 ChatGPT。我试过识别极端情况。没有任何效果。我仍然停留在最后一个隐藏的测试用例上。
答:
0赞
steviestickman
11/17/2023
#1
在十进制时区处理之后,小时数可能比 23 小时大,但在这种情况下,您的代码不会换行。例如,在转换 .结果是午夜过后 20 分钟,但您的代码将其计为 24 点后的 20 分钟。在这种情况下,您的代码将打印出来,而不是正确的8:50 a.m. MDT ACST
12:20 p.m.
12:20 a.m.
评论