0
0

More than 1 year has passed since last update.

AOJトライに関する知識知見の記録共有 :Volume0-0008

Last updated at Posted at 2021-11-15

概要

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, [])]]

補足

残課題:探索途中での枝刈り高速化

0
0
0

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