0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

覆面算の穴埋め

Posted at

覆面算を解くプログラムをPythonで作りました。
READ + WRITE + TALK = SKILL のアルファベットを数字に置き換えて、覆面算を解きます。
最上位に0は含みません。
違うアルファベットには違う数値が入ります。

import copy

def main():

    l_r = [ii for ii in range(10)]

    for r in l_r:
        l_e = remove_list_method(l_r,r)
        for e in l_e:
            l_a = remove_list_method(l_e,e)
            for a in l_a:
                l_d = remove_list_method(l_a,a)
                for d in l_d:
                    l_w = remove_list_method(l_d,d)
                    for w in l_w:
                        l_i = remove_list_method(l_w,w)
                        for i in l_i:
                            l_t = remove_list_method(l_i,i)
                            for t in l_t:
                                l_l = remove_list_method(l_t,t)
                                for l in l_l:
                                    l_k = remove_list_method(l_l,l)
                                    for k in l_k:
                                        l_s = remove_list_method(l_k,k)
                                        for s in l_s:

                                            if r == 0 or w == 0 or t == 0 or s == 0:
                                                continue

                                            one = (r * 1000) + (e * 100) + (a * 10) + d
                                            two = (w * 10000) + (r * 1000) + (i * 100) + (t * 10) + e
                                            three = (t * 1000) + (a * 100) + (l * 10) + k
                                            ans = (s * 10000) + (k * 1000) + (i * 100) + (l * 10) + l

                                            if one + two + three == ans:
                                                print(one," + ",two," + ",three," = ",ans)

def remove_list_method(l_c,d):

    l = copy.copy(l_c)
    l.remove(d)

    return l

if __name__ == "__main__":
    main()
l_r = [ii for ii in range(10)]

l_rリストに、[0,1,2,3,4,5,6,7,8,9]のリストを作って、r,e,a,d,w,i,t,l,k,sの必要なアルファベットのfor文を入れ子構造にして回していきます。
その際に、次のforループに入る前に、remove_list_methodで一つ前のループで使ってるリストから、一つ前のループの現在の数値を削除して、そのリストを次のループの探索リストにする事で、重複数値を回避します。
これで、違うアルファベットには違う数値が入ります。

if r == 0 or w == 0 or t == 0 or s == 0:
    continue

ここで、READ、WRITE、TALK、SKILLのそれぞれの最上位、R、W、T、Sが0だった場合は、continue文で、ループ処理を1つ進めます。

one = (r * 1000) + (e * 100) + (a * 10) + d
two = (w * 10000) + (r * 1000) + (i * 100) + (t * 10) + e
three = (t * 1000) + (a * 100) + (l * 10) + k
ans = (s * 10000) + (k * 1000) + (i * 100) + (l * 10) + l

if one + two + three == ans:
    print(one," + ",two," + ",three," = ",ans)

oneにREAD、twoにWRITE、threeにTALK、ansにSKILLのアルファベットに対応する数値を代入して覆面算が成り立つか、if文で判定します。
判定が真なら表示します。
結果は、

1632  +  41976  +  7380  =  50988
2543  +  72065  +  6491  =  81099
4905  +  24689  +  8017  =  37611
5094  +  75310  +  1962  =  82366
5096  +  35710  +  1982  =  42788
5180  +  65921  +  2843  =  73944
5270  +  85132  +  3764  =  94166
7092  +  37510  +  1986  =  46588
7092  +  47310  +  1986  =  56388
9728  +  19467  +  6205  =  35400

の10通りが答えです。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?