Date Range Generator - Problem
Create a Date Range Generator
You're tasked with building a generator function that produces a sequence of dates within a specified range. Given a
Key Requirements:
This problem tests your understanding of JavaScript generators, date manipulation, and iterator patterns.
You're tasked with building a generator function that produces a sequence of dates within a specified range. Given a
start date, an end date (inclusive), and a step value representing days, your generator should yield dates in chronological order.Key Requirements:
- Return a generator object (not an array)
- Include both start and end dates if they align with the step pattern
- All dates must be in
YYYY-MM-DDformat - Handle month/year transitions correctly
This problem tests your understanding of JavaScript generators, date manipulation, and iterator patterns.
Input & Output
example_1.py โ Basic Date Range
$
Input:
start = "2024-01-01", end = "2024-01-10", step = 3
โบ
Output:
["2024-01-01", "2024-01-04", "2024-01-07", "2024-01-10"]
๐ก Note:
Starting from 2024-01-01, we add 3 days each time: Jan 1 โ Jan 4 โ Jan 7 โ Jan 10. Since Jan 10 equals our end date, it's included.
example_2.py โ Cross-Month Range
$
Input:
start = "2024-01-29", end = "2024-02-05", step = 2
โบ
Output:
["2024-01-29", "2024-01-31", "2024-02-02", "2024-02-04"]
๐ก Note:
The generator correctly handles month transitions: Jan 29 โ Jan 31 โ Feb 2 โ Feb 4. Feb 6 would exceed the end date, so it's excluded.
example_3.py โ Single Day Range
$
Input:
start = "2024-12-25", end = "2024-12-25", step = 1
โบ
Output:
["2024-12-25"]
๐ก Note:
When start equals end, the generator yields exactly one date (the start/end date) regardless of step size.
Constraints
- Start date will always be โค end date
- Dates are in valid YYYY-MM-DD format
- step is a positive integer (1 โค step โค 103)
- Date range will not exceed 104 days
- Years will be between 1900 and 2100
Visualization
Tap to expand
Understanding the Visualization
1
Set Starting Position
Place marker on start date
2
Check Boundary
Verify current date hasn't exceeded end date
3
Yield Current Date
Return current date in required format
4
Jump Forward
Move marker forward by step days
5
Repeat
Continue until past end date
Key Takeaway
๐ฏ Key Insight: Generators provide the perfect balance of memory efficiency and ease of use for sequence generation problems.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code