[PyAR2] Car Talk Puzzler

Jesse Jaggars jhjaggars at gmail.com
Mon Jun 20 00:53:35 CEST 2011

Here is my solution that disregards leading zeros in the hour spot.
Feel free to point out the bugs.

from collections import defaultdict

def fmt(bit):
    return "%02d" % bit

def test(hour, minute):
    counter = defaultdict(int)
    for pos in str(hour) + fmt(minute):
        counter[pos] += 1

    for key,value in counter.iteritems():
        if value >= 3:
            return True

    return False

if __name__ == "__main__":
    times = 0
    for half in ('am', 'pm'):
        for hour in range(1,13):
            for minute in range(0, 60):
                if test(hour,minute):
                    print "%s:%s %s" % (str(hour), fmt(minute), half)
                    times += 1
    print times

On Mon, May 2, 2011 at 11:45 AM, Greg Lindstrom <gslindstrom at gmail.com>wrote:

> Each week, the radio program "Car Talk" (NPR) offers up a puzzler; a quirky
> problem that usually requires some sort of "aha" to solve.  Sometimes
> they're mathematical, sometimes not.  This week, the puzzler asks "How many
> times in a day does a digital clock (in 12-hour mode) display a time with 3
> of the same digits (1:11, 2:22, etc.).  It's a "trick" question, of course,
> but it took less than 5 minutes for me to write a python program to solve it
> (my boss will be happy to know that because I should be working).
> Can you write a routine to solve it?
> _______________________________________________
> PyAR2 mailing list
> PyAR2 at python.org
> http://mail.python.org/mailman/listinfo/pyar2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/pyar2/attachments/20110619/fdde34f9/attachment.html>

More information about the PyAR2 mailing list