立ち位置・仕様
- いろんなアルゴリズムをコピペできるように記録するのみ
- 最適化が目的でない
- たまにメモとして解説を残す
- 最低限でまとめる
- データを抽出しやすいように個別のIDを付与
- あるアルゴリズムにおいて、他のアルゴリズムが登場する際にはそのIDを付与
- IDは16進数表記
- なるべく最低限なパッケージで実装
- なるべく特殊なオブジェクト型は使用しない
TargetID
00.00.00.17
ReferenceID
桁ごとに異なる進数を持つ数値の列挙(i番目からj番目まで)
- インクリメント式に計算するのではなく、10進数→数値を一発で計算
- 出力はインデックス
from pprint import pprint
def enumeration(l, startIdx, endIdx):
m = len(l)
n_list = [len(i) for i in l]
ns_list = [n_list[0]]
for i in range(1,m):
ns_list.append(ns_list[-1]*n_list[i])
out = []
for idx in range(startIdx, endIdx):
a = []
if ns_list[-1]<=idx:
break
for j in reversed(range(1,m)):
if n_list[j] == 1:
a.append(0)
continue
ns = ns_list[j-1]
q = idx//ns
idx -= q*ns
a.append(q)
a.append(idx%ns_list[0])
out.append(list(reversed(a)))
return out
使用方法
l = [[0,1,2,3], [6], [4,5]]
lst = enumeration(l, 0, 100)
pprint(lst)
"""
[[0, 0, 0],
[1, 0, 0],
[2, 0, 0],
[3, 0, 0],
[0, 0, 1],
[1, 0, 1],
[2, 0, 1],
[3, 0, 1]]
"""