0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2021-12-18

タスク概要

Sequence

コード実装例

TIPS

  1. 例外処理含む評価パターンを追加
  2. 計算誤差比較例を追加
import pprint, sys, time

def core(arg, adv=True, n_item=10):
    a = arg[0]
    s = arg[1] if len(arg) >= 2 else 0
    if adv:
        d = pow(3, int(n_item / 2))
        odds = [d / pow(3, i) * pow(2, i) for i in range(int((n_item + 1) / 2))]
        evens = [2 * d / pow(3, i) * pow(2, i) for i in range(int(n_item / 2))]
        r = (sum(odds + evens) * a) / d
    else:
        cnst = 3 * (1 - pow(2 / 3, int(n_item / 2))) / (1 - 2 / 3) + pow(1 / 3, int(n_item / 2))
        r = a * cnst
    ret = [r, abs(r - s)]
    return ret

def app(*args):
    ret = []
    for arg in args:
        s = []
        for adv in [True, False]:
            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.0, 7.81481481],
    [2.0, 15.62962963],
    [3.0, 23.44444444],
    # additional examples
    [-1.0],
    # exceptional examples
    "例外入力"
))

実行結果

各種補足情報出力(入力値FB、デバッグログ等)含む

3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0]
[[[1.0, 7.81481481],
  [[True, 1.5e-05, [7.814814814814815, 4.814815213194379e-09]],
   [False, 1.9e-05, [7.818930041152263, 0.004115231152263199]]]],
 [[2.0, 15.62962963],
  [[True, 8e-06, [15.62962962962963, 3.703704010149522e-10]],
   [False, 1e-06, [15.637860082304526, 0.00823045230452557]]]],
 [[3.0, 23.44444444],
  [[True, 7e-06, [23.444444444444443, 4.444441259465748e-09]],
   [False, 2e-06, [23.456790123456788, 0.012345683456786105]]]],
 [[-1.0],
  [[True, 1.1e-05, [-7.814814814814815, 7.814814814814815]],
   [False, 2e-06, [-7.818930041152263, 7.818930041152263]]]],
 ['例外入力',
  [[True,
    1.3e-05,
    TypeError("can't multiply sequence by non-int of type 'float'")],
   [False,
    4e-06,
    TypeError("can't multiply sequence by non-int of type 'float'")]]]]

補遺

残課題(Pull Request絶賛募集中!)

  1. 実行速度の改善(枝刈り、データ構造の見直し等)
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?