29
\$\begingroup\$

This simple challenge is very similar to this one: How many days in a month? The only difference is you yield the number of days in a month given the index of the month instead of its name.

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...

INPUT: Integer indicating the month; 1 through 12. (Optionally, you can accept 0-indexed month numbers from 0 to 11.)

OUTPUT: Number of days in that month. February can return 28 or 29.

Complete I/O table:

INPUT   :   OUTPUT
  1          31
  2          28 or 29 
  3          31
  4          30
  5          31
  6          30
  7          31
  8          31
  9          30
 10          31
 11          30
 12          31
\$\endgroup\$
0

36 Answers 36

1
2
1
\$\begingroup\$

C# (Visual C# Compiler), 28 bytes

x=>DateTime.DaysInMonth(1,x)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ This is exactly the old revision of Embodiment's C# answer: codegolf.stackexchange.com/posts/195901/revisions :( \$\endgroup\$ Commented Nov 15, 2019 at 15:16
  • \$\begingroup\$ Ohh, I didn't see it in the answers so I assumed no one had posted this yet \$\endgroup\$
    – Joost K
    Commented Nov 15, 2019 at 15:17
1
\$\begingroup\$

Pyth, 12 bytes

--31%%Q7 2q1

Try it online!

Port of @Adám's answer

Pyth, 22 18 bytes

-+30@+*4U2t*3U2Qq2

Try it online!

Uses 1-indexed months and 29 as February. Theres probably a more refined solution somewhere Saved 4 bytes by using U2 instead of ,Z1 and replacing the logic with -1 if input is 2

How it works

-+30@+*4U2t*3U2Qq2
 +30               - 30 plus...
    @          Q   - The index Q=input
     +*4U2         - Of 4 times the list [0,1] -> [0,1,0,1,0,1,0,1]
          t*3U2    - Plus 3 times the list [0,1] minus the first element
-               q2 - Minus [2 == input], with input added implicitly
\$\endgroup\$
1
\$\begingroup\$

Haskell, 27 bytes

f 1=28;f m=31-mod m 7`mod`2

Try it online!

0-indexed. Handles February the same way that Andrew Ray's answer does, but otherwise uses the mod-7-mod-2 approach.

\$\endgroup\$
1
\$\begingroup\$

C# .NET, 98 bytes

class P{static void Main(string[]a){System.Console.Write((int)" "[int.Parse(a[0])]);}}

Try Online
Uses Ascii control characters (RS, US and FS) and gets their absolute character value.

\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 18 bytes

31-(Ans=1)-2fPart(3.5fPart(Ans/7

Takes input in Ans as a 0-indexed month. Returns 29 for February.

18 bytes

101+Ans(Ans≠11
dbd(Ans,Ans+1

Alternative solution using a different approach. Takes input in Ans as a 0-indexed month. Returns 29 for February.

\$\endgroup\$
1
\$\begingroup\$

Thunno 2, 9 bytes

7%ɗ$ḅ+31_

Try it online!

Port of Adám's APL answer.

Explanation

7%ɗ$ḅ+31_  # Implicit input
      31_  # 31 - (
7%ɗ        #   (input % 7 % 2)
     +     #   +
   $ḅ      #   (input == 1)
           # )
           # Implicit output
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.