I have problem with 4 list each one got 4 number:
P1= [3,1,3,4]
P2= [5,4,3,7]
P3= [7,4,8,1]
P4= [10,3,2,1]
I need to get the biggest number in combinational way like: Pair of 2: p1p2, p1p3, p1p4, p2p3, p2p4, p3p4
Pairs of 3: p1p2p3, p1p2p4, p1p3p4, p2p3p4
Pairs of 4: p1p2p3p4
The result for:
p1p2= [5,4,3,7]
p1p3= [7,4,8,4]
p1p4= [10,3,3,4] ……
I wrote this program and I store it in a matrix instead of a list. ‘ n= int (input(“Enter the matrix size:”))
JavaScript
x
32
32
1
matrix=[]
2
for i in range(n): #loop for rows
3
c=[]
4
for j in range(n):# loop for columns
5
j=int(input("enter the value of P["+str(i)+"]"+"["+str(j)+"]"))
6
c.append(j)
7
8
print() # to make a line between the entries
9
matrix.append(c) # store the matrix c in the new empty one (matrix)
10
11
for i in range(n):
12
for j in range(n):
13
print(matrix[i][j], end="")
14
print()
15
##############################################
16
#compar row by row element wise 2 terms
17
m2t=[]
18
for i in range(n-1):
19
for j in range(n):
20
if(matrix[i][j]>=matrix[i+1][j]):
21
m2t.append(matrix[i][j])
22
else:
23
m2t.append(matrix[i+1][j])
24
print(m2t,"#1") # check if it get here or not
25
26
print()
27
#printing the matrix of the 2 terms result
28
for a in range(n-1):
29
for b in range(n):
30
print(m2t, end="")
31
print()
32
Is there a better way to do it?
Advertisement
Answer
There is at least one more way. Wait for more elegant solutions.
JavaScript
1
18
18
1
from itertools import combinations
2
P1= [3,1,3,4]
3
P2= [5,4,3,7]
4
P3= [7,4,8,1]
5
P4= [10,3,2,1]
6
7
Full_List = [P1, P2, P3, P4]
8
9
for r in range(2,5,1):
10
Combinations = combinations(Full_List, r)
11
print("Finding maximum for a combination size of %d"%r)
12
for eachCombination in Combinations:
13
print("Finding combination for tuples ", *eachCombination)
14
print(list(map(max,zip(*eachCombination))))
15
print("Done")
16
print()
17
print()
18
Gives the following output.
JavaScript
1
49
49
1
Finding maximum for a combination size of 2
2
Finding combination for tuples [3, 1, 3, 4] [5, 4, 3, 7]
3
[5, 4, 3, 7]
4
Done
5
6
Finding combination for tuples [3, 1, 3, 4] [7, 4, 8, 1]
7
[7, 4, 8, 4]
8
Done
9
10
Finding combination for tuples [3, 1, 3, 4] [10, 3, 2, 1]
11
[10, 3, 3, 4]
12
Done
13
14
Finding combination for tuples [5, 4, 3, 7] [7, 4, 8, 1]
15
[7, 4, 8, 7]
16
Done
17
18
Finding combination for tuples [5, 4, 3, 7] [10, 3, 2, 1]
19
[10, 4, 3, 7]
20
Done
21
22
Finding combination for tuples [7, 4, 8, 1] [10, 3, 2, 1]
23
[10, 4, 8, 1]
24
Done
25
26
27
Finding maximum for a combination size of 3
28
Finding combination for tuples [3, 1, 3, 4] [5, 4, 3, 7] [7, 4, 8, 1]
29
[7, 4, 8, 7]
30
Done
31
32
Finding combination for tuples [3, 1, 3, 4] [5, 4, 3, 7] [10, 3, 2, 1]
33
[10, 4, 3, 7]
34
Done
35
36
Finding combination for tuples [3, 1, 3, 4] [7, 4, 8, 1] [10, 3, 2, 1]
37
[10, 4, 8, 4]
38
Done
39
40
Finding combination for tuples [5, 4, 3, 7] [7, 4, 8, 1] [10, 3, 2, 1]
41
[10, 4, 8, 7]
42
Done
43
44
45
Finding maximum for a combination size of 4
46
Finding combination for tuples [3, 1, 3, 4] [5, 4, 3, 7] [7, 4, 8, 1] [10, 3, 2, 1]
47
[10, 4, 8, 7]
48
Done
49