HackerRank Solution: List comprehensions [3 Methods]

Author: Bashir Alam
Reviewer: Deepak Prasad

Question: Python List Comprehensions [Basic Data Types]

Letโs learn about list comprehension! 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, 1, 0],  [1, 1, 2]]
``````

Input Format:

Four integers x, y, z and n, each on a separate line.

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:

Each variable `x`, `y` and `z` will have values of `0` or `1`. All permutations of lists in the form `[i, j, k]` = `[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]`.

Remove all arrays that sum to `n = 2` to leave only the valid permutations.

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]]`

Possible solutions

Here we will discuss the possible solutions for the given problem. The following code is already given in the hacker rank website:

``````if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())``````

Let us discuss the following possible solutions:

Solution-1: Using nested for loops

We will now use the nested for loops to solve the problem:

``````if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
l = []
for i in range(x+1):
for j in range(y+1):
for k in range(z+1):
if i+j+k == n:
continue
l.append([i,j,k])
print(l)``````

The solution takes four integers as input (`x`, `y`, `z`, and `n`)ย and uses them to generate a list of lists of integers. It uses nested for loops to iterate through all possible combinations of the integers `i`, `j`, and `k` such that `0 <= i <= x`, `0 <= j <= y`, and `0 <= k <= z`. For each combination, it checks if the sum of `i`, `j`, and `k` is equal to `n`. If it is, the combination is skipped and the loop continues. If it is not, the combination is added to a list called `l` as a sublist. Finally, the list of lists is printed.

Solution-2: Using list comprehension

Let us now use the list comprehension to solve the problem:

``````if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())

ls = [[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]
print(ls)``````

Similar to the previous solution, it also takes four integers as input (`x`, `y`, `z`, and `n`) and uses them to generate a list of lists of integers. It is similar to the previous example, but this time it uses a list comprehension to achieve the same goal in a more concise way. The list comprehension iterates through all possible combinations of the integers `i`, `j`, and `k` such that `0 <= i <= x`, `0 <= j <= y`, and `0 <= k <= z` and checks if `i+j+k` is not equal to `n`. If the condition is met it will add `[i,j,k]` as a sub-list to a list "`ls`". Finally, the list of lists is printed.

Solution-3: Alternative method

This time we will use the list comprehension in different way:

``````if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())

lsti = []
lstj = []
lstk = []
lstc = []

lsti = [i for i in range(x+1)]
lstj = [j for j in range(y+1)]
lstk = [k for k in range(z+1)]

lstc =[[i, j, k] for i in lsti for j in lstj for k in lstk if i+j+k!=n]

print(lstc)``````

The code uses list comprehension to create three separate lists, `lsti`, `lstj`, `lstk`, each containing all the integers from `0` up to `x`, `y` and `z` respectively. Then it uses another list comprehension to generate a list of lists lstc, by iterating through each element of `lsti`, `lstj`, `lstk` and creating a sublist of `[i,j,k]` if` i+j+k` not equal n and then append it to the list lstc. Finally, the list of lists is printed.

Summary

In this short article, we discussed how we can solve the list comprehension problem on hacker rank. We covered 3 different solutions with explanation.

Further Readings

Question on Hacker Rank: Python List Comprehension [Basic Data Types]

Views: 1,492

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.