日付はアルファベット順ですか?


8

YYYY-MM-DD入力として日付を(形式の文字列として)受け入れ、その日付が「アルファベット順」の場合は真の値を返し、そうでない場合は偽の値を返す関数またはプログラムを記述します。

アルファベット順の日付とは、文字列として表現した場合(特にM-D-Y順で検討した場合)、月、日、年がアルファベット順の日付です。たとえば、2018年9月26日はアルファベット順の日付です。

September 26th 2018 -> September twenty-sixth two thousand eighteen

September
Twenty-sixth
Two thousand eighteen

この課題を考える別の方法:「特定の日付の要素は字句的にソートされていますか?」

ノート:

  • 2018は「2千18」として表され、「二十八」や「二0八」ではありません。別の例として、1456年は「1万56」ではなく、「1 456」と表されます。
  • 26日は「twenty-six」ではなく「twenty-sixth」として表されます。
  • 日付の各要素は、全体として考慮されます。これが、18の「e」が2の「t」の前に来ても、2018が自動的に失敗しない理由です。

次の日付はアルファベット順ではありません。

  • 2018年9月2日(「2番目」は「9月」より前にソートする必要があります)
  • 4000年4月30日(「4000」は「30番目」より先にソートする必要があります)

追加ルール:

  • のような形式の文字列として日付を受け取りYYYY-MM-DDます。年は常に4桁で、月と日は常にそれぞれ2桁です。ゼロパディングは文字列変換では表されません(たとえば、 '2000-01-01'は予想どおり1月の最初の2000です)。
  • 日付は常に有効(2月30日なし、Smarch 1日なし)であり、年の値は正(BC日付なし)であると想定できますが、日付は遠い(2年目では9千...")。
  • 真偽値または偽値を返す必要があります。必ずしもブール値Trueまたはを返す必要はありませんFalse。あなたはJavaScriptでこれを実行して返すようにしたい場合'0'と0、それは大丈夫です。もちろん、ブール値を返したい場合は自由にしてください。
  • 標準の抜け穴は禁止されています。
  • これは code-golf

アルファベット順の日付のその他の例

  • 2066-01-02(1月第2、2万66)
  • 1000-04-08(4月8日、1000)
  • 6000-08-01(8月最初、6000)

アルファベット以外の日付のその他の例

  • 1066-01-02(1月2日、1万66)
  • 1000-04-07(4月7日、1000)
  • 8000-08-01(8月最初、8000)


@アーナウルドええと、違う?2018-09-02は誤りであるように見えますが、2018-09-26は真実です(質問のように与えられた場合)。
— Erik the Outgolfer 2018

@EriktheOutgolferそうです。私はその挑戦を完全に誤解しました。(たぶんそれは、日付が語彙的にソートされているか、またはそのようなものと言い換えるべきです)
— Arnauld

@Arnauld私は最初の段落を微調整し、あなたの提案された言い回しを含む行を含めました。うまくいけば、将来の読者の喜びのためです。ありがとうございました!
— souldeux

1
年は常につづりの数字です。1444は、144です。1991年は199です。
— souldeux 2018

回答:


1

05AB1E、66 62 58 バイト

0ì'-¡ÀDΣ2£<•88ΛηΣλšëÇñ0é≠>sîä&ζp°Lć®-αÅ®`•21вŽqT32в£Nèsè}Q

オンラインで試すか、チャレンジに記載されているすべての例を確認してください。

説明:

0ì         # Prepend a "0" before the (implicit) input
           #  i.e. "2018-09-26" → "02018-09-26"
  '-¡     '# Then split on "-"
           #  i.e. "02018-09-26" → ["02018","09","26"]
     À     # Rotate it once to the left (so [0yyyy,MM,dd] becomes [MM,dd,0yyyy])
           #  i.e. ["02018","09","26"] → ["09","26","02018"]
D          # Duplicate this list
 Σ         # Sort this list by:
  2£       #  Leave only the first 2 digits of the current item
           #  (which is why the prepended 0 was there at the start for the year)
           #   i.e. "09" → "09"
           #   i.e. "02018" → "02"
    <      #  Decrease it by 1 to make it 0-indexed
           #   i.e. "09" → 8
  •88ΛηΣλšëÇñ0é≠>sîä&ζp°Lć®-αÅ®`•21в
           #  Push the list [7,3,7,0,7,7,7,0,13,10,10,0,4,12,17,6,4,17,15,2,9,17,2,19,17,6,4,17,15,2,9,19,19,19,19,19,19,19,19,19,19,17,17,11,20,18,5,5,16,14,1,8]
   ŽqT32в  #  Push the list [12,31,9]
         £ #  Split the first list into parts of that size: [[7,3,7,0,7,7,7,0,13,10,10,0],
           #    [4,12,17,6,4,17,15,2,9,17,2,19,17,6,4,17,15,2,9,19,19,19,19,19,19,19,19,19,19,17,17],
           #    [11,20,18,5,5,16,14,1,8]]
   Nè      #  Then only leave the relevant list of the three based on the sort-index
     s     #  Swap to get the earlier integer at the top of the stack again
      è    #  And use it to index into the list
           #   i.e. [7,3,7,0,7,7,7,0,13,10,10,0] and 8 → 13
 }         # Close the sort
  Q        # And check if the sorted list is equal to the duplicated list,
           # so whether the order is still [MM,dd,0yyyy] after sorting
           # (and output the result implicitly)               

この05AB1Eのヒント(「整数リストを圧縮する方法」のセクション)を参照して、•88ΛηΣλšëÇñ0é≠>sîä&ζp°Lć®-αÅ®`•21вと[7,3,7,0,7,7,7,0,13,10,10,0,4,12,17,6,4,17,15,2,9,17,2,19,17,6,4,17,15,2,9,19,19,19,19,19,19,19,19,19,19,17,17,11,20,18,5,5,16,14,1,8]でŽqT32вある理由を理解してください[12,31,9]。

追加の一般的な説明:

ソート機能は、我々だけであればtruthy値が返されます場合は、同時にすべての3つのタイプ(日、月、年)をソートしますM < D < Y(ここで、これらのD、MとY私たちは圧縮されたリストから検索してきた値です)。

上記の3つのリストが選ばれる理由 すべての単語をタイプ別に分類すると、次の順序になります。

Sorting nr      Type        Which?

0               Month       April, August, December
1               Year        eight thousand
2               Day         eighteenth, eighth, eleventh
3               Month       February
4               Day         fifteenth, fifth, first
5               Year        five thousand, four thousand
6               Day         fourteenth, fourth
7               Month       January, July, June, March, May
8               Year        nine thousand
9               Day         ninteenth, ninth
10              Month       November, October
11              Year        one thousand
12              Day         second
13              Month       September
14              Year        seven thousand
15              Day         seventeenth, seventh
16              Year        six thousand
17              Day         sixteenth, sixth, tenth, third, thirteenth, thirtieth, thirty-first
18              Year        three thousand
19              Day         twelfth, twentieth, twenty-first through twenty-ninth
20              Year        two thousand

次に、各タイプとその元の順序(年は1000、2000など、月は1月、2月など、日は1番目、2番目など)を見ると、上記のソート番号はこれらの順序で:

Years:  [11,20,18,5,5,16,14,1,8]
Months: [7,3,7,0,7,7,7,0,13,10,10,0]
Days:   [4,12,17,6,4,17,15,2,9,17,2,19,17,6,4,17,15,2,9,19,19,19,19,19,19,19,19,19,19,17,17]

9

JavaScript(ES6)、101バイト

@Shaggyのおかげで4バイトを節約

01

s=>'_414044406550'[[,m,d]=s.split`-`,+m]<(d=`_268328715819832871${6e12-12}`[+d])&d<'_6A9338704'[s[0]]

オンラインでお試しください! またはすべての日付をテストしてください! (いくつかの無効な入力を含みます)

どうやって?

[0..10]MDYM<DD<Y

日IDの検索文字列の最後の部分は、わずかに使用して圧縮されている${6e12-12}に展開され、5999999999988(19 番〜31 番)。

  M | Month                D | Day                  Y | Year (millennium)
----+----------------    ----+----------------    ----+-------------------
  0 | April                1 | eighth               0 | eight thousand
  0 | August               1 | eleventh           ----+-------------------
  0 | December             1 | eighteenth           3 | four thousand
----+----------------    ----+----------------      3 | five thousand
  1 | February             2 | first              ----+-------------------
----+----------------      2 | fifth                4 | nine thousand
  4 | January              2 | fifteenth          ----+-------------------
  4 | March              ----+----------------      6 | one thousand
  4 | May                  3 | fourth             ----+-------------------
  4 | June                 3 | fourteenth           7 | seven thousand
  4 | July               ----+----------------    ----+-------------------
----+----------------      5 | ninth                8 | six thousand
  5 | October              5 | nineteenth         ----+-------------------
  5 | November           ----+----------------      9 | three thousand
----+----------------      6 | second             ----+-------------------
  6 | September          ----+----------------     10 | two thousand
                           7 | seventh
                           7 | seventeenth
                         ----+----------------
                           8 | third
                           8 | sixth
                           8 | tenth
                           8 | thirteenth
                           8 | sixteenth
                           8 | thirtieth
                           8 | thirty-first
                         ----+----------------
                           9 | twelfth
                           9 | twentieth
                           9 | twenty-*


@Shaggyおっと...これm=は、もちろん、完全に役に立たなかった。ありがとう。:)
— Arnauld

1
早かった!よくできました。「すべての日付をテストする」と例示の表は特に高く評価されています。
— souldeux 2018
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.