
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to find list of triplets for which i+j+k is not same as n
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]]
- Related Articles
- Program to find smallest index for which array element is also same as index in Python
- Program to find higher number with same number of set bits as n in Python?\n
- C++ program to find range whose sum is same as n
- Program to check if the given list has Pythagorean Triplets or not in Python
- C++ program to find permutation for which sum of adjacent elements sort is same as given array
- Program to find an element in list whose value is same as its frequency in Python
- Program to find number of good triplets in Python
- C++ program to find number of l-r pairs for which XORed results same as summation
- Program to check we can find four elements whose sum is same as k or not in Python
- Program to check we can find three unique elements ose sum is same as k or not Python
- Program to remove all nodes of a linked list whose value is same as in Python
- Program to find minimum number of subsequence whose concatenation is same as target in python
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Python - Ways to create triplets from given list
- Python program to find N largest elements from a list

Advertisements