get Month Name [CF]

Map the given integer to a month.

Example:

  • For mo = 1, the output should be getMonthName(mo) = "Jan",
  • For mo = 0, the output should be getMonthName(mo) = "invalid month".

Input/Output

  • [time limit] 4000ms (py)
  • [input] integer mo (A non-negative integer).
  • Constraints: 0 ≤ mo ≤ 15.
  • [output] string

A 3-letter abbreviation of month number mo or "invalid month" if the month doesn’t exist.

Here are abbreviations of all months:

My Solution:

def getMonthName(mo):
    months = {
        1: "Jan", 2: "Feb", 3: "Mar", 4:"Apr", 
        5: "May", 6: "Jun", 7: "Jul", 8:"Aug", 
        9: "Sep", 10: "Oct", 11: "Nov", 12: "Dec"
    }
    if mo in months.keys():
        return months.get(mo)
    return "invalid month"

Result Tests:

>>> getMonthName(1)
"Jan"
>>> getMonthName(0)
"invalid month"
>>>
AM

Written by Agus Makmun Author

SDE Lead working across the whole stack: backend, frontend, databases and the infrastructure that ships them. These days mostly infrastructure and Next.js, like moving apps off Vercel and Supabase to self-hosted.

arrow_backPrevious post Find the number of even digits in the given integer [CF]
search esc