A challenge from the Mensa Puzzle Calendar

Gregor Lingl glingl at aon.at
Thu Oct 3 16:52:26 EDT 2002


Chris Myers schrieb:
> I have the "Mensa Puzzle Calendar" on my desktop, and one of the first
> things I do in the morning, aside from checking my email, is to try to
> solve the daily puzzle.  Most of them are word-type problems,
> cryptograms, anagrams, age problems, etc., but today's was a nice
> numerical challenge that lent itself to writing a program to solve it:
> 
> 
> The following multiplication example uses all the digits from 0 to 9,
> and X's have been used to represent numbers, not the multiplication
> sign.
> 
>      7XX
>       XX
>    -----
>    XXXXX
> 
> 
> I wrote (IMHO) a very nice piece of python code to solve this for me,
> and then generalized it a bit: What if the first digit (7) is not
> given?  How many unique solutions do we get and what are they? (I
> included those numbers that started with 0 for consistency.)

Just to contrast yours: a quick and dirty solution using at least
on pythonesque feature:

 >>> def contains_all_digits(a,b,c):
     x = str(a)+str(b)+str(c)
     if len(x) == 9: x+="0" # if one of them has a leading 0
     if len(x) != 10:
         return 0
     for d in "0123456789":
         if d not in x:
             return 0
     return 1

 >>> print [(x,y,x*y) for x in range(1000)
                         for y in range(100)
                           if contains_all_digits(x,y,x*y)]
[(138, 42, 5796), (157, 28, 4396), (159, 48, 7632), (186, 39, 7254), 
(198, 27, 5346), (297, 18, 5346), (297, 54, 16038), (345, 78, 26910), 
(367, 52, 19084), (396, 45, 17820), (402, 39, 15678), (483, 12, 5796), 
(495, 36, 17820), (594, 27, 16038), (715, 46, 32890), (927, 63, 58401)]

Is this correct?

Regards, Gregor L.

> 
> I'm a puzzle kind of guy, so I thought some of you might be, too.
> I'd be very curious to see the kind of solutions people come up with.
> My solution ended up being a total of 19 lines of code, including a
> print statement to give me a nice formatted output for each solution,
> resembling the initial problem from the calendar.
> 
> OK, folks.  The gauntlet is down.  Have at it!
> 
> (NOTE: I realize this is not really a Python thingy, but more of an
> algorithm design thingy, but nonetheless, fun.)





More information about the Python-list mailing list