+ 1

X = int(input()) Y = int(input()) Z = int(input()) N = int(input()) ans = [[i, j, k] for i in range(X + 1) for j in range(Y

can someone explain this code? https://code.sololearn.com/cpW9tGC1lOQ0/?ref=app

27th Oct 2020, 3:10 PM
Jaya Pratha
Jaya Pratha - avatar
2 Answers
+ 4
If you take the expression out of the list comprehension and put it this way it might be more clear for you to understand what it is suppose to do for i in range(X+1): for j in range(Y+1): for k in range(Z+1): if(i+j+k)!=N: print([i,j,k]) So suppose all the inputs are 1 Now when the loop starts i=0,j=0,k=0,since 0+0+0 is not equal to 1 it prints [0,0,0] And as you know inner loop is executed first so in the next iteration we have i=0,j=0,k=1,since 0+0+1 is equal to 1 ,it doesn't prints this series , In next iteration we will have i=0,j=1,k=0 ,then for i=0,j=1,k=1 and so on
27th Oct 2020, 3:27 PM
Abhay
Abhay - avatar
+ 3
Jaya Pratha , the code creates a number of combinations (permutations) in a list using nested loops. Similar things can also be done by using python module itertools.permutation.
27th Oct 2020, 3:22 PM
Lothar
Lothar - avatar