I have an array:
array([[ 4, 10],
[ 4, 2],
[ 0, 7],
[ 5, 11],
[ 6, 8],
[ 3, 6],
[ 9, 7],
[ 2, 11],
[ 9, 5],
[ 8, 1]])
I need a method with which I can arrange pairs of values so that as many paired two-element sets as possible have a common value. This is an example of a desired ordered array:
array([[ 4, 10],
[ 4, 2],
[ 2, 11],
[ 5, 11],
[ 9, 5],
[ 9, 7],
[ 0, 7], #note the gap here:
[ 8, 1],
[ 6, 8],
[ 3, 6]])
There are several conditions in these arrays. No duplicate pairs (ie, [1,0] and [0,1] will appear elsewhere in the array if [0,1] already exists). No pair has the same value (ie: [1,1] will not exist). No pair will have more than two matches (iow: no value more than two times in the whole array), but a pair can only have zero matches (note that in the above array there is a space for which there is no match).
, , . , - , . , : 1) , numpy collections . 2) , numpy .sort() ( ), . 3) , , , . ( ", -!" )
, :
def shuffled(N=12, ans=1):
'''returns is now the unsorted test array'''
r = range(N)
random.shuffle(r)
l = []
for i in range(N):
l.append((r[i-1], r[i]))
random.shuffle(l)
return np.array(l)[ans+1:]
def test_ordered(a):
'''checks if generated array has been sorted'''
c0 = a[1:,0]==a[:-1,0]
c1 = a[1:,0]==a[:-1,1]
c2 = a[1:,1]==a[:-1,0]
c3 = a[1:,1]==a[:-1,1]
cond = c0+c1+c2+c3
ans = sum(numpy.logical_not(cond))
return ans
( ? , .)
:
. 20% , , , , , . , "" "". . . 10 000 N. 0,5 . z - .
!
