Decepticon C# challenge, dates, in Python

Updated TrustyTony 1 Tallied Votes 267 Views Share

I did code for challenge that I saw posted for C# by deceptikon:
http://www.daniweb.com/software-development/csharp/threads/479304/coding-challenge-string-to-date

Here it is. Of course normally the code would be without prints and put in function, but this is enough for now for me. There could also be normalization from '/' separated form not producing ambiguous form, checking for single digit month/day.

Notice that reversed order of slice assignement is a must as well as variable holding the original length or that length's format!

cases = """1114
112014
01114
0112014
10114
1012014
010114
01012014
01/10/14
"""


formats = dict(((4, (1, 2)),
               (5, (1, 3)),
               (6, (2, 4)),
               (7, (1, 3)),
               (8, (2, 4))))

for case in cases.split():
    print case
    # special case the first zero included, second dropped, 4 number year
    if len(case) == 7 and case.startswith('0'):
        case = case[:2] + '0' + case[2:]

    elif len(case) == 6 and case[2:4] in ('19','20'):
        print 'Ambigious case!'
    case_processed = list(case.lstrip('0').replace('/',''))
    #print case_processed
    #print formats[len(case_processed)]
    format_found = formats[len(case_processed)]
    for  ind in reversed(format_found):
        case_processed[ind:ind] = '/'
    #print case_processed
    case_processed = ''.join(case_processed)
    print case_processed
    print '-'*14