Mr Shang

我是一个勤劳的码农,每天在技术的牧场辛勤劳作


导航栏
 » 首页
 » 分类浏览
 » 关于我
 » 我的Github
 » RSS订阅
Group 59

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"
>>>
© Mr Shang - niceshang[#]outlook.com - Powered by Jekyll.