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?

いろんなアルゴリズム"00.00.00.17"

Last updated at Posted at 2025-06-09

知恵の館の目次

立ち位置・仕様

  • いろんなアルゴリズムをコピペできるように記録するのみ
  • 最適化が目的でない
  • たまにメモとして解説を残す
  • 最低限でまとめる
  • データを抽出しやすいように個別の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]]
"""
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?