파이썬으로 복면산 문제를 만들고있는데 input에 더 많은 단어를 넣어서 답이 나오게는 방법은 어떻게 하나요?
조회수 350회
from itertools import permutations
def to_int(revstr, map):
value = 0
for i in range(len(revstr)):
value += map[revstr[i]] * (10 ** i)
return value
def is_valid(w1, w2, w3, map):
leadings = [map[w1[0]], map[w2[0]], map[w3[0]]]
if 0 in leadings:
return False
a = to_int(w1[::-1], map)
b = to_int(w2[::-1], map)
c = to_int(w3[::-1], map)
return a + b == c
def solve(w1, w2, w3):
solutions = []
letters = sorted(set(w1 + w2 + w3))
digits = list(range(10))
perms = list(permutations(digits, len(letters)))
for perm in perms:
map = {k:v for k, v in zip(letters, perm)}
if is_valid(w1, w2, w3, map):
solutions.append(map.copy())
return solutions
w1, w2, w3 = input(" ").split()
solutions = solve(w1, w2, w3)
for map in solutions:
print(to_int(w1[::-1],map), to_int(w2[::-1], map), to_int(w3[::-1], map))
코드를 봐꺼가면서 단어를 더 넣는건 되는데 한코드로 단어를 더 넣어서 답이나오게는 못하겠네요 ㅠㅠ 어떤씩으로 봐꿔야 할까요?
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력