조금 늦었지만 표시되지 않은 방법이 있다고 생각합니다.
나는 모든 K 항목과 정수 인덱스의 시작 순서가 주어지면 대략 K에 비례하여 K 항목의 인덱스 순열을 생성하는 알고리즘임을 기억했다. K! 0과 K 사이의 정수를 무작위로 생성 할 수있는 한 K 항목의 (팩토리얼) 순열! 루틴을 사용하여 N 개의 고유 한 임의의 인덱스를 메모리에 생성 한 다음 해당 순열을 디스크에 인쇄 할 수 있습니다.
:
가
from math import factorial
from copy import copy
import random
def perm_at_index(items, index):
'''
>>> for i in range(10):
print i, perm_at_index([1,2,3], i)
0 [1, 2, 3]
1 [1, 3, 2]
2 [2, 1, 3]
3 [2, 3, 1]
4 [3, 1, 2]
5 [3, 2, 1]
6 [1, 2, 3]
7 [1, 3, 2]
8 [2, 1, 3]
9 [2, 3, 1]
'''
itms, perm = items[:], []
itmspop, lenitms, permappend = itms.pop, len(itms), perm.append
thisfact = factorial(lenitms)
thisindex = index % thisfact
while itms:
thisfact /= lenitms
thischoice, thisindex = divmod(thisindex, thisfact)
permappend(itmspop(thischoice))
lenitms -= 1
return perm
if __name__ == '__main__':
N = 10 # Change to 1 million
k = 25 # Change to 144
K = ['K%03i' % j for j in range(k)] # ['K000', 'K001', 'K002', 'K003', ...]
maxperm = factorial(k) # You need arbitrary length integers for this!
indices = set(random.randint(0, maxperm) for r in range(N))
while len(indices) < N:
indices |= set(random.randint(0, maxperm) for r in range(N - len(indices)))
for index in indices:
print (' '.join(perm_at_index(K, index)))
출력이있는 다음과 같은 : I는 K가 = 144이 성공적으로 사용하고 있지만 여기
이 25 N을 10로 설정 K와 알고리즘 파이썬 버전
K008 K016 K024 K014 K003 K007 K015 K018 K009 K006 K021 K012 K017 K013 K022 K020 K005 K000 K010 K001 K011 K002 K019 K004 K023
K006 K001 K023 K008 K004 K017 K015 K009 K021 K020 K013 K000 K012 K014 K016 K002 K022 K007 K005 K018 K010 K019 K011 K003 K024
K004 K017 K008 K002 K009 K020 K001 K019 K018 K013 K000 K005 K023 K014 K021 K015 K010 K012 K016 K003 K024 K022 K011 K006 K007
K023 K013 K016 K022 K014 K024 K011 K019 K001 K004 K010 K017 K018 K002 K000 K008 K006 K009 K003 K021 K005 K020 K012 K015 K007
K007 K001 K013 K003 K023 K022 K016 K017 K014 K018 K020 K015 K006 K004 K011 K009 K000 K012 K002 K024 K008 K021 K005 K010 K019
K002 K023 K004 K005 K024 K001 K006 K007 K014 K021 K015 K012 K022 K013 K020 K011 K008 K003 K017 K016 K019 K010 K009 K000 K018
K001 K004 K007 K024 K011 K022 K017 K023 K002 K003 K006 K021 K010 K014 K013 K020 K012 K016 K019 K000 K015 K008 K018 K009 K005
K009 K003 K010 K008 K020 K024 K007 K018 K023 K013 K001 K019 K006 K002 K016 K000 K004 K017 K014 K011 K022 K021 K012 K005 K015
K006 K009 K018 K010 K015 K016 K011 K008 K001 K013 K003 K004 K002 K005 K022 K020 K021 K017 K000 K019 K024 K012 K023 K014 K007
K017 K006 K010 K015 K018 K004 K000 K022 K024 K020 K014 K001 K023 K016 K005 K011 K002 K007 K009 K013 K019 K012 K021 K003 K008
문제 가능한 RAM, 아니면이 속도? –
약간의 개선 : 2 단계와 3 단계를 여러 번 반복하지 않도록하려면 1.1N 순열을 생성하고 중복을 제거한 다음 첫 번째 N을 가져옵니다. 순열 수가 N보다 작 으면 2 단계로 이동하십시오. – Shahbaz
언제나 (순차적으로) 순열을 디스크에 쓰고 디스크에 최적화 된 정렬 알고리즘을 사용하면 RAM에서 모든 작업을 훨씬 느리게 수행 할 수 있습니다. – amit