Have we turned everyone off? No? Let’s throw in some programming!

Since I figure my LCS posts don’t turn enough of my regular readers off (the ones who like to hear juicy stories from my now-boring personal life!) and since LCS is now off the air, I guess I’ll move onto some more esoteric and geeky fun.

My friend Mr. Wellons loves his number theory and programming puzzles! He recently offered up this fairly simple one:

What is a number that has the largest number of its permutations be possible dates without slashes?

For example, 01082008 is a date, but another permutation, 08102080 also is.

This was a pretty easy problem, here was my program to generate my solution:

#!/usr/bin/env python

import datetime

def is_date(num):
    padder = str(num).zfill( 8 )
    try:
        return datetime.date(int(padder[4:8]),
                             int(padder[0:2]),
                             int(padder[2:4]))
    except:
        return 0

def normalize(num):
    ls = list(str(num).zfill(8))
    ls.sort()
    return ''.join(ls)

best_date = (0,0)
all_dates = {}
for i in range(1010001,12319999):
    if is_date(i):
        j = normalize(i)
        all_dates[j] = all_dates.get(j,0)+1
        if all_dates[j] > best_date[0]:
            best_date = (all_dates[j],j)
            print best_date

Can anyone improve on that? And does anyone have any ideas as to why there is only one unique answer and it is what it is?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: