I am trying to solve a problem titled Time Zones on Kattis in Python 3. The link to the problem is here
My current solution:
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)
My code always fails the second test case (1/2 test cases solved). When I first submitted it, I failed the second test case. That was when I realised I should have included decimal support, since there were timezones with half-hour differences. I incorporated decimal support (shown by the decimal handling comment), submitted it again, and it still failed the second one.
My code seems perfectly fine to me and I tried numerous of my own test cases, all of which were successful.
I tried decimal test cases, negative test cases, negative decimal test cases, and more.
None of them are incorrect.
My current strategy is to intentionally try to get an incorrect test case, and debug it to pinpoint the issue.
I expect that my code would be perfectly fine and would successfully solve the problem, however, I am still stuck with the last hidden test case.
I have tried using ChatGPT. I have tried identifying corner cases. Nothing is working. I am still stuck with the last hidden test case.
I looked at the Kattis link and its list of time zones. I see that your list mimics their sample list. In scanning their sample, there appears to be a lot of missing time zone codes (e.g. for Russian time zones). Perhaps, their list was meant to be a starter, but they actually want all of the codes included. You might check out the link to the time and date website “timeanddate.com/time/zones”.