For example:
a list just like m=[[2,3],[3,4],[1,2],[4,7],[1,7]]
every element is a little list[x,y]
, x is not equal to y.
if element a and b in m follow: a[1]==b[0]
, then a and b will "combined into" c=[a[0],b[1]]
,
after that,a and b will remove from m,
and c will added into m.
of course,length of m reduced by one.
my question is :
after several times combined oprations,
is there same element exist in new m?
if yes, return True else return False.
I write a demo below
from random import choice
def combine_two(a,b):
c = [None,None]
if a[1]==b[0]:
c[0]=a[0]
c[1]=b[1]
elif b[1] == a[0]:
c[0]=b[0]
c[1]=a[1]
return c
def fusion(list):
ss = list[:]
nn = len(ss)
for i in range(100):
ele_1 = choice(ss)
ele_2 = choice(ss)
c = combine_two(ele_1,ele_2)
if c != [None,None]:
ss.remove(ele_1)
ss.remove(ele_2)
ss.append(c)
return ss
def check(list):
n = len(list)
for i in range(n):
for j in range(n):
if i != j:
if list[i][0] == list[j][0] and list[i][1] == list[j][1]:
return False
return True
jj = [[2,3],[3,4],[1,2],[4,7],[11,13],[1,7]]
m = fusion(jj)
print m
n = check(m)
print n
but my fusion function is a workaround.
if the input list is very long. it will go wrong.
I have no idea of how to handle 2 element at a time Intelligently.
is there a better way to achieve this function?