English 中文(简体)
• 更快地制定法律,解决哪一封信在某个公式中代表哪些字母。
原标题:Make code faster to solve which letter represents which digit in a given equation

我将进行数学竞赛,使写作方案能够解决问题。

该法典试图解决这一问题:

WHITE+WATER=PICNIC 每一封不同的字母代表数。 计算由事先知情同意委员会代表的人数。

No imports are allowed (tqdm was just a progress bar). What I tried is below. At the rate of which my computer goes, it is not fast enough for the times competition because it needs to range over 10 to the power of digits there is.

这一问题是否有明确的解决办法?

from tqdm import tqdm

def find_solution():
    for W in tqdm(range(10)):
        for H in tqdm(range(10), desc= H ):
            for I in tqdm(range(10), desc= I ):
                for T in tqdm(range(10)):
                    for E in tqdm(range(10)):
                        for A in (range(10)):
                            for R in (range(10)):
                                for P in (range(1, 10)): # P cannot be 0
                                    for C in (range(10)):
                                        for N in (range(10)):
                                            white = W * 10000 + H * 1000 + I * 100 + T * 10 + E
                                            water = W * 10000 + A * 1000 + T * 100 + E * 10 + R
                                            picnic = P * 100000 + I * 10000 + C * 1000 + N * 100 + I * 10 + C

                                            if white + water == picnic:
                                                return { W : W,  H : H,  I : I,  T : T,  E : E,  A : A,  R : R,  P : P,  C : C,  N : N}

    return None

solution = find_solution()

if solution:
    print("Solution found:")
    print(solution)
else:
    print("No solution found.")
最佳回答

意见:

  1. P=1 due to a carry to its sixth digit
  2. W must be 5-9 due to a carry being generated (1)
  3. No letter can be the same value as another letter.
def find_solution():
    P = 1
    for W in range(5,10):
        for H in range(10):
            if H in (P,W): continue
            for I in range(10):
                if I in (H,P,W): continue
                for T in range(10):
                    if T in (I,H,P,W): continue
                    for E in range(10):
                        if E in (T,I,H,P,W): continue
                        for A in range(10):
                            if A in (E,T,I,H,P,W): continue
                            for R in range(10):
                                if R in (A,E,T,I,H,P,W): continue
                                for C in range(10):
                                    if C in (R,A,E,T,I,H,P,W): continue
                                    for N in range(10):
                                        if N in (C,R,A,E,T,I,H,P,W): continue
                                        white = W * 10000 + H * 1000 + I * 100 + T * 10 + E
                                        water = W * 10000 + A * 1000 + T * 100 + E * 10 + R
                                        picnic = P * 100000 + I * 10000 + C * 1000 + N * 100 + I * 10 + C

                                        if white + water == picnic:
                                            print(f  WHITE= {white} )
                                            print(f  WATER= {water} )
                                            print(f PICNIC={picnic} )
                                            print()

find_solution()

产出(2项解决办法,约1项):

 WHITE= 83642
 WATER= 85427
PICNIC=169069

 WHITE= 85642
 WATER= 83427
PICNIC=169069

当然,如果你能够使用图书馆(约0.3秒)。 Note the documentation of itertools.permutations 只执行粗 Python的甲草胺,但低于上文硬编码版本:

from itertools import permutations

def find_solution():
    P=1
    for W,H,I,T,E,A,R,C,N in permutations([0,2,3,4,5,6,7,8,9]):
        if W < 5: continue
        white = W * 10000 + H * 1000 + I * 100 + T * 10 + E
        water = W * 10000 + A * 1000 + T * 100 + E * 10 + R
        picnic = P * 100000 + I * 10000 + C * 1000 + N * 100 + I * 10 + C

        if white + water == picnic:
            print(f  WHITE= {white} )
            print(f  WATER= {water} )
            print(f PICNIC={picnic} )
            print()

find_solution()
问题回答

I ve除去了 t木进口品,用固定范围 lo替代了nes树脂。 这应当使法典更加简单、快捷,而不使用外部进口。

def find_solution():
    for W in range(10):
        for H in range(10):
            for I in range(10):
                for T in range(10):
                    for E in range(10):
                        for A in range(10):
                            for R in range(10):
                                for P in range(1, 10):  # P cannot be 0
                                    for C in range(10):
                                        for N in range(10):
                                            white = W * 10000 + H * 1000 + I * 100 + T * 10 + E
                                            water = W * 10000 + A * 1000 + T * 100 + E * 10 + R
                                            picnic = P * 100000 + I * 10000 + C * 1000 + N * 100 + I * 10 + C

                                            if white + water == picnic:
                                                return { W : W,  H : H,  I : I,  T : T,  E : E,  A : A,  R : R,  P : P,  C : C,  N : N}

    return None

solution = find_solution()

if solution:
    print("Solution found:")
    print(solution)
else:
    print("No solution found.")




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签