some OT: how to solve this kind of problem in our program?

John Machin sjmachin at lexicon.net
Sun Dec 24 00:13:18 EST 2006


oyster wrote:
[snip]
> I have written the case 1 in python, it needs 90 seconds on my pc, and
> the same approach in www.freebasic.net takes less than 1 seconds
> [code for python]
> import sets
> import time
> try:
>   import psyco
>   psyco.full()
> except:
>   pass
>
> d0, d1=1, 2
>
> st=time.time()
> result=[]
> for a0 in range(1,10):
>   for a1 in sets.Set(range(1,10))-sets.Set([a0]):
>     for a2 in sets.Set(range(1,10))-sets.Set([a0,a1]):
>       a1a2=a1*10+a2
>       if 2*a0< a1a2:
>         for b0 in sets.Set(range(1,10))-sets.Set([a0,a1,a2]):
>           for b1 in sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0]):
>             for b2 in sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0, b1]):
>               b1b2=b1*10+b2
>               if 2*a0*b1b2 + 2*b0*a1a2 < a1a2*b1b2:
>                 for c0 in sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0, b1, b2]):
>                   for c1 in
> sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0, b1, b2, c0]):
>                     for c2 in
> sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0, b1, b2, c0, c1]):

Has it not occurred to you to "calculate" sets.Set(range(1,10)) *ONCE*,
store it, and reuse it?

For the remaining uses of sets.Set, do
   set = sets.Set
once up the front, and remember to remove it when (as you should!) you
upgrade off Python 2.3.

HTH,
John




More information about the Python-list mailing list