Files
cryptoanarchywiki.github.io/_posts/2016-06-11-get-month-name-cf.md
agusmakmun 387a4226b1 added posts
2016-06-11 03:50:53 +07:00

48 lines
1003 B
Markdown

---
layout: post
title: "get Month Name [CF]"
date: 2016-06-11 03:43:45 +0700
categories: [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:**
```python
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**:
```python
>>> getMonthName(1)
"Jan"
>>> getMonthName(0)
"invalid month"
>>>
```