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-0054

Posted at

タスク概要

Sum of Nth decimal places

コード実装例

TIPS

  1. 例外処理含む評価パターンを追加
  2. 小数値入力を考慮
import pprint, sys, time

def core(arg, acc=True):
    a, b, n = arg[:3]
    if acc:
        ret = []
    else:
        digits = str(a / b).split(".")[1][:n]
        ret = [sum([int(i) for i in digits]), digits]
    return ret

def app(*args):
    ret = []
    for arg in args:
        s = []
        for acc in [True, False]:
            st = time.time()
            try:
                r = core(arg, acc=acc)
            except Exception as e:
                r = e
            s.append([acc, round(time.time() - st, 6), r])
        ret.append([arg, s])
    return ret

print(sys.version)
pprint.pprint(app(
    # basic examples
    [1, 2, 3],
    [2, 3, 4],
    [5, 4, 3],
    [4, 3, 2],
    # additional examples
    [-1.1, 2.5, 2],
    # exceptional examples
    "例外入力"
))

実行結果

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

3.8.10
[GCC 9.4.0]
[[[1, 2, 3], [[True, 1e-06, []], [False, 1.7e-05, [5, '5']]]],
 [[2, 3, 4], [[True, 1e-06, []], [False, 5e-06, [24, '6666']]]],
 [[5, 4, 3], [[True, 0.0, []], [False, 2e-06, [7, '25']]]],
 [[4, 3, 2], [[True, 0.0, []], [False, 3e-06, [6, '33']]]],
 [[-1.1, 2.5, 2], [[True, 0.0, []], [False, 3e-06, [8, '44']]]],
 ['例外入力',
  [[True, 2e-06, []],
   [False,
    3e-06,
    TypeError("unsupported operand type(s) for /: 'str' and 'str'")]]]]

補遺

残課題

  1. 実行速度の改善(枝刈り、データ構造の見直し等)
0
0
1

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?