98
21
Aww, man, this expiry date doesn't write the months with letters! I can't tell if it's expiring on March 10th or October 3rd... Wait, no, never mind, the year says 2012. (alley-oops half-used brick of cheese into the trash can like a pro)
So let's suppose for a moment that you're too busy to try to reason out when this jar of marinara is supposed to expire. You just want the Cliff Notes version: how likely is it that it's past due? Let's write some code!
You know that the manufacturers print the date as an ordered triple of integers, in one of three formats:
YEAR MONTH DAY
MONTH DAY YEAR
DAY MONTH YEAR
And you know that some dates can only be interpreted in one or two ways, not all three: the 55 in 55-11-5
has to be a year, meaning this particular box of Twinkies expired November 5th, 1955. The year is sometimes given in four digits and not two, which can rule out some options. When it's two digits, though, 50..99 means 1950..1999 and 0..49 means 2000..2049.
Your job is to write a program or function that takes an array of integers which is a valid date in at least one of the interpretations above, and outputs a percent chance it is still good. The percent chance is simply the percentage of valid interpretations of the date that are on or later than today's date.
The array of integers will be your language's [Int]
type of length three if it is an argument to a function, and given as either dash-, slash-, or space-separated (you get to pick) integers if used as input on STDIN to a full program.*
"Today's date" can be today's actual date, as obtained through a date function, or the date given in an extra argument to function or extra paramater in STDIN. It may be in Unix epoch seconds, another year-month-day triple entered in one of the three ways above, or another more convenient fashion.
Let's have some examples! The expiry date input will be in the dash-separated style, and assume for the examples below that today's date is July 5th, 2006.
14-12-14
- Both valid interpretations for this (DMY and YMD) are equivalent, December 14, 2014. The output is 100 because this product is definitely still good.8-2-2006
- The last number is a year, for sure, since it has four digits. This could be either February 8th (expired) or August 2nd (still good). The output is 50.6-7-5
- This could be anything! The "July 5th, 2006" interpretation is still good (for one day only), but the remaining two are both in 2005 and should be tossed as quickly as possible. The output is 33.6-5-7
- Here, two out of three interpretations are safe. You can round your decimal up or down, so 66 or 67 are both okay.12-31-99
- Okay, this one is unambiguously from the turn of the century (years from 50 to 99 are 19XX, and 31 can't possibly be a month). A big fat 0, and you really should clean out your fridge more often.
You can safely assume that any input which does not meet the standards above is not privy to the output rules above.
No web requests or standard loopholes. Date handling libraries are allowed. This is code golf: may the shortest program win.
* If you are using brainfuck or some similarly datatype-handicapped language, you can assume the ASCII values of the first three characters in input are the integers for the date. This excludes the four-digit year logic, sure, but I think we would be too astounded by seeing a solution to this in Brainfuck to slight you for it.
39Umm... the current year is 2014, not 2006. Your milk is eight years past its expiry at best. – John Dvorak – 2014-07-06T01:32:51.093
11@JanDvorak I just didn't want to try very hard to construct meaningful examples, so I tweaked today's date to make it easier. – algorithmshark – 2014-07-06T02:08:12.053
1Do we need to account for 29 Feb and leap years? – aditsu quit because SE is EVIL – 2014-07-06T10:16:21.000
Do we have to round? – Martin Ender – 2014-07-06T10:22:47.763
@algorithmshark
6-7-5
example - will it stand for 2006-07-05 or 2016-07-05? – eithed – 2014-07-06T11:20:35.713@eithedog "0..49 means 2000..2049." so the former – Martin Ender – 2014-07-06T12:01:13.180
3Those Twinkies expired December 5th, '55 – David says Reinstate Monica – 2014-07-06T13:14:44.837
7@Dgrin91 don't care, I'll still eat them :D – aditsu quit because SE is EVIL – 2014-07-06T15:35:51.967
Is output on stderr tolerated? – Joey – 2014-07-06T15:52:20.230
Is it safe to assume 6 doesn't mean 1906, 1806, or just 6 A.D.? – Ian D. Scott – 2014-07-06T18:30:21.187
1Do we actually need to support years outside of the 1950-2049 range? – Martin Ender – 2014-07-06T19:00:15.433
1@IanD.Scott: Looking at the examples and the problem description, 6 has to be treated as 2006. – Joey – 2014-07-06T19:00:47.600
@m.buettner Alright, alright, you guys don't have to round the percentage. I didn't think adding in a floor() was such a big deal, but okay. Four digit years have to stay, though, because those actually occur on some (many, nowadays) expiry labels. – algorithmshark – 2014-07-06T21:10:18.537
@algorithmshark Oh no, I don't mind adding the
floor
as long it's a hard rule that everyone has to follow. Gotta fix my year-canonicalisation code, though. – Martin Ender – 2014-07-06T21:58:10.8876In Australia, the milk expires about a week before the use by date – gnibbler – 2014-07-07T05:24:35.610
5You should add a test with a 00 in it, since that can't be a legal day or month. – MtnViewMark – 2014-07-08T05:59:18.817
1Wait, what? Twinkies expire!? – The Guy with The Hat – 2014-07-08T21:12:40.847
This problem implicitly makes the assumption that all three orderings of day/month/year are equally likely. A more accurate model of the problem would provide prior probabilities of each ordering, and require that the programs weight their answers by them. For example, if the three orderings have probabilities 0.5, 0.25, 0.25, then the date
6-7-5
would have the result 50 instead of 33. – Rory O'Kane – 2014-07-09T19:25:35.867@RoryO'Kane: And an even more accurate model would include information what item uses what format. So what? It's still a fun task and problems don't need to be terribly realistic either; they just should be fun. – Joey – 2014-07-09T21:15:50.450
@Јοеу I wasn’t suggesting that this code gold challenge should include prior probabilities; I know that that would be unnecessarily complicated. I was just pointing out an interesting feature of the problem that this is a simulation of, for the curious. – Rory O'Kane – 2014-07-11T06:09:09.050