Days in indexed month

APL (Dyalog Unicode), 13 12 bytesSBCS

Anonymous tacit prefix function. 0-indexed. February is 29.

31-1∘=+2|7∘|

Try it online!

31- thirty-one minus…

1∘= the equal-to-one'ness…

  + plus…

   2| the two-mod of…

    7∘| the seven-mod'ness

The reason I ask this near-duplicate is curiosity about mathematical patterns in the sequence of numbers. For example, a simple observation is that even-indexed months have 31 until August and then it flips — but golf that observation and/or make better ones that yield better strokes...

This solution implements the following mathematical pattern:

$$f(x)=31-[x=1]-\big((x\mod 7)\mod 2\big)$$

or

$$f(x)=31-x-\left[x=1\right]+7\left\lfloor\frac{x}7\right\rfloor+2\left\lfloor x-7\left\lfloor\frac{x}7\right\rfloor\over2\right\rfloor$$


Poetic, 706 bytes

counting a day,one month<t-o>twelve
i got frustrated doing a list
i do say,o,i edited a list,a month&a day count
in twelve(in December)to one(January)i listed a day"s count,then the numeral months
and once i am ready,o,i think i got a number to say
a number,o,i know a count
o,i say i am thirty if one"s short
i am ready,o,i know a count is not a thirty if we switch it
a long,a short,a etc.i switch
i do think i want a count of Febr-u"ry=two n eight:a case of a month"s age as shorter than the others"s
a day"s a day,i reckon
i know,i am clever
o,a date"s count at min=a thirty-day,or affix a one,i think
i do start&i produce threes if ever i am thirty
P.S.note:i index by one,i receive any-format numbers

Try it online!

As noted in the final line of the program, this program takes months as 1-based indices, and can take both simple numbers and numbers with a leading 0. (How's that for code comments?)

In pseudocode, my approach was basically like this:

N = input(); // Poetic doesn't support native numerical input, only input as ASCII codes; I basically have to write myself a number input routine every time
DIGIT = 49 // ASCII value for "1"; I would set it to 3, but that would be slightly longer
if (N>7) N++; // this accounts for the July->August parity shift
if (N!=2) { // if not February...
    DIGIT += 2; // now it's "3"
    print(DIGIT); // output: "3"
    DIGIT -= 3; // and now it's "0"...
    if (N%2==1) THREE++; // ...unless N is odd
    print(DIGIT); // output: "30" if short month, "31" if long month
} else { // if February...
    DIGIT += 1; // now it's "2"
    print(DIGIT); // output: "2"
    DIGIT += 6; // now it's "8"
    print(DIGIT); // output: "28"
}

Pyke, 4 bytes

~%R@

I have no idea how does this work (actually, after reading the source, I think I do, but Pyke seems extremely weird to me) as I simply used the second half of the Pyke answer by @Blue in the related question OP linked to. Try it online!

Tags:

Date

Code Golf