23
\$\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\$

34 Answers 34

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\$

Javascript n=>15662007>>~-n*2&3|28 20 characters

This is utilizing a binary map 15662007 which is

111011101111101110110111 11.10.11.10.11.11.10.11.10.11.01.11 3. 2. 3. 2. 3. 3. 2. 3. 2. 3. 1. 3

You just add each item to 28 and see that it provides a proper mapping to each month.

\$\endgroup\$
0
\$\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\$
0
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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