Suppose we have three numbers i, j and k and another number n. We shall have to find the list of all triplets (i, j, k) for which i+j+k not same as n. We shall have to solve this problem using list comprehension strategy.
So, if the input is like i = 1, j = 1, z = 2 and n = 3, then the output will be [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 2]]
To solve this, we will follow these steps −
arr = an array of triplets [x, y, z] for all x in range from 0 to i, for all y in range from 0 to j and for all z in range from 0 to k, when x+y+z is not same as n
Example
Let us see the following implementation to get better understanding −
def solve(i, j, k, n): arr = [[x, y, z] for x in range(i+1) for y in range(j+1) for z in range(k+1) if x+y+z != n] return arr i = 1 j = 1 k = 2 n = 3 print(solve(i, j, k, n))
Input
1, 1, 2, 3
Output
[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 2]]