我已经设计了一个程序来在不使用循环的情况下找到第 n 个工作日。
请提出您对此的建议-
操作工作日的算法 -
问题:从任何特定日期找出第 n 个工作日的日期。
解决方案:
标准化到最近的星期一 -
If today(or the initial day) happens to be something other than monday, bring the day to the closest monday by simple addition or subtraction.
Run Code Online (Sandbox Code Playgroud)
例如:初始日 - 10 月 17 日。这恰好是星期三。因此,通过减少 2 个日期来使这个没有星期一正常化。现在命名这两个日期,即初始标准化因子。
将这些周内的工作日数 + 周结束数相加。
例如:要增加 10 个工作日,我们需要增加 12 天。由于 10 天有 1 个星期,其中仅包括 1 个星期六和 1 个星期日。这是因为,我们正在标准化到最近的星期一。
摊销回来 -
现在从结束日期开始添加初始归一化因子(对于负初始归一化)和另一个常数因子(例如,k)。或者,如果初始归一化是从星期五获得的,则加 1,恰好是 +3。如果开始日期在周六和周日,则视为周一。所以这一步不需要摊销。
例如:假设初始归一化来自周三,则初始归一化因子为 -2。因此将 2 添加到结束日期和常数 k。
The constant k is either 2 or 0.
Run Code Online (Sandbox Code Playgroud)常量定义 -
If initial normalization factor is -3, then add 2 to the resulting date if the day before amortization is (wed,thu,fri)
If initial normalization factor is -2, then add 2 to the resulting date if the day before amortization is (thu,fri)
If initial normalization factor is -1, then add 2 to the resulting date if the day before amortization is (fri)
Run Code Online (Sandbox Code Playgroud)
例子 -
Find the 15th working day from Oct,17 (wednesday).
Run Code Online (Sandbox Code Playgroud)
第1步 -
初始标准化 = -2 现在开始日期是 10 月 15 日(星期一)。
第2步 -
add 15 working days -
15 days => 2 weeks
weekends = 2 (2 sat, 2 sun)
so add 15 + 4 = 19 days to Oct, 15 monday.
end_date = 2, nov, Friday
Run Code Online (Sandbox Code Playgroud)
步骤 3a -
end_date = end_date + initial normalization = 4, nov sunday
Run Code Online (Sandbox Code Playgroud)
步骤 3b -
end_date = end_date + constant_factor = 4, nov, sunday + 2 = 6, nov (Tuesday)
Run Code Online (Sandbox Code Playgroud)
交叉验证 -
Add 15th working day to Oct, 17 wednesday
Oct,17 + 3 (Oct 17,18,19) + 5 (Oct 22-26) + 5 (Oct 29 - Nov 2) + 2 (Nov 5, Nov 6)
Now the answer is 6, Nov, Tuesday.
Run Code Online (Sandbox Code Playgroud)
我已经通过几个案例进行了验证。请分享您的建议。
拉森。
小智 5
首先,它是一个不错的算法,但我对边界条件有疑问:例如,如果我需要从今天的日期开始查找第 0 个工作日怎么办:
第1步 -
initial normalization = -2 now start date is Oct,15 (monday).
Run Code Online (Sandbox Code Playgroud)
第2步 -
add 0 working days -
0 days => 0 weeks
weekends = 0
so add 0 + 0 = 0 days to Oct, 15 monday.
end_date = 15, oct, monday
Run Code Online (Sandbox Code Playgroud)
步骤 3a -
end_date = end_date + initial normalization = 17, oct wednesday
Run Code Online (Sandbox Code Playgroud)
步骤 3b -
end_date = end_date + constant_factor = 17, Oct wednesday or 19,oct friday based on whether constant factor is 0 or 2 as it be only one of these values.
Run Code Online (Sandbox Code Playgroud)
现在让我们重复从今天开始寻找第一个工作日的步骤:
第1步 -
initial normalization = -2 now start date is Oct,15 (monday).
Run Code Online (Sandbox Code Playgroud)
第2步 -
add 1 working days -
1 days => 0 weeks
weekends = 0
so add 1 + 0 = 1 days to Oct, 15 monday.
end_date = 15, oct, monday
Run Code Online (Sandbox Code Playgroud)
步骤 3a -
end_date = end_date + initial normalization = 17, oct wednesday
Run Code Online (Sandbox Code Playgroud)
步骤 3b -
end_date = end_date + constant_factor = 17, Oct wednesday or 19,oct friday based on whether constant factor is 0 or 2 as it be only one of these values.
Run Code Online (Sandbox Code Playgroud)
您是否注意到,算法为 0 和 1 给出了相同的最终结果。如果 t 事先定义了 0 个工作日和 1 个工作日被视为相同的场景,那么这可能不是问题,但理想情况下它们应该给出不同的结果。
我还建议您考虑负面测试案例,例如如果我需要从今天开始找到第 -6 个工作日怎么办,您的算法会正确地给我一个过去的日期吗?
| 归档时间: |
|
| 查看次数: |
1186 次 |
| 最近记录: |