Neural Mastery
← Practice
The Code Is Right, the Rule Is Wrongeasy

The Code Is Right, the Rule Is Wrong

easy Specify, Verify, Debug 15–20 min

Standard library only · runs in your browser

Your stage 1 suite is green and the code ships. Two weeks later, three support tickets say the bot refused refunds on day 30. The code does exactly what everyone agreed. The agreement was missing a word.

Task

The tickets:

  • Kai, Honolulu (UTC−10). The parcel arrived March 1 at 10 am. On March 31 at 7 pm Kai asked for a refund, which is day 30, and was refused.
  • Yui, Tokyo (UTC+9). Delivered March 2 at 1 am, asked April 1 at 11 pm. Day 30, refused.
  • Arjun, Pune (UTC+5:30). Delivered July 1 at 12:30 am, asked July 31 at 11:30 pm. Day 30, refused.

The shop stores every timestamp in UTC, and the agent counted days between the UTC dates. The owner confirms what "30 days" meant all along: calendar days on the customer's own calendar.

Fix local_days_between(delivered_at, requested_at, tz_offset_hours). It returns the number of calendar days between the two moments, counted on the customer's local calendar. Timestamps are ISO strings ending in Z. The offset is in hours and can be fractional (5.5 for India).

Function Signature

local_days_between(delivered_at: str, requested_at: str, tz_offset_hours: float) -> int

Examples

Example 1: Kai, Honolulu: day 30 on the local calendar
Input: delivered_at = '2026-03-01T20:00:00Z' requested_at = '2026-04-01T05:00:00Z' tz_offset_hours = -10
Output: 30
Explanation: UTC dates say 31 days. Kai's calendar says 30.
Example 2: Yui, Tokyo: day 30 on the local calendar
Input: delivered_at = '2026-03-01T16:00:00Z' requested_at = '2026-04-01T14:00:00Z' tz_offset_hours = 9
Output: 30
Example 3: A customer in London (UTC+0)
Input: delivered_at = '2026-05-10T23:30:00Z' requested_at = '2026-05-11T00:10:00Z' tz_offset_hours = 0
Output: 1
Explanation: Forty minutes apart, but on two different days.

Constraints

  • Fixed offsets only. No daylight saving in this stage.
  • Standard library only: `datetime` and `timedelta` are imported.
  • The starter is the agent's version. It passes some tests already, which is exactly how it shipped.
Python3Saved ✓
import json
import math
import re
from datetime import date, datetime, timedelta


def local_days_between(delivered_at, requested_at, tz_offset_hours):
"""Calendar days between delivery and the request, on the customer's calendar."""
# The agent's version: counts days between the UTC dates.
start = datetime.fromisoformat(delivered_at.replace("Z", "+00:00"))
end = datetime.fromisoformat(requested_at.replace("Z", "+00:00"))
return (end.date() - start.date()).days

Case 1: Kai, Honolulu: day 30 on the local calendar
Input: delivered_at = '2026-03-01T20:00:00Z' requested_at = '2026-04-01T05:00:00Z' tz_offset_hours = -10
Expected: 30
Case 2: Yui, Tokyo: day 30 on the local calendar
Input: delivered_at = '2026-03-01T16:00:00Z' requested_at = '2026-04-01T14:00:00Z' tz_offset_hours = 9
Expected: 30
Case 3: A customer in London (UTC+0)
Input: delivered_at = '2026-05-10T23:30:00Z' requested_at = '2026-05-11T00:10:00Z' tz_offset_hours = 0
Expected: 1