それは毎年の仕事です、申し分なく


22

1≤n≤365の数値を指定すると、年のn番目の日を「Day-numberth Month」形式で出力します。たとえば、1が与えられた場合、「of」なしで「1月1日」を出力する必要があります。

グレゴリオ暦が使用され、プログラムはうるう年を考慮すべきではないため、どのような状況でもプログラムが「2月29日」を出力することはありません。前述の「日番号月」形式に従う限り、任意の方法を使用できます。また、プログラムは序数を正しく出力する必要があります。つまり、1、2、または3を常に出力する必要があります。先行スペースまたは他のインデントが許可されます。

これはコードゴルフであるため、キャラクターによる最短の解決策が優先されます。

テストケース:

1 gives 1st January
2 gives 2nd January
3 gives 3rd January
365 gives 31st December
60 gives 1st March
11 gives 11th January

4
また、365を超える番号にエラーメッセージを強制する必要がありますか?プログラムは、それが無効な入力であり、それを処理する必要がないと想定できますか?
Rɪᴋᴇʀ

5
すべての人が英語を母国語とするわけではないので、11、12、13の数字に「th」、「1」で終わる数字に「st」、「2」に「nd」、「3」を追加します。 「rd」を取得し、他のすべては「th」を取得します。
アダム

9
おっと、すぐに答えを受け入れないでください。特に間違った答えはありませ
アダム

6
あなたは、少なくとも追加する必要があります11(11 番目の 1月)と21(21 回目のテストケースに1月)。
アーナルド

1
また、テストケースを編集しているときに、テストケースの形式を正確に指定することもできます。何人かの回答者は、それ123=が必要な出力の一部であると考えました。または単にのようなものを読むために、あなたのテストケースを編集します365与えます31st December
アダム

回答:


9

PHP38 40 30 28バイト

<?=date("jS F",86399*$argn);

オンラインでお試しください!

php -nF入力ありで実行はからSTDINです。例(上記の名前のスクリプトy.php):

$ echo 1|php -nF y.php
1st January
$ echo 2| php -nF y.php
2nd January
$ echo 3| php -nF y.php
3rd January
$ echo 11|php -nF y.php
11th January
$ echo 21|php -nF y.php
21st January
$ echo 60|php -nF y.php
1st March
$ echo 365|php -nF y.php
31st December

説明

day number * number of seconds per day(86400)を掛けることにより、1970年の希望する日(うるう年ではない)のエポックタイムスタンプを作成します。ただし、これは1日高くなるため、代わりにnumber of seconds in a day - 1(86399)を乗算します。入力数値の範囲(1≤n≤365)の場合、各正しい日の終わりのタイムスタンプが生成されます。次に、出力にPHPの組み込みの日付書式を使用します。


なぜ-n必要なのですか?
ベン

@Venすべての場合に当てはまるわけではありませんが、一貫性のない動作を引き起こす可能性のあるローカルphp.iniの設定を無効にします。
640KB

6

ゼリー 79 78  77 バイト

-1バグの修正:) (インデックスを見つけるために事前にトランスポーズするべきではなく、ポストリバースする必要がありますが、ヘッドではなくテールにすることができます)
-1リフレクションを使用する⁽©ṅB+30_2¦2-> ⁽0ṗb4+28m0

⁽0ṗb4+28m0SRṁRƲœiµṪȮ%30%20«4ị“nḄƲf⁷»s3¤Ṗ,ị“£ṢtẒ⁽ẹ½MḊxɲȧėAṅ ɓaṾ¥D¹ṀẏD8÷ṬØ»Ḳ¤$K

結果を出力する完全なプログラム

オンラインでお試しください!

どうやって?

これは後で更新します...

⁽©ṅB+30_2¦2SRṁRƲZœiµḢȮ%30%20«4ị“nḄƲf⁷»s3¤Ṗ,ị“...»Ḳ¤$K - Main Link: integer, n
⁽©ṅB+30_2¦2SRṁRƲZœi - f(n) to get list of integers, [day, month]
⁽©ṅ                 - compressed literal 2741
   B                - to a list of binary digits -> [ 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1]
    +30             - add thirty                    [31,30,31,30,31,30,31,31,30,31,30,31]
         ¦          - sparse application...
        2           - ...to indices: [2]
       _  2         - ...action: subtract two       [31,28,31,30,31,30,31,31,30,31,30,31]
               Ʋ    - last four links as a monad - i.e. f(x):
           S        -   sum x                       365
            R       -   range                       [1..365]
              R     -   range x (vectorises)        [[1..31],[1..28],...]
             ṁ      -   mould like                  [[1..31],[32..59],...]
                Z   - transpose                     [[1,32,...],[2,33,...],...]
                 œi - 1st multi-dimensional index of n  -> [day, month]

µḢȮ%30%20«4ị“nḄƲf⁷»s3¤Ṗ,ị“...»Ḳ¤$K - given [day, month] format and print
µ                                  - start a new monadic chain - i.e. f(x=[day, month])
 Ḣ                                 - head -- get the day leaving x as [month])
  Ȯ                                - print it (with no newline) and yield it
   %30                             - modulo by thirty
      %20                          - modulo by twenty
         «4                        - minimum of that and four
                     ¤             - nilad followed by link(s) as a nilad:
            “nḄƲf⁷»                -   dictionary words "standard"+" the" = "standard the"
                   s3              -   split into threes = ["sta","nda","rd ","the"]
           ị                       - index into
                      Ṗ            - remove rightmost character
                               ¤   - nilad followed by link(s) as a nilad:
                         “...»     -   dictionary words "January"+" February"+...
                              Ḳ    -   split at spaces = ["January","February",...]
                        ị          - index into (vectorises across [month])
                       ,           - pair                  e.g. ["th", ["February"]]
                                K  - join with spaces           ["th ", "February"]
                                   - print (implicitly smashes)   th February

4
「標準的な」トリックは驚くべきものです。
ヴェン

@Venに同意します、素晴らしいトリックです!また、サイズ2()の部分に分割された圧縮文字列と比較して、05AB1Eの回答に1バイトを保存しました。:)"thstndrd".•oθ2(w•2ô
ケビンクルーッセン

1
これは、私が今まで見た中で最も長いゼリープログラムの1つでなければなりません。
JAD

6

C#(Visual C#Interactive Compiler)115 113 109 98バイト

g=>$"{f=(g=p.AddDays(g-1)).Day}{"tsnr"[f=f%30%20<4?f%10:0]}{"htdd"[f]} {g:MMMM}";DateTime p;int f;

9バイトを節約してくれた@someoneに感謝

オンラインでお試しください!


1
@KevinCruijssenモジュロの順序が乱れたので、修正する必要があります。
の無知の

.code.tio(2,22): error CS0165: Use of unassigned local variable 'p'構造物は機能しないようです。
JAD

var g=new DateTime().AddDays(n-1)かかわらず動作します
JAD

私の@JADの間違い、修正
無知の


5

Python 3.8(プレリリース)、112バイト

lambda x:str(d:=(t:=gmtime(x*86399)).tm_mday)+'tsnrhtdd'[d%5*(d%30%20<4)::4]+strftime(' %B',t)
from time import*

オンラインでお試しください!

奇妙なことにd:=(t:=gmtime(~-x*86400)、おそらくインタープリター()は割り当て式の周りに文字があるかどうかをチェックするだけで、式自体が括弧で囲まれているのではなく、括弧で囲む必要はありません。

-2 gwaughに感謝します
-5 xnorに感謝します


5

Perl 6の166の 161バイト

{~(.day~(<th st nd rd>[.day%30%20]||'th'),<January February March April May June July August September October November December>[.month-1])}o*+Date.new(1,1,1)-1

オンラインでお試しください!

すべての月名をハードコーディングします。これにより、ほとんどのスペースが占有されます。男、Perl 6には適切な日付フォーマッターが本当に必要です。


4

ハック、115 59 39バイト

$x==>date("jS F",mktime(0,0,0,1,$x));

@gwaughはゴルフ中に私のものと同じ解決策を得たので、代わりにこれをHackに投稿しています:)。


うわー、偉大な心は同様に考える。:)あなたに+1!
640KB

@gwaughハハ、トップレベルのプログラムができるとは知らなかった。私も編集してトップレベルにし、より良いスコアを得る方法を見つけます;-)
Ven

1
@gwaughは代わりに私のハックを作りました。
ヴェン

1
おそらく、mktime()呼び出しにうるう年以外のパラメーターを指定する必要があります。そうでない場合、うるう年で実行すると、間違った出力が返されます。(私の答えにならなければなりませんでした)。
640KB

4

JavaScript(ES6)、 117  113バイト

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

d=>(n=(d=new Date(1,0,d)).getDate())+([,'st','nd','rd'][n%30%20]||'th')+' '+d.toLocaleString('en',{month:'long'})

オンラインでお試しください!

コメント済み

d =>                     // d = input day
  ( n =                  //
    ( d =                // convert d to
      new Date(1, 0, d)  //   a Date object for the non leap year 1901
    ).getDate()          // save the corresponding day of month into n
  ) + (                  //
    [, 'st', 'nd', 'rd'] // ordinal suffixes
    [n % 30 % 20]        // map { 1, 2, 3, 21, 22, 23, 31 } to { 'st', 'nd', 'rd' }
    || 'th'              // or use 'th' for everything else
  ) + ' ' +              // append a space
  d.toLocaleString(      // convert d to ...
    'en',                // ... the English ...
    { month: 'long' }    // ... month name
  )                      //

日付組み込みなし、188バイト

f=(d,m=0)=>d>(k=31-(1115212>>m*2&3))?f(d-k,m+1):d+([,'st','nd','rd'][d%30%20]||'th')+' '+`JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember`.match(/.[a-z]*/g)[m]

オンラインでお試しください!


毎月11日、12日、13日失敗
期限切れデータ

1
@ExpiredDataこれを報告していただきありがとうございます。修正されました。
アーナルド

コメントを無視して、ID10Tエラーを作成しました。
アスガルント

nodejsが言語タグをどのように処理するかはわかりませんが、を使用してもを使用して0も機能するよう"en"です。に変更するtoLocaleStringと、4バイト節約されます。110バイト
tsh

@tsh toLocaleString認識されない文字列または数値が渡されたときにシステムのデフォルト設定を使用しているようです。だから、それは何でも構いません。英語のロケールのみがインストールされるため、このパラメーターは基本的にTIOインスタンスでは無効です。
アーナウルド

4

Smalltalk、126バイト

d:=Date year:1day:n.k:=m:=d dayOfMonth.10<k&(k<14)and:[k:=0].o:={#st.#nd.#rd}at:k\\10ifAbsent:#th.m asString,o,' ',d monthName

1
Smalltalkはわかりませんが、これは正しいです11th,12th,13thか?私が正しく読んだ場合、あなたはその日を10で整数除算し11st,12nd,13rdますが、それはコードに何か他の何かがそれを修正しない限り、それを知らない間に結果になることを意味します。
ケビンクルーイッセン

@KevinCruijssenあなたは正しいです。これについて私の注意をお寄せいただきありがとうございます。これを修正するには、もう少しバイトを費やす必要があります。
レアンドロカニリア

1
@KevinCruijssen、完了 再度、感謝します。
レアンドロカニリア

3

C#の(ビジュアルC#インタラクティブコンパイラ)141の 139 133 124 122バイト

a=>{var d=s.AddDays(a-1);int x=d.Day,m=x%30%20;return x+"thstndrd".Substring(m<4?m*2:0,2)+d.ToString(" MMMM");};DateTime s

Arnauldに感謝します

オンラインでお試しください!


a=>{var d=s.AddDays(a-1);int x=d.Day,m=x%30%20;return x+"thstndrd"[(m<4?m*2:0)..2]+$" {d:MMMM}";};DateTime s ただし、C#8を使用すると、これを次のように削減できます 。ただし、現時点では、対話型コンパイラは言語レベルを「プレビュー」に変更することをサポートしていないようです。
19:07のアルカノックス


後にセミコロンを追加する必要があると確信していますDataTime s
無知の具現化

3

R158 134バイト

「st」、「nd」、「rd」、「th」をゴルフするための-24バイト@Nick Kennedy。ありがとう!

f=format;paste0(a<-as.double(f(d<-as.Date(scan(,''),'%j'),'%e')),`if`((a-1)%%10>2|a%/%10==1,'th',c("st","nd","rd")[a%%10]),f(d,' %B'))

オンラインでお試しください!



はい、`if`もっとよく学ぶ必要があります。ありがとう。
CTホール

3

MySQL、47 45 42バイト

SELECT DATE_FORMAT(MAKEDATE(1,n),"%D %M")

1901は、うるう年であった/ない年に置き換えることができます。

編集:@Embodyment of Ignoranceのおかげで、スペースを削除して2バイトを節約し、年を1に変更してさらに3バイトを節約しました


1901, nと文字列の間のスペースを削除できますか?
無知の

@EmbodimentofIgnoranceはい、できます、ありがとう!
NicolasB

また、1901を1のような年に置き換えてみませんか?1はうるう年ではなく、3バイト短くなります
無知の具現化

@EmbodimentofIgnorance完了および完了:-)
NicolasB

3

05AB1E81 79 78 76 75 74 73 71 70 69 バイト

•ΘÏF•ºS₂+.¥-D0›©ÏθDT‰ć≠*4šß„—ÊØ3ôsè¨ð”……‚應…ä†ï€¿…Ë…ê†Ä…æ…Ì…Í”#®OèJ

@Grimyのおかげで-9バイト。
@JonathanAllanのJelly answerで使用しstandard theトリックのおかげで-1バイト。 th,st,nd,rd

オンラインそれを試してみたり、すべての可能なテストケースを確認してください

説明:

•ΘÏF        # Push compressed integer 5254545
     º       # Mirror it vertically: 52545455454525
      S      # Converted to a list of digits: [5,2,5,4,5,4,5,5,4,5,4,5,2,5]
       ₂+    # And 26 to each: [31,28,31,30,31,30,31,31,30,31,30,31,28,31]
             # (the additional trailing 28,31 won't cause any issues)
           # Undelta this list (with automatic leading 0):
             #  [0,31,59,90,120,151,181,212,243,273,304,334,365,393,424]
  -          # Subtract each from the (implicit) input-integer
   D0       # Duplicate the list, and check for each if it's positive (> 0)
      ©      # Store the resulting list in the register (without popping)
       Ï     # Only leave the values at those truthy indices
        θ    # And get the last value from the list, which is our day
D            # Duplicate this day
 T          # Take the divmod-10 of this day: [day//10, day%10]
   ć         # Extract the head; pop and push the remainder-list and head: [day%10], day//10
            # Check whether the day//10 is NOT 1 (0 if day//10 == 1; 1 otherwise)
     *       # Multiply that by the [day%10] value
      4š     # Prepend a 4 to this list
        ß    # Pop and push the minimum of the two (so the result is one of [0,1,2,3,4],
             # where the values are mapped like this: 1..3→1..3; 4..9→4; 10..19→0; 20..23→0..3; 24..29→4; 30,31→0,1)
 thŠØ       # Push dictionary string "th standards"
      3ô     # Split it into parts of size 3: ["th ","sta","nda","rds"]
        sè   # Swap and index the integer into this list (4 wraps around to index 0)
          ¨  # And remove the trailing character from this string
ð            # Push a space " "
”……‚應…ä†ï€¿…Ë…ê†Ä…æ…Ì…Í”
             # Push dictionary string "December January February March April May June July August September October November"
 #           # Split on spaces
  ®          # Push the list of truthy/falsey values from the register again
   O         # Get the amount of truthy values by taking the sum
    è        # Use that to index into the string-list of months (12 wraps around to index 0)
J            # Join everything on the stack together to a single string
             # (and output the result implicitly)

この05AB1Eのヒントを参照して、理由を理解してください。

  • (セクション辞書の使用方法?”……‚應…ä†ï€¿…Ë…ê†Ä…æ…Ì…Í”"December January February March April May June July August September October November"
  • (セクション辞書の使用方法?…thŠØ"th standards"
  • (セクション大きな整数を圧縮する方法?•ΘÏF•5254545

1
:-2圧縮のため5в28+を使用してバイトTIO
Grimmy

1
Sを使用することをお勧めします。再び-1バイト:TIO
Grimmy

1
@Grimyの-1バイトに感謝し•EË7Óæ•S₂+ますが、残念ながら-3ゴルフはうまくいきません。05AB1Eではインデックス作成が自動的にラップ5st,6nd,7rd,25st,26nd,27rd,29stされるため、間違っています。PS:それは働いているならば、されている可能性の追加のために-1。:)
ケビンクルーッセン

1
再び-1(「the the standard」の代わりに「th standard」を使用すると、の必要がなくなりますÁ)。
グリムリー

1
-1•C.ñÒā•to •ΘÏF•º、余分な数字は重要ではありません)
Grimmy

2

bash、82 80バイト

@ASCIIのみのおかげで-2バイト

a=(th st nd rd);set `printf "%(%e %B)T" $[$1*86399]`;echo $1${a[$1%30%20]-th} $2

TIO

bash + GNU日付、77バイト

a=(th st nd rd);set `date -d@$[$1*86399] +%e\ %B`;echo $1${a[$1%30%20]-th} $2


@ASCIIのみ、各日で100を減算、はい、1日(86400)未満の100 * 365 = 36500s、86399(1日で1を減算)でも動作します
Nahuel Fouilleul

:/はまだ本当に長い見えますが、まだ、より良い方法を発見していない
ASCIIのみ

2

シェル+ coreutils、112 90バイト

date -d0-12-31\ $1day +%-dth\ %B|sed 's/1th/1st/;s/2th/2nd/;s/3th/3rd/;s/\(1.\).. /\1th /'

オンラインでお試しください!リンクにはテストケースが含まれます。編集:@NahuelFouilleulのおかげで22バイトを保存しました。説明:

date -d0-12-31\ $1day

うるう年でない前の最初の日以降の日数を計算します。(残念ながら、からの相対的な日付計算はできません@-1。)

+%-dth\ %B|sed

月の日(先行ゼロなし)、、thおよび完全な月名を出力します。

's/1th/1st/;s/2th/2nd/;s/3th/3rd/;

アップ修正1st2nd3rd21st22nd23rd31st

s/\(1.\).. /\1th /'

に復元11th13thます。


私は、私の後にこの答えを見救うことができる18bytesをまた、1つのsedコマンドを使用してsdays除去することができ、191969
ナウエルFouilleul

@NahuelFouilleul最後の1つはBash-ismを使用しているため、別の回答として投稿する必要がありますが、他のヒントをありがとう!
ニール

2

ゼリー115の 114 101 97バイト

%30%20¹0<?4Ḥ+ؽị“thstndrd”ṭ
“5<Ḟ’b4+28ÄŻ_@µ>0T,>0$ƇZṪµ1ịị“£ṢtẒ⁽ẹ½MḊxɲȧėAṅ ɓaṾ¥D¹ṀẏD8÷ṬØ»Ḳ¤,2ịÇƊṚK

オンラインでお試しください!

ジェリーの標準では長いが、第一原理から行われた。

文字列圧縮の理解を深め、13バイトを節約してくれた@JonathanAllanに感謝します。


“£ṢtẒ⁽ẹ½MḊxɲȧėAṅ ɓaṾ¥D¹ṀẏD8÷ṬØ»Ḳ¤13を節約します(Compress.dictionaryは先頭のスペースを探し、特別な処理を行います)。
ジョナサンアラン


1

、124バイト

func[n][d: 1-1-1 + n - 1[rejoin[d/4 either 5 > t: d/4 % 30 % 20[pick[th st nd rd]t + 1]['th]]pick system/locale/months d/3]]

オンラインでお試しください!

n日付を形成するために1-1-1(2001年1月1日)に-1日を追加します。Arnauldのメソッドを使用して、月のサフィックスにインデックスを付けます。残念レッドが、これは追加の微調整を必要とし、1-インデックスされます。良いことは、レッドが月の名前を知っていることです:)


1

APL(NARS)、235文字、470バイト

{k←↑⍸0<w←+\v←(1-⍵),(12⍴28)+13561787⊤⍨12⍴4⋄k<2:¯1⋄d←1+v[k]-w[k]⋄(⍕d),({d∊11..13:'th'⋄1=10∣d:'st'⋄2=10∣d:'nd'⋄3=10∣d:'rd'⋄'th'}),' ',(k-1)⊃(m≠' ')⊂m←'January February March April May June July August September October November December'}

13561787は、各月の長さを取得するために基数4で合計できる数値(12)28)です。テスト:

  f←{k←↑⍸0<w←+\v←(1-⍵),(12⍴28)+13561787⊤⍨12⍴4⋄k<2:¯1⋄d←1+v[k]-w[k]⋄(⍕d),({d∊11..13:'th'⋄1=10∣d:'st'⋄2=10∣d:'nd'⋄3=10∣d:'rd'⋄'th'}),' ',(k-1)⊃(m≠' ')⊂m←'January February March April May June July August September October November December'}     
  ⊃f¨1 2 3 365 60 11
1st January  
2nd January  
3rd January  
31st December
1st March    
11th January 


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.