タスク概要
Card Game
コード実装例
TIPS
- 例外処理含む評価パターンを追加
import pprint, sys, time
def core(arg, adv=True, n_cards=10, s_limit=20, k=3):
my_cards = arg[0]
your_cards = arg[1]
n_cards = max([n_cards] + my_cards + your_cards)
if adv:
candidates = [n for n in range(1, s_limit - sum(my_cards)) if n not in my_cards + your_cards]
else:
candidates = [n for n in range(1, n_cards + 1) if n not in my_cards + your_cards and sum(my_cards + [n]) <= s_limit]
flag = "YES" if len(candidates) >= 0.5 * n_cards else "NO"
return [flag, n_cards, candidates[:k]]
def app(*args):
ret = []
for arg in args:
s = []
for adv in [False, True]:
st = time.time()
try:
r = core(arg, adv=adv)
except Exception as e:
r = e
s.append([adv, round(time.time() - st, 6), r])
ret.append([arg, s])
return ret
print(sys.version)
pprint.pprint(app(
# basic examples
[[1, 2], [3]],
[[5, 6], [9]],
[[8, 9], [10]],
# additional examples
[[1000000, 3], [1]],
[[4, 7], [1000000]],
# exceptional examples
"例外入力"
))
実行結果
各種補足情報出力(入力値FB、デバッグログ等)含む
3.8.10 (default, Sep 28 2021, 16:10:42)
[GCC 9.3.0]
[[[[1, 2], [3]],
[[False, 1e-05, ['YES', 10, [4, 5, 6]]],
[True, 5e-06, ['YES', 10, [4, 5, 6]]]]],
[[[5, 6], [9]],
[[False, 5e-06, ['YES', 10, [1, 2, 3]]],
[True, 3e-06, ['YES', 10, [1, 2, 3]]]]],
[[[8, 9], [10]],
[[False, 4e-06, ['NO', 10, [1, 2, 3]]], [True, 2e-06, ['NO', 10, [1, 2]]]]],
[[[1000000, 3], [1]],
[[False, 0.267864, ['NO', 1000000, []]], [True, 1e-05, ['NO', 1000000, []]]]],
[[[4, 7], [1000000]],
[[False, 0.26715, ['NO', 1000000, [1, 2, 3]]],
[True, 5e-06, ['NO', 1000000, [1, 2, 3]]]]],
['例外入力',
[[False, 8e-06, TypeError('can only concatenate list (not "str") to list')],
[True, 3e-06, TypeError('can only concatenate list (not "str") to list')]]]]
補遺
残課題(Pull Request絶賛募集中!)
- 実行速度の改善(枝刈り、データ構造の見直し等)
- コア関数の汎用化