HackerRank List Comprehensions solution in Python

HackerRank List Comprehensions solution in Python

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. You have to 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.

Input Format

Four integers X, Y, Z  and N each on four separate lines, respectively.

Constraints

Print the list in lexicographic increasing order.

Sample Input 0

1

1

1

2

Sample Output 0

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

Explanation 0

Concept

You have already used lists in previous hacks. List comprehensions are an elegant way to build a list without having to use different for loops to append values one by one. This example might help.

Example: You are given two integers x and y . You need to find out the ordered pairs ( i , j ) , such that ( i + j ) is not equal to n and print them in lexicographic order.( 0 <= i <= x ) and ( 0 <= j <= y) This is the code if we dont use list comprehensions in Python.

python x = int ( raw_input()) y = int ( raw_input()) n = int ( raw_input()) ar = [] p = 0 for i in range ( x + 1 ) : for j in range( y + 1): if i+j != n: ar.append([]) ar[p] = [ i , j ] p+=1 print ar
Other smaller codes may also exist, but using list comprehensions is always a good option. Code using list comprehensions:

python x = int ( raw_input()) y = int ( raw_input()) n = int ( raw_input()) print [ [ i, j] for i in range( x + 1) for j in range( y + 1) if ( ( i + j ) != n )]

Sample Input 1

2

2

2

2

Sample Output 1[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 2], [0, 2, 1], [0, 2, 2], [1, 0, 0], [1, 0, 2], [1, 1, 1], [1, 1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]]

Solution in Python3

x,y,z,n = [int(input()) for i in range(4)]
print([[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if ((i+j+k) != n)])

This solution is written in Python3 and is using a concept called list comprehension. List comprehension is an elegant way to build a list without having to use different for loops to append values one by one.

Step 1: The first line of code is taking four integers X, Y, Z and N as input, each on four separate lines, respectively. It assigns them to a variable called x, y, z, n respectively.

Step 2: The second line of code is the list comprehension. It starts with creating an empty list. Then it creates three nested for loops, each for the variables i, j and k. The range of each variable is from 0 to the input value + 1. The variable i is looping through the range of x+1, variable j is looping through the range of y+1 and variable k is looping through the range of z+1.

Step 3: Inside the list comprehension, there is an if statement that checks if the sum of i, j, and k is not equal to N. If this condition is true, it appends the current values of i, j, k to the list as a sublist.

Step 4: Once the for loops have completed, the final list comprehension will contain all possible coordinates given by (i, j, k) on a 3D grid where the sum of (i+j+k) is not equal to N. This final list is then printed out.

In summary, this solution is using list comprehension to create a 3D grid of coordinates, where the sum of the coordinates is not equal to N. And it also prints the final list of coordinates in lexicographic increasing order.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe