Skip to content

Instantly share code, notes, and snippets.

@9seconds
Created December 9, 2012 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9seconds/4246577 to your computer and use it in GitHub Desktop.
Save 9seconds/4246577 to your computer and use it in GitHub Desktop.
#!/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