0

It is possible to do this program with a single repetition structure

# Print all pairs of numbers in array A whose sum is greater than 10 A = [4,5,2,1,3,7] for value_1 in A: for value_2 in A: total = value_1 + value_2 if (total > 10): print (value_1, " + ", value_2, " > 10")

17th Feb 2017, 11:39 PM
Javier I. Rivera R.
Javier I. Rivera R. - avatar
2 odpowiedzi
+ 2
I think it's not possible because you have to pick every number in A => for loop and every number should be combined with every number in A => second for loop.
18th Feb 2017, 12:25 AM
Marco
Marco - avatar
+ 1
Well, it actually is: from itertools import combinations A = [4, 5, 2, 1, 3, 7] B = combinations(A, 2) for pair in B: if pair[0] + pair[1] > 10: print(pair[0], "+", pair[1], "> 10") You can also try list comprefensions, but they'll still actually use two iterating constructions.
18th Feb 2017, 5:31 AM
DotJason