Find the coordinates of a cuboid using list comprehension in Python

11,290

Solution 1

range is actually a half-closed function. So, the ending value will not be included in the resulting range.

If X=2, the possible values of Xi can be 0, 1 and 2

In your code, range(X) will give only 0 and 1, if X is 2. You should have used range(X + 1).

X, Y, Z, N = 1, 1, 1, 2
[[x,y,z] for x in range(X + 1) for y in range(Y + 1) for z in range(Z + 1) if x+y+z != N]

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

You can write the same, with itertools.product, like this

X, Y, Z, N = 1, 1, 1, 2
from itertools import product
[list(i) for i in product(range(X + 1), range(Y + 1), range(Z + 1)) if sum(i) != N]

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

Solution 2

Another approch with itertools.product and list comprehension:

In [91]: [list(l) for l in it.product([0,1], repeat=3) if sum(l) != 2]
Out[91]: [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]

Solution 3

try this:

    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    ans=[[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(ans)

as range function doesn't include the ending value and stopped at (n-1)th position so if we use range(X) here it will result just 0 hence +1 should be used to print the list in lexicographic increasing order.

Solution 4

x = 1
y = 1
z = 2
n = 3

arr_3_D = []

for i in range(x+1):
    for j in range(y+1):
        for k in range(z+1):
            if i+j+k != n:
                arr = [i,j,k]
                arr_3_D.append(arr)


print(arr_3_D)

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

Share:
11,290
H.Burns
Author by

H.Burns

Updated on July 18, 2022

Comments

  • H.Burns
    H.Burns almost 2 years

    X, Y and Z are the three coordinates of a cuboid.

    Now X=1,Y=1, Z=1 and N=2.

    I have to generate a list of all possible coordinates on a 3D grid where the sum of Xi + Yi + Zi is not equal to N. If X=2, the possible values of Xi can be 0, 1 and 2. The same applies to Y and Z.

    I have written this below code so far, and it is giving the output as :

    [[0, 0, 0]]
    

    however the expected output is

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

    Below is my code, what is going wrong in my code?

    [[x,y,z] for x in range(X) for y in range(Y) for z in range(Z) if x+y+z != N]
    
    • M4rtini
      M4rtini over 8 years
      Try to print range(1) and you may see the problem
  • LordWilmore
    LordWilmore about 6 years
    Please add explanation around your answer.
  • Tasnuva Leeya
    Tasnuva Leeya about 6 years
    as range function doesn't include the ending value and stopped at (n-1)th position so if we use range(X) here it will result just 0 hence +1 should be used to print the list in lexicographic increasing order. @LordWilmore
  • MyNameIsCaleb
    MyNameIsCaleb over 4 years
    The provided answer was flagged for review as a Low Quality Post. Here are some guidelines for How do I write a good answer?. This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From review.
  • Brian Tompsett - 汤莱恩
    Brian Tompsett - 汤莱恩 over 3 years
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. You can use the edit button to improve this answer to get more votes and reputation!
  • Sillians
    Sillians over 2 years
    Nice approach, well done. How can you approach this using a list comprehension rather than multiple loops.