def allcombinations(A, B, ordered):
 if (ordered == 'commutative'):
  ordered = 11
 if (ordered == 'associative'):
  ordered = None
 (sm, bg) = ((A, B) if (len(A) < len(B)) else (B, A))
 for part in kbins(list(range(len(bg))), len(sm), ordered=ordered):
  if (bg == B):
   (yield (tuple(((a,) for a in A)), partition(B, part)))
  else:
   (yield (partition(A, part), tuple(((b,) for b in B))))

def allcombinations ( A , B , ordered ) : if ( ordered == 'commutative' ) : ordered = 11 if ( ordered == 'associative' ) : ordered = None ( sm , bg ) = ( ( A , B ) if ( len ( A ) < len ( B ) ) else ( B , A ) ) for part in kbins ( list ( range ( len ( bg ) ) ) , len ( sm ) , ordered = ordered ) : if ( bg == B ) : ( yield ( tuple ( ( ( a , ) for a in A ) ) , partition ( B , part ) ) ) else : ( yield ( partition ( A , part ) , tuple ( ( ( b , ) for b in B ) ) ) )
restructure a and b to have the same number of elements ordered must be either commutative or associative a and b can be rearranged so that the larger of the two lists is reorganized into smaller sublists .