Let me give you a simple example.
I want to create a code that obtains all two possible bundles when A={1,2,3,4}
.
The answer to the above number is
(1,2)(3,4): {1,2,3,4} and (1,3)(2,4): {1,3,2,4}
B=[1,2,3,4,5,6]When expanded to
(1,2)(3,4)(5,6)
(1,2)(3,5)(4,6)
(1,3)(2,4)(5,6)
(1,3)(2,5)(4,6)
(1,4)(2,3)(5,6)
(1,4)(2,6)(3,5)
(1,5)(2,3)(4,6)
(1,5)(2,4)(3,6)
(1,6)(2,3)(4,5)
(1,6)(2,4)(3,5)
It's going to be like this way.
How can it be implemented in Python or Matlab?
python matlab
>>> def get_pairs(s, depth=0):
# # print('>'*depth, s)
# # depth+=1
if len(s) == 2:
#print(s)
yield (tuple(s),)
for i in range(1, len(s)):
pair = (s[0], s[i])
s_pair = s[1:]
s_pair.remove(s[i])
for pairs in get_pairs(s_pair):
#print(pair, pairs)
yield (pair, *pairs)
>>> r = [ p for p in get_pairs(s) ]
>>> from pprint import pprint
>>> pprint(r)
[((1, 2), (3, 4), (5, 6)),
((1, 2), (3, 5), (4, 6)),
((1, 2), (3, 6), (4, 5)),
((1, 3), (2, 4), (5, 6)),
((1, 3), (2, 5), (4, 6)),
((1, 3), (2, 6), (4, 5)),
((1, 4), (2, 3), (5, 6)),
((1, 4), (2, 5), (3, 6)),
((1, 4), (2, 6), (3, 5)),
((1, 5), (2, 3), (4, 6)),
((1, 5), (2, 4), (3, 6)),
((1, 5), (2, 6), (3, 4)),
((1, 6), (2, 3), (4, 5)),
((1, 6), (2, 4), (3, 5)),
((1, 6), (2, 5), (3, 4))]
>>> pprint(list(get_pairs([1,2,3,4])))
[((1, 2), (3, 4)), ((1, 3), (2, 4)), ((1, 4), (2, 3))]
© 2025 OneMinuteCode. All rights reserved.