Agus Makmun

I am freelance developer. Currently doing more in backend, focused in Python and Django.

get Month Name [CF]

10 Jun 2016 » python, codefights

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