2

ループを使用せずにn番目の営業日を見つける手順を考案しました。

これについてあなたの提案を持ってきてください -

営業日を操作するアルゴリズム -

問題: 特定の日から n 番目の営業日の日付を見つけます。

解決:

  1. 最も近い月曜日に正規化 -

    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.
    

    例: 初日 - 10 月 17 日。たまたま水曜日です。したがって、2 つの日付を下に移動して、この月曜日でないことを正規化します。ここで、この 2 つの日付に名前を付けます。初期正規化係数です。

  2. これらの週に該当する稼働日数 + 週末を追加します。

    例: 10 営業日を追加するには、12 日を追加する必要があります。10 日間は 1 週間に 1 土曜日と 1 日曜日しか含まれないためです。これは、最も近い月曜日に正規化しているためです。

  3. 償却 -

    ここで、終了日から、初期正規化係数 (負の初期正規化の場合) と別の定数係数 (k など) を追加します。または、最初の正規化が金曜日から取得された場合は 1 を追加します。これはたまたま +3 です。開始日が土曜日と日曜日の場合は、月曜日として扱います。したがって、この段階では償却は必要ありません。

    例: 最初の正規化が水曜日からのものである場合、最初の正規化係数は -2 です。したがって、終了日と定数 k に 2 を追加します。

    The constant k is either 2 or 0. 
    

定数定義 -

    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) 

例 -

   Find the 15th working day from Oct,17 (wednesday).

ステップ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

ステップ 3a -

end_date = end_date + initial normalization = 4, nov sunday

ステップ 3b -

end_date = end_date + constant_factor = 4, nov, sunday + 2 = 6, nov (Tuesday)

クロス検証 -

 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.

いくつかのケースで確認しました。あなたの提案を共有してください。

ラーセン。

4

2 に答える 2

5

まず、その素晴らしいアルゴリズムですが、境界条件については疑問があります。

ステップ1 -

initial normalization = -2 now start date is Oct,15 (monday).

ステップ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

ステップ 3a -

end_date = end_date + initial normalization = 17, oct wednesday

ステップ 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.

今日から最初の営業日を見つけるための手順を繰り返しましょう。

ステップ1 -

initial normalization = -2 now start date is Oct,15 (monday).

ステップ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

ステップ 3a -

end_date = end_date + initial normalization = 17, oct wednesday

ステップ 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.

お気づきでしたか、アルゴリズムは 0 と 1 に対して同じ最終結果を返します。0 営業日と 1 営業日が同じシナリオと見なされることを事前に定義していれば、問題ではないかもしれませんが、理想的には、異なる結果が得られるはずです。

また、否定的なテスト ケースを検討することをお勧めします。たとえば、今日から 6 営業日後に検索する必要がある場合、あなたのアルゴリズムは過去の日付を正当に与えてくれますか?

于 2012-10-17T05:25:34.760 に答える
2

今日(17/10、水)から 0 日目の営業日を考えてみましょう。

ステップ1 -

 start_date = 17/10 wed
 normalized date = 15/10 mon

ステップ2 -

end_date = 正規化された日付 + 営業日 = 15/10 月 + 0 = 15/10 月

ステップ 3 -

  amortized_back = end_date_before_amortization + normalization factor
                 = 15/10 + (+2) = 17/10 wed

  since the end_date_before_amortization falls on monday and initial normalization is 2, constant factor = 0.

  hence, end_date = 17/10 wed.

今ケース 2、今日から最初の営業日。

ステップ1 -

 start_date = 17/10 wed
 normalized date = 15/10 mon

ステップ2 -

end_date = 正規化された日付 + 営業日 = 15/10 月 + 1 = 16/10 火 ステップ 3 -

 amortized_back = end_date_before_amortization + normalization factor
                 = 16/10 + (+2) = 18/10 thu.

  since the end_date_before_amortization falls on tuesday and initial normalization is 2, constant factor = 0.

  hence, end_date = 18/10 thu.

0番目と1番目のWDで機能しているようです。

于 2012-10-17T16:41:48.473 に答える