これは、Set と Line Length のすべての値に対して私が見つけた一般的な解決策です。1 つ目は、2 つのソリューションが同じ共通要素を共有することは望まないが、各ソリューションには他のすべてのソリューションと共通の要素が 1 つあることを前提としています。フォームを選択する無限のプールがある場合、ソリューションの総数は各ソリューションの長さによって制限されます。
SET_LENGTH = 10
CHOICE_LENGTH = 300
data = set(range(CHOICE_LENGTH))
solutions =[]
solution_sets = []
used = set()
while True:
new_solution = []
#Try to get unique values from each previous set
try:
for sol_set in solution_sets:
while True:
candidate = sol_set.pop()
if not candidate in used:
new_solution.append(candidate)
used.update([candidate])
break
except KeyError, e:
print e
break
#Fill with new data until the line is long enough
try:
while len(new_solution) < SET_LENGTH:
new_solution.append(data.pop())
except KeyError, e:
print e
break
solutions.append(new_solution)
solution_sets.append(set(new_solution))
#Show the results
for solution in solutions:
print solution
print "Orphans %s" % len(data)
たとえば、n = 300 x = 10 の場合:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[1, 10, 19, 20, 21, 22, 23, 24, 25, 26]
[2, 11, 19, 27, 28, 29, 30, 31, 32, 33]
[3, 12, 20, 32, 34, 35, 36, 37, 38, 39]
[4, 13, 21, 33, 34, 40, 41, 42, 43, 44]
[5, 14, 22, 27, 36, 40, 45, 46, 47, 48]
[6, 15, 23, 28, 37, 41, 45, 49, 50, 51]
[7, 16, 24, 29, 38, 42, 47, 49, 52, 53]
[8, 17, 25, 30, 39, 43, 48, 50, 52, 54]
[9, 18, 26, 31, 35, 44, 46, 51, 53, 54]
Orphans 245
同じ共通要素を共有するソリューションの数が気にならない場合は、さらに簡単です。
SET_LENGTH = 2
CHOICE_LENGTH = 300
data = set(range(CHOICE_LENGTH))
solutions =[]
alpha = data.pop()
while True:
new_solution = [alpha]
try:
[new_solution.append(data.pop()) for x in range(SET_LENGTH-1)]
except KeyError, e:
break
solutions.append(new_solution)
for solution in solutions:
print solution
print "Solutions: %s" % len(solutions)
print "Orphans: %s" % len(data)