Home > Back-end >  how do i use list comprehensions to print a list of all possible dimensions of a cuboid in python?
how do i use list comprehensions to print a list of all possible dimensions of a cuboid in python?

Time:11-22

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.

I'm unable to solve this problem. Could anyone help me out with it?

CodePudding user response:

Try it online!

x, y, z, n = 2, 3, 4, 5
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])

Output:

[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 4), (0, 3, 0), (0, 3, 1), (0, 3, 3), (0, 3, 4), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 4), (1, 2, 0), (1, 2, 1), (1, 2, 3), (1, 2, 4), (1, 3, 0), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 4), (2, 1, 0), (2, 1, 1), (2, 1, 3), (2, 1, 4), (2, 2, 0), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)]
  • Related