概要
Sum of 4 Integers
コード
TIPS:オペランド定義域を可変化
import itertools
def calc_num_opr(sm, inf=0, sup=10):
c = range(inf, sup)
return [[a,b,c,d] for a, b, c, d in itertools.product(c, c, c, c) if sum([a,b,c,d]) == sm]
def core(arg):
num_opr = calc_num_opr(int(arg))
return len(num_opr), num_opr
def app(*args):
return [[arg, core(arg)] for arg in args]
from pprint import pprint
try:
pprint(app(
# base
35,
1,
# additional
51,
# exception
#"str"
))
except Exception as e:
print(e)
実行結果
デバッグログ出力部含む
[[35, (4, [[8, 9, 9, 9], [9, 8, 9, 9], [9, 9, 8, 9], [9, 9, 9, 8]])],
[1, (4, [[0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]])],
[51, (0, [])]]
補足
残課題:探索途中での枝刈り高速化