Advanced · Techniques · 210 of 264
Days between dates
How many days are there from the first date to the second? Use the datetime module.
Input: two dates YYYY-MM-DD, one per line (the second is not earlier). Output: the number of days.
Examples
Input
2024-01-01 2024-03-01
Output
60
Input
2023-05-05 2023-05-05
Output
0
Input
1999-12-31 2000-01-01
Output
1
One more test checks other inputs when you press Check.
Stuck? The idea behind techniques
Some problems have a known shape. Two pointers move from both ends of a sorted list. A sliding window keeps a running total as it moves, adding one item and taking one away.
A dictionary of what has been seen turns a search inside a loop into a single lookup.
nums = [2, 7, 11, 15]
target = 9
seen = {}
for i, x in enumerate(nums):
if target - x in seen:
print(seen[target - x], i)
seen[x] = iWatch for: A loop inside a loop over the same long list is slow: the hidden tests may use big inputs.