タスク概要
Sum of Nth decimal places
コード実装例
TIPS
- 例外処理含む評価パターンを追加
- 小数値入力を考慮
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'")]]]]
補遺
残課題
- 実行速度の改善(枝刈り、データ構造の見直し等)