19
0
Write a function or program that takes string inputs, fully-spelled, English month names in title case: January
, February
, March
, etc. (null/CR/LF terminated OK, delimited with some non-alpha character if you so choose) and either
compares two inputs, returning a Truthy value if the second input is greater (in month order) than the first. Equal values result in a Falsey value
or sorts an arbitrary sequence (list, delimited string, etc.) of them in chronological order
(The crux of the challenge is defining a method/expression that gives the correct lexicographical sort. Some languages might have a shorter answer with one or the other)
You cannot use any internal time parsing methods (e.g. strptime
) to translate the month name into a number or a pre-canned mapping of month names. Use properties of the strings themselves, a parsimonious look-up table you define, or something clever.
Example
Functioning examples, though the first is prohibited by the rules...
import datetime
def is_later_month(a, b):
'''
Example of prohibited code because it relies on language
features about how to parse month names
'''
return datetime.strptime(a, '%B') < datetime.strptime(b, '%B')
The below versions are OK though, because we code that info
months = {
'January': 1, 'February': 2, 'March': 3,
'April': 4, 'May': 5, 'June': 6,
'July': 7, 'August': 8, 'September': 9,
'October': 10, 'November': 11, 'December': 12,
}
def is_later_month(a, b):
"""
Returns True/False when comparing two months.
"""
return months[a] < months[b]
Or you could do a sorting function
months = {'as above...'}
def sort_months(l):
"""
Sorts list and returns it. Different input and output than the above,
but equally valid. Sorting versus comparing might be shorter in your
favorite language.
"""
return sorted(l, key=lambda x: months[x])
Example tests
assert is_later_month('January', 'February')
assert is_later_month('January', 'December')
assert is_later_month('November', 'December')
assert not is_later_month('July', 'July')
assert not is_later_month('October', 'September')
You cannot use any internal time parsing methods (e.g. strptime) to translate the month name into a number. This is a bit unclear. Can we use a language's predefined literal that contains the months' names? – Luis Mendo – 2016-08-11T00:40:00.203
I'll delete my answer then. But it's still not clear what is allowed and what is not. – Luis Mendo – 2016-08-11T01:02:39.033
The problem is you can't anticipate all those potential tricks, such as predefined arrays. Perhaps a better option would have been to use a less common set of strings, such as made-up names. But it's too late now for that I guess – Luis Mendo – 2016-08-11T01:08:04.197
Is what I'm expressing clear? If Python had a builtin
months
that was a list of all Month names, I'd want to forbidmonths[x] < months[y]
as an answer. The list of month names has some more peculiar features (varying length, commonality) that make the challenge easier/harder over randomly generated strings. – Nick T – 2016-08-11T01:13:38.247Yes, I think it's clear. I just fear that there may be other similar cases that you haven't explicitly ruled out (but I don't know which ones) – Luis Mendo – 2016-08-11T01:39:04.510
Can someone please provide (as a comment here) an example of a solution that violates the rule: "You cannot use any internal time parsing methods (e.g. strptime) to translate the month name into a number or a pre-canned mapping of month names." ? – Bevo – 2016-08-12T16:19:23.530
@Bevo there's one in the question above (marked "prohibited", using
strptime
). The "spirit of the rules" are that the month names are to be treated as arbitrary strings with an arbitrary order. Any a priori knowledge that a language might have about that string:order mapping is prohibited. – Nick T – 2016-08-12T17:14:55.743