Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8
It's just a rough idea, but it can do this task? from itertools import combinations as comb lst = ['A','B','C','D'] res = list(comb(lst,2)) pairs = [] buf = [] for i in range(0,len(res),2): buf.append((res[i])) buf.append((res[i+1])) pairs.append(buf[:]) buf.clear() for i in pairs: print(i) ''' [('A', 'B'), ('A', 'C')] [('A', 'D'), ('B', 'C')] [('B', 'D'), ('C', 'D')] '''
4th Mar 2020, 5:12 PM
Lothar
Lothar - avatar
+ 4
you can check on the permutation function
4th Mar 2020, 4:01 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
Small introduction to the permutation method here: https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2466/
4th Mar 2020, 4:03 PM
Seb TheS
Seb TheS - avatar
4th Mar 2020, 8:03 PM
Lothar
Lothar - avatar
+ 3
Here is my attempt although with bigger numbers the grouping is not really optimized :) but it was fun to play with sets. https://code.sololearn.com/cVQ3g2F5tjNG/?ref=app
4th Mar 2020, 8:38 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Jakub Jaczewski Does the order matter? Are: [('A', 'B'), ('C', 'D')] And: [('C', 'D'), ('A', 'B')] The same?
4th Mar 2020, 4:08 PM
Seb TheS
Seb TheS - avatar
+ 2
Hi Jakub Jaczewski This is an interesting problem, and good example of a real world programming problem. Engineers, programmers and the like, normally just want to jump in and solve the problem, but the most important part of programming is not taught in the beginning. The most important thing is to understand is the user requirement. What is the problem this program is solving. For example if you say it is like football league, then it could mean that home games and away games are important. One can have the convention that the home team is always mentioned first, ie (A, B) means a homegame for A, where (B, A) means its a homegame for B. I think you need to expand a bit more on the user requirement.
6th Mar 2020, 12:13 PM
Louis
Louis - avatar
+ 1
Jakub Jaczewski Maybe it takes too much time for larger numbers.
6th Mar 2020, 6:44 PM
Seb TheS
Seb TheS - avatar
0
Hi Jakub Jaczewski , in case of unordered pairs of 4 elements it’s quite simple solution. {[(‘A’,’B’),(‘C’,’D’)],[(‘A’,’C’),(‘B’,’D’)],[(‘A’,’D’),(‘B’,’C’)]} And that’s all! If order is important than you can easy take it, changing order in each pair. It will give you 4 combinations from each one.
6th Mar 2020, 3:18 PM
Vadym M
Vadym M - avatar