समस्या
A weather club wants a small helper for its sign-up form: members type the month of their birthday as a word (for example October) and the day as a number, and the form shows the season they were born in. The club uses these fixed dates for the Northern Hemisphere:
- Spring: March 20 to June 20
- Summer: June 21 to September 21
- Autumn: September 22 to December 20
- Winter: December 21 to March 19
All ranges include both end dates.
Implement season_from_date(month, day).
Input
month: The month as a full English name with a capital first letter, such as "March". Any other text ("march", "Mar", "") is invalid.
day: The day of the month. It is valid from 1 up to the number of days in that month; February allows 29.
Output
Return "Spring", "Summer", "Autumn" or "Winter". If the month name or the day is not valid, return "Invalid".
सीमाएँ
month has at most 20 characters
−1000 ≤ day ≤ 1000
उदाहरण
इनपुट:
month = "April"
day = 11
अपेक्षित आउटपुट:
expected = "Spring"
इनपुट:
std::string month = "April";
long long day = 11;
अपेक्षित आउटपुट:
std::string expected = "Spring";
इनपुट:
string month = "April";
long day = 11;
अपेक्षित आउटपुट:
string expected = "Spring";
व्याख्या
April 11 falls between March 20 and June 20, so the answer is "Spring".
इनपुट:
month = "February"
day = 30
अपेक्षित आउटपुट:
expected = "Invalid"
इनपुट:
std::string month = "February";
long long day = 30;
अपेक्षित आउटपुट:
std::string expected = "Invalid";
इनपुट:
string month = "February";
long day = 30;
अपेक्षित आउटपुट:
string expected = "Invalid";
व्याख्या
February never has 30 days, so the date is "Invalid".
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Check that the month is one of the twelve names and that the day fits that month first.
संकेत 2
Comparing month first and day second lets you test whether a date is inside a range.
संकेत 3
Winter wraps around the new year, so handle it as whatever is left after the other three seasons.
अपेक्षित जटिलता
O(1) time, O(1) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।