Created
December 9, 2012 19:08
-
-
Save 9seconds/4246577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
''' | |
Solving best-before problem | |
http://www.spotify.com/se/jobs/tech/best-before/ | |
''' | |
# ############################################################################ | |
from sys import stdin | |
from itertools import permutations | |
from datetime import date | |
# ############################################################################ | |
def process_line(line): | |
digits = [ | |
int(chunk.strip()) | |
for chunk in line.split('/') | |
] | |
if len(digits) != 3 or any(dig < 0 for dig in digits): | |
raise ValueError | |
variants = ( | |
process_variant(*variant) | |
for variant in permutations(digits) | |
) | |
variants = (variant for variant in variants if variant is not None) | |
return min(variants).isoformat() | |
def process_variant(day, month, year): | |
if year < 1000: | |
year += 2000 | |
try: | |
return date(year, month, day) | |
except: | |
return None | |
if __name__ == '__main__': | |
for line in (l.rstrip() for l in stdin): | |
try: | |
print process_line(line) | |
except (AttributeError, ValueError): | |
print line + ' is illegal' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment