Algorithm


Problem Name: Python - List Comprehensions

Problem Link: https://www.hackerrank.com/challenges/list-comprehensions/problem?isFullScreen=true

In this HackerRank Functions in PYTHON problem solution,

Let's learn about list comprehensions! You are given three integers x, y and z representing the dimensions of a cuboid along with an integer n.Print a list of all possible

coordinates given by (i,j,k) on a 3D grid where the sum of

i + j + k is not equal to n. Here

0 < = i <= x; 0 <= j <= y ; 0 <= k <= z Please use list comprehensions rather than multiple loops, as a learning exercise.

Example

x = 1

y = 1

z = 2

n = 3

All permutations of [i,j,k] are:

[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2],[1,0,0],[1,0,1],[1,0,2],[1,1,0],[1,1,1],[1,1,2]]

Print an array of the elements that do not sum to n = 3.

[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,0,2],[1,1,2]]

 

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming


from itertools import product
if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    xd = [i for i in range(x+1)]
    yd = [i for i in range(y+1)]
    zd = [i for i in range(z+1)]
    print([list(x) for x in product(xd,yd,zd) if sum(x)!=n])
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Print Function in PYTHON solution in Hackerrank
Next
[Solved] Find the Runner-Up Score! in PYTHON solution in Hackerrank