我在Python中创建一个程序,找到可以匹配以下乘法字母的数字,例如:
OTTO x
____I
STOP
(意思是OTTO x I = STOP)
这是我一直在研究的代码,但似乎并没有停止......
# OTTO x I = STOP
times = 1
digit_o = 0
digit_t = 0
digit_i = 1
digit_s = 0
digit_p = 0
concatenation_otto = str(str(digit_o) + str(digit_t) + str(digit_t) + str(digit_o))
concatenation_stop = str(str(digit_s) + str(digit_t) + str(digit_o) + str(digit_p))
def amend() :
concatenation_otto = str(str(digit_o) + str(digit_t) + str(digit_t) + str(digit_o))
concatenation_stop = str(str(digit_s) + str(digit_t) + str(digit_o) + str(digit_p))
amend()
print "working..."
while (concatenation_otto * digit_i == concatenation_stop) :
while times == 7 :
if times == 1 :
digit_o += 1
times += 1
print digit_o + " / " + str(times)
amend()
break
elif times == 2 :
digit_t += 1
times += 1
amend()
break
elif times == 3 :
digit_i += 1
times += 1
amend()
break
elif times == 4 :
digit_s += 1
times += 1
amend()
break
elif times == 5 :
digit_p += 1
times += 1
amend()
break
times == 1
continue
print digit_o
print concatenation_otto + " x " + str(digit_i) + " = " + concatenation_stop
请帮忙,我希望你完全明白我的意思。谢谢!
答案 0 :(得分:2)
没有必要重新发明轮子。使用itertools.permutations
创建数字的5个排列,然后检查结果。
import itertools
for O, T, I, S, P in itertools.permutations((1,2,3,4,5,6,7,8,9), 5):
OTTO = 1000 * O + 100 * T + 10 * T + O
STOP = 1000 * S + 100 * T + 10 * O + P
if OTTO * I == STOP:
print(O, T, I, S, P)
break