68 lines
1.4 KiB
Python
68 lines
1.4 KiB
Python
|
#!/usr/bin/env python3
|
||
|
import numpy as np
|
||
|
import csv
|
||
|
import itertools
|
||
|
import math
|
||
|
|
||
|
import scipy.special
|
||
|
|
||
|
#o(n) = (n-1) * o(n-2) <- pairs
|
||
|
def number_pairs(size):
|
||
|
if size<2:
|
||
|
return 1
|
||
|
n=size-1
|
||
|
return n*number_pairs(size-2)
|
||
|
|
||
|
|
||
|
#https://stackoverflow.com/questions/5360220/how-to-split-a-list-into-pairs-in-all-possible-ways
|
||
|
def all_pairs(lst):
|
||
|
if len(lst) < 2:
|
||
|
yield lst
|
||
|
return
|
||
|
a = lst[0]
|
||
|
for i in range(1,len(lst)):
|
||
|
pair = (a,lst[i])
|
||
|
for rest in all_pairs(lst[1:i]+lst[i+1:]):
|
||
|
yield [pair] + rest
|
||
|
|
||
|
def all_triples(lst):
|
||
|
if len(lst) < 3:
|
||
|
yield lst
|
||
|
return
|
||
|
a = lst[0]
|
||
|
for i in range(1,len(lst)):
|
||
|
for j in range(i+1,len(lst)):
|
||
|
pair = (a,lst[i],lst[j])
|
||
|
for rest in all_triples(lst[1:i]+lst[i+1:j]+lst[j+1:]):
|
||
|
yield [pair] + rest
|
||
|
|
||
|
def all_quadruples(lst):
|
||
|
if len(lst) < 4:
|
||
|
yield lst
|
||
|
return
|
||
|
a = lst[0]
|
||
|
for i in range(1,len(lst)):
|
||
|
for j in range(i+1,len(lst)):
|
||
|
for k in range(j+1,len(lst)):
|
||
|
pair = (a,lst[i],lst[j],lst[k])
|
||
|
for rest in all_quadruples(lst[1:i]+lst[i+1:j]+lst[j+1:k]+lst[k+1:]):
|
||
|
yield [pair] + rest
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
#a=[1,2,3,4]
|
||
|
a=[1,2,3,4,5,6]
|
||
|
|
||
|
print(number_pairs(24))
|
||
|
|
||
|
|
||
|
count=0
|
||
|
for i in all_pairs(a):
|
||
|
count+=1
|
||
|
#print(i)
|
||
|
|
||
|
print(count)
|